Skip to content

Commit 511364e

Browse files
committed
Auto merge of rust-lang#108944 - cjgillot:clear-local-info, r=oli-obk
Wrap the whole LocalInfo in ClearCrossCrate. MIR contains a lot of information about locals. The primary purpose of this information is the quality of borrowck diagnostics. This PR aims to drop this information after MIR analyses are finished, ie. starting from post-cleanup runtime MIR.
2 parents e386217 + 2adf2cd commit 511364e

File tree

30 files changed

+188
-214
lines changed

30 files changed

+188
-214
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -1985,16 +1985,18 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19851985
let (place_desc, note) = if let Some(place_desc) = opt_place_desc {
19861986
let local_kind = if let Some(local) = borrow.borrowed_place.as_local() {
19871987
match self.body.local_kind(local) {
1988-
LocalKind::ReturnPointer | LocalKind::Temp => {
1989-
bug!("temporary or return pointer with a name")
1988+
LocalKind::Temp if self.body.local_decls[local].is_user_variable() => {
1989+
"local variable "
19901990
}
1991-
LocalKind::Var => "local variable ",
19921991
LocalKind::Arg
19931992
if !self.upvars.is_empty() && local == ty::CAPTURE_STRUCT_LOCAL =>
19941993
{
19951994
"variable captured by `move` "
19961995
}
19971996
LocalKind::Arg => "function parameter ",
1997+
LocalKind::ReturnPointer | LocalKind::Temp => {
1998+
bug!("temporary or return pointer with a name")
1999+
}
19982000
}
19992001
} else {
20002002
"local data "
@@ -2008,16 +2010,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20082010
self.prefixes(borrow.borrowed_place.as_ref(), PrefixSet::All).last().unwrap();
20092011
let local = root_place.local;
20102012
match self.body.local_kind(local) {
2011-
LocalKind::ReturnPointer | LocalKind::Temp => {
2012-
("temporary value".to_string(), "temporary value created here".to_string())
2013-
}
20142013
LocalKind::Arg => (
20152014
"function parameter".to_string(),
20162015
"function parameter borrowed here".to_string(),
20172016
),
2018-
LocalKind::Var => {
2017+
LocalKind::Temp if self.body.local_decls[local].is_user_variable() => {
20192018
("local binding".to_string(), "local binding introduced here".to_string())
20202019
}
2020+
LocalKind::ReturnPointer | LocalKind::Temp => {
2021+
("temporary value".to_string(), "temporary value created here".to_string())
2022+
}
20212023
}
20222024
};
20232025

@@ -2482,15 +2484,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
24822484
let (place_description, assigned_span) = match local_decl {
24832485
Some(LocalDecl {
24842486
local_info:
2485-
Some(box LocalInfo::User(
2486-
ClearCrossCrate::Clear
2487-
| ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
2487+
ClearCrossCrate::Set(
2488+
box LocalInfo::User(BindingForm::Var(VarBindingForm {
24882489
opt_match_place: None,
24892490
..
2490-
})),
2491-
))
2492-
| Some(box LocalInfo::StaticRef { .. })
2493-
| None,
2491+
}))
2492+
| box LocalInfo::StaticRef { .. }
2493+
| box LocalInfo::Boring,
2494+
),
24942495
..
24952496
})
24962497
| None => (self.describe_any_place(place.as_ref()), assigned_span),

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustc_hir::intravisit::Visitor;
66
use rustc_index::vec::IndexVec;
77
use rustc_infer::infer::NllRegionVariableOrigin;
88
use rustc_middle::mir::{
9-
Body, CastKind, ConstraintCategory, FakeReadCause, Local, Location, Operand, Place, Rvalue,
10-
Statement, StatementKind, TerminatorKind,
9+
Body, CastKind, ConstraintCategory, FakeReadCause, Local, LocalInfo, Location, Operand, Place,
10+
Rvalue, Statement, StatementKind, TerminatorKind,
1111
};
1212
use rustc_middle::ty::adjustment::PointerCast;
1313
use rustc_middle::ty::{self, RegionVid, TyCtxt};
@@ -220,7 +220,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
220220
);
221221
err.span_label(body.source_info(drop_loc).span, message);
222222

