Skip to content

Commit fcdf963

Browse files
committed
Record for each MIR local where it has been introduced.
1 parent 730197e commit fcdf963

File tree

12 files changed

+38
-22
lines changed

12 files changed

+38
-22
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
139139
// whether or not the right-hand side is a place expression
140140
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
141141
opt_match_place: Some((opt_match_place, match_span)),
142-
binding_mode: _,
143-
opt_ty_info: _,
144-
pat_span: _,
142+
..
145143
})) = *local_decl.local_info()
146144
{
147145
let stmt_source_info = self.body.source_info(location);

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
335335
LocalInfo::User(BindingForm::Var(mir::VarBindingForm {
336336
binding_mode: BindingMode(ByRef::No, Mutability::Not),
337337
opt_ty_info: Some(sp),
338-
opt_match_place: _,
339-
pat_span: _,
338+
..
340339
})) => {
341340
if suggest {
342341
err.span_note(sp, "the binding is already a mutable borrow");
@@ -750,6 +749,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
750749
opt_ty_info: _,
751750
opt_match_place: _,
752751
pat_span,
752+
introductions: _,
753753
})) => pat_span,
754754
_ => local_decl.source_info.span,
755755
};

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,8 @@ pub struct VarBindingForm<'tcx> {
882882
pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
883883
/// The span of the pattern in which this variable was bound.
884884
pub pat_span: Span,
885+
/// For each introduction place, record here the span and whether this was a shorthand pattern.
886+
pub introductions: Vec<(Span, /* is_shorthand */ bool)>,
885887
}
886888

887889
#[derive(Clone, Debug, TyEncodable, TyDecodable)]
@@ -1088,12 +1090,8 @@ impl<'tcx> LocalDecl<'tcx> {
10881090
matches!(
10891091
self.local_info(),
10901092
LocalInfo::User(
1091-
BindingForm::Var(VarBindingForm {
1092-
binding_mode: BindingMode(ByRef::No, _),
1093-
opt_ty_info: _,
1094-
opt_match_place: _,
1095-
pat_span: _,
1096-
}) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
1093+
BindingForm::Var(VarBindingForm { binding_mode: BindingMode(ByRef::No, _), .. })
1094+
| BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
10971095
)
10981096
)
10991097
}
@@ -1105,12 +1103,8 @@ impl<'tcx> LocalDecl<'tcx> {
11051103
matches!(
11061104
self.local_info(),
11071105
LocalInfo::User(
1108-
BindingForm::Var(VarBindingForm {
1109-
binding_mode: BindingMode(ByRef::No, _),
1110-
opt_ty_info: _,
1111-
opt_match_place: _,
1112-
pat_span: _,
1113-
}) | BindingForm::ImplicitSelf(_),
1106+
BindingForm::Var(VarBindingForm { binding_mode: BindingMode(ByRef::No, _), .. })
1107+
| BindingForm::ImplicitSelf(_),
11141108
)
11151109
)
11161110
}

compiler/rustc_middle/src/thir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ pub enum PatKind<'tcx> {
798798
/// (The same binding can occur multiple times in different branches of
799799
/// an or-pattern, but only one of them will be primary.)
800800
is_primary: bool,
801+
is_shorthand: bool,
801802
},
802803

803804
/// `Foo(...)` or `Foo{...}` or `Foo`, where `Foo` is a variant name from an ADT with

compiler/rustc_mir_build/src/builder/block.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
286286
block,
287287
node,
288288
span,
289+
false,
289290
OutsideGuard,
290291
ScheduleDrops::Yes,
291292
);

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<'tcx> MatchPairTree<'tcx> {
170170
None
171171
}
172172

173-
PatKind::Binding { mode, var, ref subpattern, .. } => {
173+
PatKind::Binding { mode, var, is_shorthand, ref subpattern, .. } => {
174174
// In order to please the borrow checker, when lowering a pattern
175175
// like `x @ subpat` we must establish any bindings in `subpat`
176176
// before establishing the binding for `x`.
@@ -209,6 +209,7 @@ impl<'tcx> MatchPairTree<'tcx> {
209209
source,
210210
var_id: var,
211211
binding_mode: mode,
212+
is_shorthand,
212213
}));
213214
}
214215

