Skip to content

Commit d8810e3

Browse files
committed
Auto merge of #137030 - matthiaskrgr:rollup-267aumr, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #135778 (account for `c_enum_min_bits` in `multiple-reprs` UI test) - #136052 (Correct comment for FreeBSD and DragonFly BSD in unix/thread) - #136886 (Remove the common prelude module) - #136956 (add vendor directory to .gitignore) - #136958 (Fix presentation of purely "additive" replacement suggestion parts) - #136967 (Use `slice::fill` in `io::Repeat` implementation) - #136976 (alloc boxed: docs: use MaybeUninit::write instead of as_mut_ptr) - #137007 (Emit MIR for each bit with on `dont_reset_cast_kind_without_updating_operand`) - #137008 (Move code into `rustc_mir_transform`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents bdc97d1 + bd094fb commit d8810e3

File tree

189 files changed

+1068
-1104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+1068
-1104
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ no_llvm_build
5454
/library/target
5555
/src/bootstrap/target
5656
/src/tools/x/target
57+
# Created by `x vendor`
58+
/vendor
5759
# Created by default with `src/ci/docker/run.sh`
5860
/obj/
5961
# Created by nix dev shell / .envrc

compiler/rustc_errors/src/emitter.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -1976,13 +1976,16 @@ impl HumanEmitter {
19761976
Some(Style::HeaderMsg),
19771977
);
19781978