223-
if let Some(info) = &local_decl.is_block_tail {
223+
if let LocalInfo::BlockTailTemp(info) = local_decl.local_info() {
224224
if info.tail_result_is_ignored {
225225
// #85581: If the first mutable borrow's scope contains
226226
// the second borrow, this suggestion isn't helpful.

compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
196196
if self.body.local_decls[local].is_ref_for_guard() {
197197
continue;
198198
}
199-
if let Some(box LocalInfo::StaticRef { def_id, .. }) =
200-
&self.body.local_decls[local].local_info
199+
if let LocalInfo::StaticRef { def_id, .. } =
200+
*self.body.local_decls[local].local_info()
201201
{
202-
buf.push_str(self.infcx.tcx.item_name(*def_id).as_str());
202+
buf.push_str(self.infcx.tcx.item_name(def_id).as_str());
203203
ok = Ok(());
204204
continue;
205205
}

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
102102
//
103103
// opt_match_place is None for let [mut] x = ... statements,
104104
// whether or not the right-hand side is a place expression
105-
if let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
106-
VarBindingForm {
107-
opt_match_place: Some((opt_match_place, match_span)),
108-
binding_mode: _,
109-
opt_ty_info: _,
110-
pat_span: _,
111-
},
112-
)))) = local_decl.local_info
105+
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
106+
opt_match_place: Some((opt_match_place, match_span)),
107+
binding_mode: _,
108+
opt_ty_info: _,
109+
pat_span: _,
110+
})) = *local_decl.local_info()
113111
{
114112
let stmt_source_info = self.body.source_info(location);
115113
self.append_binding_error(
@@ -478,9 +476,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
478476
let mut suggestions: Vec<(Span, String, String)> = Vec::new();
479477
for local in binds_to {
480478
let bind_to = &self.body.local_decls[*local];
481-
if let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
482-
VarBindingForm { pat_span, .. },
483-
)))) = bind_to.local_info
479+
if let LocalInfo::User(BindingForm::Var(VarBindingForm { pat_span, .. })) =
480+
*bind_to.local_info()
484481
{
485482
let Ok(pat_snippet) =
486483
self.infcx.tcx.sess.source_map().span_to_snippet(pat_span) else { continue; };

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+38-54
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
77
use rustc_middle::ty::{self, Ty, TyCtxt};
88
use rustc_middle::{
99
hir::place::PlaceBase,
10-
mir::{self, BindingForm, ClearCrossCrate, Local, LocalDecl, LocalInfo, LocalKind, Location},
10+
mir::{self, BindingForm, Local, LocalDecl, LocalInfo, LocalKind, Location},
1111
};
1212
use rustc_span::source_map::DesugaringKind;
1313
use rustc_span::symbol::{kw, Symbol};
@@ -105,8 +105,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
105105
reason = String::new();
106106
} else {
107107
item_msg = access_place_desc;
108-
let local_info = &self.body.local_decls[local].local_info;
109-
if let Some(box LocalInfo::StaticRef { def_id, .. }) = *local_info {
108+
let local_info = self.body.local_decls[local].local_info();
109+
if let LocalInfo::StaticRef { def_id, .. } = *local_info {
110110
let static_name = &self.infcx.tcx.item_name(def_id);
111111
reason = format!(", as `{static_name}` is an immutable static item");
112112
} else {
@@ -305,15 +305,13 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
305305
..
306306
}) = &self.body[location.block].statements.get(location.statement_index)
307307
{
308-
match decl.local_info {
309-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
310-
mir::VarBindingForm {
311-
binding_mode: ty::BindingMode::BindByValue(Mutability::Not),
312-
opt_ty_info: Some(sp),
313-
opt_match_place: _,
314-
pat_span: _,
315-
},
316-
)))) => {
308+
match *decl.local_info() {
309+
LocalInfo::User(BindingForm::Var(mir::VarBindingForm {
310+
binding_mode: ty::BindingMode::BindByValue(Mutability::Not),
311+
opt_ty_info: Some(sp),
312+
opt_match_place: _,
313+
pat_span: _,
314+
})) => {
317315
if suggest {
318316
err.span_note(sp, "the binding is already a mutable borrow");
319317
}
@@ -346,10 +344,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
346344
}
347345
} else if decl.mutability.is_not() {
348346
if matches!(
349-
decl.local_info,
350-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
351-
hir::ImplicitSelfKind::MutRef
352-
),)))
347+
decl.local_info(),
348+
LocalInfo::User(BindingForm::ImplicitSelf(hir::ImplicitSelfKind::MutRef))
353349
) {
354350
err.note(
355351
"as `Self` may be unsized, this call attempts to take `&mut &mut self`",
@@ -482,22 +478,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
482478

483479
match self.local_names[local] {
484480
Some(name) if !local_decl.from_compiler_desugaring() => {
485-
let label = match local_decl.local_info.as_deref().unwrap() {
486-
LocalInfo::User(ClearCrossCrate::Set(
487-
mir::BindingForm::ImplicitSelf(_),
488-
)) => {
481+
let label = match *local_decl.local_info() {
482+
LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => {
489483
let (span, suggestion) =
490484
suggest_ampmut_self(self.infcx.tcx, local_decl);
491485
Some((true, span, suggestion))
492486
}
493487

494-
LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::Var(
495-
mir::VarBindingForm {
496-
binding_mode: ty::BindingMode::BindByValue(_),
497-
opt_ty_info,
498-
..
499-
},
500-
))) => {
488+
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
489+
binding_mode: ty::BindingMode::BindByValue(_),
490+
opt_ty_info,
491+
..
492+
})) => {
501493
// check if the RHS is from desugaring
502494
let opt_assignment_rhs_span =
503495
self.body.find_assignments(local).first().map(|&location| {
@@ -534,16 +526,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
534526
self.infcx.tcx,
535527
local_decl,
536528
opt_assignment_rhs_span,
537-
*opt_ty_info,
529+
opt_ty_info,
538530
)
539531
} else {
540-
match local_decl.local_info.as_deref() {
541-
Some(LocalInfo::User(ClearCrossCrate::Set(
542-
mir::BindingForm::Var(mir::VarBindingForm {
543-
opt_ty_info: None,
544-
..
545-
}),
546-
))) => {
532+
match local_decl.local_info() {
533+
LocalInfo::User(mir::BindingForm::Var(
534+
mir::VarBindingForm {
535+
opt_ty_info: None, ..
536+
},
537+
)) => {
547538
let (span, sugg) = suggest_ampmut_self(
548539
self.infcx.tcx,
549540
local_decl,
@@ -555,7 +546,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
555546
self.infcx.tcx,
556547
local_decl,
557548
opt_assignment_rhs_span,
558-
*opt_ty_info,
549+
opt_ty_info,
559550
),
560551
}
561552
};
@@ -564,21 +555,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
564555
}
565556
}
566557

567-
LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::Var(
568-
mir::VarBindingForm {
569-
binding_mode: ty::BindingMode::BindByReference(_),
570-
..
571-
},
572-
))) => {
558+
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
559+
binding_mode: ty::BindingMode::BindByReference(_),
560+
..
561+
})) => {
573562
let pattern_span = local_decl.source_info.span;
574563
suggest_ref_mut(self.infcx.tcx, pattern_span)
575564
.map(|replacement| (true, pattern_span, replacement))
576565
}
577566

578-
LocalInfo::User(ClearCrossCrate::Clear) => {
579-
bug!("saw cleared local state")
580-
}
581-
582567
_ => unreachable!(),
583568
};
584569