compiler/rustc_mir_build/src/builder/matches/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
579579
block,
580580
var,
581581
irrefutable_pat.span,
582+
false,
582583
OutsideGuard,
583584
ScheduleDrops::Yes,
584585
);
@@ -608,6 +609,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
608609
block,
609610
var,
610611
irrefutable_pat.span,
612+
false,
611613
OutsideGuard,
612614
ScheduleDrops::Yes,
613615
);
@@ -799,6 +801,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
799801
block: BasicBlock,
800802
var: LocalVarId,
801803
span: Span,
804+
is_shorthand: bool,
802805
for_guard: ForGuard,
803806
schedule_drop: ScheduleDrops,
804807
) -> Place<'tcx> {
@@ -812,6 +815,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
812815
{
813816
self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
814817
}
818+
let local_info = self.local_decls[local_id].local_info.as_mut().unwrap_crate_local();
819+
if let LocalInfo::User(BindingForm::Var(var_info)) = &mut **local_info {
820+
var_info.introductions.push((span, is_shorthand));
821+
}
815822
Place::from(local_id)
816823
}
817824

@@ -1217,6 +1224,7 @@ struct Binding<'tcx> {
12171224
source: Place<'tcx>,
12181225
var_id: LocalVarId,
12191226
binding_mode: BindingMode,
1227+
is_shorthand: bool,
12201228
}
12211229

12221230
/// Indicates that the type of `source` must be a subtype of the
@@ -2725,6 +2733,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
27252733
block,
27262734
binding.var_id,
27272735
binding.span,
2736+
binding.is_shorthand,
27282737
RefWithinGuard,
27292738
ScheduleDrops::Yes,
27302739
);
@@ -2742,6 +2751,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
27422751
block,
27432752
binding.var_id,
27442753
binding.span,
2754+
binding.is_shorthand,
27452755
OutsideGuard,
27462756
ScheduleDrops::Yes,
27472757
);
@@ -2775,6 +2785,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
27752785
block,
27762786
binding.var_id,
27772787
binding.span,
2788+
binding.is_shorthand,
27782789
OutsideGuard,
27792790
schedule_drops,
27802791
);
@@ -2827,6 +2838,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
28272838
opt_ty_info: None,
28282839
opt_match_place,
28292840
pat_span,
2841+
introductions: Vec::new(),
28302842
},
28312843
)))),
28322844
};

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10541054
opt_ty_info: param.ty_span,
10551055
opt_match_place: Some((None, span)),
10561056
pat_span: span,
1057+
introductions: vec![(span, false)],
10571058
}))
10581059
};
10591060
self.var_indices.insert(var, LocalsForNode::One(local));

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
369369
ty: var_ty,
370370
subpattern: self.lower_opt_pattern(sub),
371371
is_primary: id == pat.hir_id,
372+
is_shorthand: false,
372373
}
373374
}
374375

@@ -386,9 +387,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
386387
let res = self.typeck_results.qpath_res(qpath, pat.hir_id);
387388
let subpatterns = fields
388389
.iter()
389-
.map(|field| FieldPat {
390-
field: self.typeck_results.field_index(field.hir_id),
391-
pattern: *self.lower_pattern(field.pat),
390+
.map(|field| {
391+
let mut pattern = *self.lower_pattern(field.pat);
392+
if let PatKind::Binding { ref mut is_shorthand, .. } = pattern.kind {
393+
*is_shorthand = field.is_shorthand;
394+
}
395+
let field = self.typeck_results.field_index(field.hir_id);
396+
FieldPat { field, pattern }
392397
})
393398
.collect();
394399

compiler/rustc_mir_build/src/thir/print.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,13 +703,14 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
703703
self.print_pat(subpattern, depth_lvl + 3);
704704
print_indented!(self, "}", depth_lvl + 1);
705705
}
706-
PatKind::Binding { name, mode, var, ty, subpattern, is_primary } => {
706+
PatKind::Binding { name, mode, var, ty, subpattern, is_primary, is_shorthand } => {
707707
print_indented!(self, "Binding {", depth_lvl + 1);
708708
print_indented!(self, format!("name: {:?}", name), depth_lvl + 2);
709709
print_indented!(self, format!("mode: {:?}", mode), depth_lvl + 2);
710710
print_indented!(self, format!("var: {:?}", var), depth_lvl + 2);
711711
print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 2);
712712
print_indented!(self, format!("is_primary: {:?}", is_primary), depth_lvl + 2);
713+
print_indented!(self, format!("is_shorthand: {:?}", is_shorthand), depth_lvl + 2);
713714

714715
if let Some(subpattern) = subpattern {
715716
print_indented!(self, "subpattern: Some( ", depth_lvl + 2);

0 commit comments

Comments
 (0)