Skip to content

Commit bcb161d

Browse files
committed
Wrap the whole LocalInfo in ClearCrossCrate.
1 parent 2e7034e commit bcb161d

File tree

19 files changed

+134
-142
lines changed

19 files changed

+134
-142
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,15 +2482,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
24822482
let (place_description, assigned_span) = match local_decl {
24832483
Some(LocalDecl {
24842484
local_info:
2485-
Some(box LocalInfo::User(
2486-
ClearCrossCrate::Clear
2487-
| ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
2485+
ClearCrossCrate::Set(
2486+
box LocalInfo::User(BindingForm::Var(VarBindingForm {
24882487
opt_match_place: None,
24892488
..
2490-
})),
2491-
))
2492-
| Some(box LocalInfo::StaticRef { .. })
2493-
| None,
2489+
}))
2490+
| box LocalInfo::StaticRef { .. }
2491+
| box LocalInfo::Boring,
2492+
),
24942493
..
24952494
})
24962495
| None => (self.describe_any_place(place.as_ref()), assigned_span),

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 8 additions & 11 deletions
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

Lines changed: 38 additions & 54 deletions
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

Lines changed: 1 addition & 4 deletions
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
}

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

Lines changed: 1 addition & 1 deletion
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_middle/src/mir/mod.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,13 @@ impl<T> ClearCrossCrate<T> {
572572
}
573573
}
574574

575+
pub fn as_mut(&mut self) -> ClearCrossCrate<&mut T> {
576+
match self {
577+
ClearCrossCrate::Clear => ClearCrossCrate::Clear,
578+
ClearCrossCrate::Set(v) => ClearCrossCrate::Set(v),
579+
}
580+
}
581+
575582
pub fn assert_crate_local(self) -> T {
576583
match self {
577584
ClearCrossCrate::Clear => bug!("unwrapping cross-crate data"),
@@ -760,7 +767,7 @@ pub struct LocalDecl<'tcx> {
760767
pub mutability: Mutability,
761768

762769
// FIXME(matthewjasper) Don't store in this in `Body`
763-
pub local_info: Option<Box<LocalInfo<'tcx>>>,
770+
pub local_info: ClearCrossCrate<Box<LocalInfo<'tcx>>>,
764771

765772
/// `true` if this is an internal local.
766773
///
@@ -890,7 +897,7 @@ pub enum LocalInfo<'tcx> {
890897
/// The `BindingForm` is solely used for local diagnostics when generating
891898
/// warnings/errors when compiling the current crate, and therefore it need
892899
/// not be visible across crates.
893-
User(ClearCrossCrate<BindingForm<'tcx>>),
900+
User(BindingForm<'tcx>),
894901
/// A temporary created that references the static with the given `DefId`.
895902
StaticRef { def_id: DefId, is_thread_local: bool },
896903
/// A temporary created that references the const with the given `DefId`
@@ -902,9 +909,15 @@ pub enum LocalInfo<'tcx> {
902909
DerefTemp,
903910
/// A temporary created for borrow checking.
904911
FakeBorrow,
912+
/// A local without anything interesting about it.
913+
Boring,
905914
}
906915

907916
impl<'tcx> LocalDecl<'tcx> {
917+
pub fn local_info(&self) -> &LocalInfo<'tcx> {
918+
&**self.local_info.as_ref().assert_crate_local()
919+
}
920+
908921
/// Returns `true` only if local is a binding that can itself be
909922
/// made mutable via the addition of the `mut` keyword, namely
910923
/// something like the occurrences of `x` in:
@@ -914,14 +927,14 @@ impl<'tcx> LocalDecl<'tcx> {
914927
pub fn can_be_made_mutable(&self) -> bool {
915928
matches!(
916929
self.local_info,
917-
Some(box LocalInfo::User(ClearCrossCrate::Set(
930+
ClearCrossCrate::Set(box LocalInfo::User(
918931
BindingForm::Var(VarBindingForm {
919932
binding_mode: ty::BindingMode::BindByValue(_),
920933
opt_ty_info: _,
921934
opt_match_place: _,
922935
pat_span: _,
923936
}) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
924-
)))
937+
))
925938
)
926939
}
927940

@@ -931,22 +944,22 @@ impl<'tcx> LocalDecl<'tcx> {
931944
pub fn is_nonref_binding(&self) -> bool {
932945
matches!(
933946
self.local_info,
934-
Some(box LocalInfo::User(ClearCrossCrate::Set(
947+
ClearCrossCrate::Set(box LocalInfo::User(
935948
BindingForm::Var(VarBindingForm {
936949
binding_mode: ty::BindingMode::BindByValue(_),
937950
opt_ty_info: _,
938951
opt_match_place: _,
939952
pat_span: _,
940953
}) | BindingForm::ImplicitSelf(_),
941-
)))
954+
))
942955
)
943956
}
944957

945958
/// Returns `true` if this variable is a named variable or function
946959
/// parameter declared by the user.
947960
#[inline]
948961
pub fn is_user_variable(&self) -> bool {
949-
matches!(self.local_info, Some(box LocalInfo::User(_)))
962+
matches!(self.local_info, ClearCrossCrate::Set(box LocalInfo::User(_)))
950963
}
951964

952965
/// Returns `true` if this is a reference to a variable bound in a `match`
@@ -955,29 +968,31 @@ impl<'tcx> LocalDecl<'tcx> {
955968
pub fn is_ref_for_guard(&self) -> bool {
956969
matches!(
957970
self.local_info,
958-
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::RefForGuard)))
971+
ClearCrossCrate::Set(box LocalInfo::User(BindingForm::RefForGuard))
959972
)
960973
}
961974

962975
/// Returns `Some` if this is a reference to a static item that is used to
963976
/// access that static.
964977
pub fn is_ref_to_static(&self) -> bool {
965-
matches!(self.local_info, Some(box LocalInfo::StaticRef { .. }))
978+
matches!(self.local_info, ClearCrossCrate::Set(box LocalInfo::StaticRef { .. }))
966979
}
967980

968981
/// Returns `Some` if this is a reference to a thread-local static item that is used to
969982
/// access that static.
970983
pub fn is_ref_to_thread_local(&self) -> bool {
971984
match self.local_info {
972-
Some(box LocalInfo::StaticRef { is_thread_local, .. }) => is_thread_local,
985+
ClearCrossCrate::Set(box LocalInfo::StaticRef { is_thread_local, .. }) => {
986+
is_thread_local
987+
}
973988
_ => false,
974989
}
975990
}
976991

977992
/// Returns `true` if this is a DerefTemp
978993
pub fn is_deref_temp(&self) -> bool {
979994
match self.local_info {
980-
Some(box LocalInfo::DerefTemp) => return true,
995+
ClearCrossCrate::Set(box LocalInfo::DerefTemp) => return true,
981996
_ => (),
982997
}
983998
return false;
@@ -1001,7 +1016,7 @@ impl<'tcx> LocalDecl<'tcx> {
10011016
pub fn with_source_info(ty: Ty<'tcx>, source_info: SourceInfo) -> Self {
10021017
LocalDecl {
10031018
mutability: Mutability::Mut,
1004-
local_info: None,
1019+
local_info: ClearCrossCrate::Set(Box::new(LocalInfo::Boring)),
10051020
internal: false,
10061021
is_block_tail: None,
10071022
ty,

0 commit comments

Comments
 (0)