@@ -1151,20 +1136,19 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11511136
pub fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symbol>) -> bool {
11521137
debug!("local_info: {:?}, ty.kind(): {:?}", local_decl.local_info, local_decl.ty.kind());
11531138

1154-
match local_decl.local_info.as_deref() {
1139+
match *local_decl.local_info() {
11551140
// Check if mutably borrowing a mutable reference.
1156-
Some(LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::Var(
1157-
mir::VarBindingForm {
1158-
binding_mode: ty::BindingMode::BindByValue(Mutability::Not), ..
1159-
},
1160-
)))) => matches!(local_decl.ty.kind(), ty::Ref(_, _, hir::Mutability::Mut)),
1161-
Some(LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf(kind)))) => {
1141+
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
1142+
binding_mode: ty::BindingMode::BindByValue(Mutability::Not),
1143+
..
1144+
})) => matches!(local_decl.ty.kind(), ty::Ref(_, _, hir::Mutability::Mut)),
1145+
LocalInfo::User(mir::BindingForm::ImplicitSelf(kind)) => {
11621146
// Check if the user variable is a `&mut self` and we can therefore
11631147
// suggest removing the `&mut`.
11641148
//
11651149
// Deliberately fall into this case for all implicit self types,
11661150
// so that we don't fall in to the next case with them.
1167-
*kind == hir::ImplicitSelfKind::MutRef
1151+
kind == hir::ImplicitSelfKind::MutRef
11681152
}
11691153
_ if Some(kw::SelfLower) == local_name => {
11701154
// Otherwise, check if the name is the `self` keyword - in which case

compiler/rustc_borrowck/src/type_check/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1180,10 +1180,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11801180
}
11811181
}
11821182
Some(l)
1183-
if matches!(
1184-
body.local_decls[l].local_info,
1185-
Some(box LocalInfo::AggregateTemp)
1186-
) =>
1183+
if matches!(body.local_decls[l].local_info(), LocalInfo::AggregateTemp) =>
11871184
{
11881185
ConstraintCategory::Usage
11891186
}
@@ -1684,7 +1681,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
16841681
// - maybe we should make that a warning.
16851682
return;
16861683
}
1687-
LocalKind::Var | LocalKind::Temp => {}
1684+
LocalKind::Temp => {}
16881685
}
16891686