1979+
let other_suggestions = suggestions.len().saturating_sub(MAX_SUGGESTIONS);
1980+
19791981
let mut row_num = 2;
19801982
for (i, (complete, parts, highlights, _)) in
1981-
suggestions.iter().enumerate().take(MAX_SUGGESTIONS)
1983+
suggestions.into_iter().enumerate().take(MAX_SUGGESTIONS)
19821984
{
19831985
debug!(?complete, ?parts, ?highlights);
19841986

1985-
let has_deletion = parts.iter().any(|p| p.is_deletion(sm) || p.is_replacement(sm));
1987+
let has_deletion =
1988+
parts.iter().any(|p| p.is_deletion(sm) || p.is_destructive_replacement(sm));
19861989
let is_multiline = complete.lines().count() > 1;
19871990

19881991
if i == 0 {
@@ -2167,7 +2170,7 @@ impl HumanEmitter {
21672170
self.draw_code_line(
21682171
&mut buffer,
21692172
&mut row_num,
2170-
highlight_parts,
2173+
&highlight_parts,
21712174
line_pos + line_start,
21722175
line,
21732176
show_code_change,
@@ -2213,7 +2216,12 @@ impl HumanEmitter {
22132216
if let DisplaySuggestion::Diff | DisplaySuggestion::Underline | DisplaySuggestion::Add =
22142217
show_code_change
22152218
{
2216-
for part in parts {
2219+
for mut part in parts {
2220+
// If this is a replacement of, e.g. `"a"` into `"ab"`, adjust the
2221+
// suggestion and snippet to look as if we just suggested to add
2222+
// `"b"`, which is typically much easier for the user to understand.
2223+
part.trim_trivial_replacements(sm);
2224+
22172225
let snippet = if let Ok(snippet) = sm.span_to_snippet(part.span) {
22182226
snippet
22192227
} else {
@@ -2376,9 +2384,12 @@ impl HumanEmitter {
23762384
row_num = row + 1;
23772385
}
23782386
}
2379-
if suggestions.len() > MAX_SUGGESTIONS {
2380-
let others = suggestions.len() - MAX_SUGGESTIONS;
2381-
let msg = format!("and {} other candidate{}", others, pluralize!(others));
2387+
if other_suggestions > 0 {
2388+
let msg = format!(
2389+
"and {} other candidate{}",
2390+
other_suggestions,
2391+
pluralize!(other_suggestions)
2392+
);
23822393
buffer.puts(row_num, max_line_num_len + 3, &msg, Style::NoStyle);
23832394
}
23842395

compiler/rustc_errors/src/lib.rs

+30
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,40 @@ impl SubstitutionPart {
230230
!self.snippet.is_empty() && self.replaces_meaningful_content(sm)
231231
}
232232

233+
/// Whether this is a replacement that overwrites source with a snippet
234+
/// in a way that isn't a superset of the original string. For example,
235+
/// replacing "abc" with "abcde" is not destructive, but replacing it
236+
/// it with "abx" is, since the "c" character is lost.
237+
pub fn is_destructive_replacement(&self, sm: &SourceMap) -> bool {
238+
self.is_replacement(sm)
239+
&& !sm.span_to_snippet(self.span).is_ok_and(|snippet| {
240+
self.snippet.trim_start().starts_with(snippet.trim_start())
241+
|| self.snippet.trim_end().ends_with(snippet.trim_end())
242+
})
243+
}
244+
233245
fn replaces_meaningful_content(&self, sm: &SourceMap) -> bool {
234246
sm.span_to_snippet(self.span)
235247
.map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())
236248
}
249+
250+
/// Try to turn a replacement into an addition when the span that is being
251+
/// overwritten matches either the prefix or suffix of the replacement.
252+
fn trim_trivial_replacements(&mut self, sm: &SourceMap) {
253+
if self.snippet.is_empty() {
254+
return;
255+
}
256+
let Ok(snippet) = sm.span_to_snippet(self.span) else {
257+
return;
258+
};
259+
if self.snippet.starts_with(&snippet) {
260+
self.span = self.span.shrink_to_hi();
261+
self.snippet = self.snippet[snippet.len()..].to_string();
262+
} else if self.snippet.ends_with(&snippet) {
263+
self.span = self.span.shrink_to_lo();
264+
self.snippet = self.snippet[..self.snippet.len() - snippet.len()].to_string();
265+
}
266+
}
237267
}
238268

239269
impl CodeSuggestion {

compiler/rustc_middle/src/mir/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub mod generic_graphviz;
4949
pub mod graphviz;
5050
pub mod interpret;
5151
pub mod mono;
52-
pub mod patch;
5352
pub mod pretty;
5453
mod query;
5554
mod statement;

compiler/rustc_mir_dataflow/src/drop_flag_effects.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,26 @@ use rustc_middle::mir::{self, Body, Location, Terminator, TerminatorKind};
33
use tracing::debug;
44

55
use super::move_paths::{InitKind, LookupResult, MoveData, MovePathIndex};
6-
use crate::elaborate_drops::DropFlagState;
6+
7+
/// The value of an inserted drop flag.
8+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
9+
pub enum DropFlagState {
10+
/// The tracked value is initialized and needs to be dropped when leaving its scope.
11+
Present,
12+
13+
/// The tracked value is uninitialized or was moved out of and does not need to be dropped when
14+
/// leaving its scope.
15+
Absent,
16+
}
17+
18+
impl DropFlagState {
19+
pub fn value(self) -> bool {
20+
match self {
21+
DropFlagState::Present => true,
22+
DropFlagState::Absent => false,
23+
}
24+
}
25+
}
726

827
pub fn move_path_children_matching<'tcx, F>(
928
move_data: &MoveData<'tcx>,

compiler/rustc_mir_dataflow/src/impls/initialized.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::util::Discr;
99
use rustc_middle::ty::{self, TyCtxt};
1010
use tracing::{debug, instrument};
1111

12-
use crate::elaborate_drops::DropFlagState;
12+
use crate::drop_flag_effects::DropFlagState;
1313
use crate::framework::SwitchIntTarget;
1414
use crate::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex};
1515
use crate::{

compiler/rustc_mir_dataflow/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty;
1515
// Please change the public `use` directives cautiously, as they might be used by external tools.
1616
// See issue #120130.
1717
pub use self::drop_flag_effects::{
18-
drop_flag_effects_for_function_entry, drop_flag_effects_for_location,
18+
DropFlagState, drop_flag_effects_for_function_entry, drop_flag_effects_for_location,
1919
move_path_children_matching, on_all_children_bits, on_lookup_result_bits,
2020
};
2121
pub use self::framework::{
@@ -26,7 +26,6 @@ use self::move_paths::MoveData;
2626

2727
pub mod debuginfo;
2828
mod drop_flag_effects;
29-
pub mod elaborate_drops;
3029
mod errors;
3130
mod framework;
3231
pub mod impls;

compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use rustc_middle::mir::patch::MirPatch;
21
use rustc_middle::mir::*;
32
use rustc_middle::ty::{self, TyCtxt};
43
use tracing::debug;
54

5+
use crate::patch::MirPatch;
66
use crate::util;
77

88
/// This pass moves values being dropped that are within a packed

compiler/rustc_mir_transform/src/add_subtyping_projections.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use rustc_middle::mir::patch::MirPatch;
21
use rustc_middle::mir::visit::MutVisitor;
32
use rustc_middle::mir::*;
43
use rustc_middle::ty::TyCtxt;
54

5+
use crate::patch::MirPatch;
6+
67
pub(super) struct Subtyper;
78

89
struct SubTypeChecker<'a, 'tcx> {

compiler/rustc_mir_transform/src/coroutine.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1061,9 +1061,8 @@ fn insert_switch<'tcx>(
10611061
}
10621062

10631063
fn elaborate_coroutine_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1064-
use rustc_middle::mir::patch::MirPatch;
1065-
use rustc_mir_dataflow::elaborate_drops::{Unwind, elaborate_drop};
1066-
1064+
use crate::elaborate_drop::{Unwind, elaborate_drop};
1065+
use crate::patch::MirPatch;
10671066
use crate::shim::DropShimElaborator;
10681067

10691068
// Note that `elaborate_drops` only drops the upvars of a coroutine, and

compiler/rustc_mir_transform/src/deref_separator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use rustc_middle::mir::patch::MirPatch;
21
use rustc_middle::mir::visit::NonUseContext::VarDebugInfo;
32
use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
43
use rustc_middle::mir::*;
54
use rustc_middle::ty::TyCtxt;
65

6+
use crate::patch::MirPatch;
7+
78
pub(super) struct Derefer;
89

910
struct DerefChecker<'a, 'tcx> {

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::fmt::Debug;
22

3-
use rustc_middle::mir::patch::MirPatch;
43
use rustc_middle::mir::*;
54
use rustc_middle::ty::{Ty, TyCtxt};
65
use tracing::trace;
76

87
use super::simplify::simplify_cfg;
8+
use crate::patch::MirPatch;
99

1010
/// This pass optimizes something like
1111
/// ```ignore (syntax-highlighting-only)

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
55
use rustc_abi::FieldIdx;
66
use rustc_hir::def_id::DefId;
7-
use rustc_middle::mir::patch::MirPatch;
87
use rustc_middle::mir::visit::MutVisitor;
98
use rustc_middle::mir::*;
109
use rustc_middle::span_bug;
1110
use rustc_middle::ty::{Ty, TyCtxt};
1211

12+
use crate::patch::MirPatch;
13+
1314
/// Constructs the types used when accessing a Box's pointer
1415
fn build_ptr_tys<'tcx>(
1516
tcx: TyCtxt<'tcx>,

compiler/rustc_mir_dataflow/src/elaborate_drops.rs compiler/rustc_mir_transform/src/elaborate_drop.rs

+6-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{fmt, iter, mem};
33
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
44
use rustc_hir::lang_items::LangItem;
55
use rustc_index::Idx;
6-
use rustc_middle::mir::patch::MirPatch;
76
use rustc_middle::mir::*;
87
use rustc_middle::span_bug;
98
use rustc_middle::ty::adjustment::PointerCoercion;
@@ -13,29 +12,11 @@ use rustc_span::DUMMY_SP;
1312
use rustc_span::source_map::Spanned;
1413
use tracing::{debug, instrument};
1514

16-
/// The value of an inserted drop flag.
17-
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
18-
pub enum DropFlagState {
19-
/// The tracked value is initialized and needs to be dropped when leaving its scope.
20-
Present,
21-
22-
/// The tracked value is uninitialized or was moved out of and does not need to be dropped when
23-
/// leaving its scope.
24-
Absent,
25-
}
26-
27-
impl DropFlagState {
28-
pub fn value(self) -> bool {
29-
match self {
30-
DropFlagState::Present => true,
31-
DropFlagState::Absent => false,
32-
}
33-
}
34-
}
15+
use crate::patch::MirPatch;
3516

3617
/// Describes how/if a value should be dropped.
3718
#[derive(Debug)]
38-
pub enum DropStyle {
19+
pub(crate) enum DropStyle {
3920
/// The value is already dead at the drop location, no drop will be executed.
4021
Dead,
4122

@@ -56,7 +37,7 @@ pub enum DropStyle {
5637

5738
/// Which drop flags to affect/check with an operation.
5839
#[derive(Debug)]
59-
pub enum DropFlagMode {
40+
pub(crate) enum DropFlagMode {
6041
/// Only affect the top-level drop flag, not that of any contained fields.
6142
Shallow,
6243
/// Affect all nested drop flags in addition to the top-level one.
@@ -65,7 +46,7 @@ pub enum DropFlagMode {
6546

6647
/// Describes if unwinding is necessary and where to unwind to if a panic occurs.
6748
#[derive(Copy, Clone, Debug)]
68-
pub enum Unwind {
49+
pub(crate) enum Unwind {
6950
/// Unwind to this block.
7051
To(BasicBlock),
7152
/// Already in an unwind path, any panic will cause an abort.
@@ -98,7 +79,7 @@ impl Unwind {
9879
}
9980
}
10081

101-
pub trait DropElaborator<'a, 'tcx>: fmt::Debug {
82+
pub(crate) trait DropElaborator<'a, 'tcx>: fmt::Debug {
10283
/// The type representing paths that can be moved out of.
10384
///
10485
/// Users can move out of individual fields of a struct, such as `a.b.c`. This type is used to
@@ -177,7 +158,7 @@ where
177158
/// value.
178159
///
179160
/// When this returns, the MIR patch in the `elaborator` contains the necessary changes.
180-
pub fn elaborate_drop<'b, 'tcx, D>(
161+
pub(crate) fn elaborate_drop<'b, 'tcx, D>(
181162
elaborator: &mut D,
182163
source_info: SourceInfo,
183164
place: Place<'tcx>,

compiler/rustc_mir_transform/src/elaborate_drops.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ use std::fmt;
33
use rustc_abi::{FieldIdx, VariantIdx};
44
use rustc_index::IndexVec;
55
use rustc_index::bit_set::DenseBitSet;
6-
use rustc_middle::mir::patch::MirPatch;
76
use rustc_middle::mir::*;
87
use rustc_middle::ty::{self, TyCtxt};
9-
use rustc_mir_dataflow::elaborate_drops::{
10-
DropElaborator, DropFlagMode, DropFlagState, DropStyle, Unwind, elaborate_drop,
11-
};
128
use rustc_mir_dataflow::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
139
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
1410
use rustc_mir_dataflow::{
15-
Analysis, MoveDataTypingEnv, ResultsCursor, on_all_children_bits, on_lookup_result_bits,
11+
Analysis, DropFlagState, MoveDataTypingEnv, ResultsCursor, on_all_children_bits,
12+
on_lookup_result_bits,
1613
};
1714
use rustc_span::Span;
1815
use tracing::{debug, instrument};
1916

2017
use crate::deref_separator::deref_finder;
18+
use crate::elaborate_drop::{DropElaborator, DropFlagMode, DropStyle, Unwind, elaborate_drop};
19+
use crate::patch::MirPatch;
2120

2221
/// During MIR building, Drop terminators are inserted in every place where a drop may occur.
2322
/// However, in this phase, the presence of these terminators does not guarantee that a destructor

compiler/rustc_mir_transform/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ mod check_pointers;
4949
mod cost_checker;
5050
mod cross_crate_inline;
5151
mod deduce_param_attrs;
52+
mod elaborate_drop;
5253
mod errors;
5354
mod ffi_unwind_calls;
5455
mod lint;
5556
mod lint_tail_expr_drop_order;
57+
mod patch;
5658
mod shim;
5759
mod ssa;
5860

compiler/rustc_mir_transform/src/match_branches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::iter;
22

33
use rustc_abi::Integer;
44
use rustc_index::IndexSlice;
5-
use rustc_middle::mir::patch::MirPatch;
65
use rustc_middle::mir::*;
76
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
87
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
98
use rustc_type_ir::TyKind::*;
109
use tracing::instrument;
1110

1211
use super::simplify::simplify_cfg;
12+
use crate::patch::MirPatch;
1313

1414
pub(super) struct MatchBranchSimplification;
1515

0 commit comments

Comments
 (0)