16901687
// When `unsized_fn_params` or `unsized_locals` is enabled, only function calls

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
241241
pub fn debug_introduce_local(&self, bx: &mut Bx, local: mir::Local) {
242242
let full_debug_info = bx.sess().opts.debuginfo == DebugInfo::Full;
243243

244-
// FIXME(eddyb) maybe name the return place as `_0` or `return`?
245-
if local == mir::RETURN_PLACE && !self.mir.local_decls[mir::RETURN_PLACE].is_user_variable()
246-
{
247-
return;
248-
}
249-
250244
let vars = match &self.per_local_var_debug_info {
251245
Some(per_local) => &per_local[local],
252246
None => return,
@@ -303,7 +297,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
303297

304298
let local_ref = &self.locals[local];
305299

306-
let name = if bx.sess().fewer_names() {
300+
// FIXME Should the return place be named?
301+
let name = if bx.sess().fewer_names() || local == mir::RETURN_PLACE {
307302
None
308303
} else {
309304
Some(match whole_local_var.or(fallback_var.clone()) {

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
643643
if base_ty.is_unsafe_ptr() {
644644
if proj_base.is_empty() {
645645
let decl = &self.body.local_decls[place_local];
646-
if let Some(box LocalInfo::StaticRef { def_id, .. }) = decl.local_info {
646+
if let LocalInfo::StaticRef { def_id, .. } = *decl.local_info() {
647647
let span = decl.source_info.span;
648648
self.check_static(def_id, span);
649649
return;

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ pub mod ty {
704704

705705
fn importance(&self) -> DiagnosticImportance {
706706
match self.0 {
707-
mir::LocalKind::Var | mir::LocalKind::Temp => DiagnosticImportance::Secondary,
707+
mir::LocalKind::Temp => DiagnosticImportance::Secondary,
708708
mir::LocalKind::ReturnPointer | mir::LocalKind::Arg => {
709709
DiagnosticImportance::Primary
710710
}

0 commit comments

Comments
 (0)