Skip to content

Commit c70a55b

Browse files
committed
Record for each MIR local where it has been introduced.
1 parent 05effa2 commit c70a55b

File tree

11 files changed

+38
-22
lines changed

11 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
@@ -140,9 +140,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
140140
// whether or not the right-hand side is a place expression
141141
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
142142
opt_match_place: Some((opt_match_place, match_span)),
143-
binding_mode: _,
144-
opt_ty_info: _,
145-
pat_span: _,
143+
..
146144
})) = *local_decl.local_info()
147145
{
148146
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
@@ -307,8 +307,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
307307
LocalInfo::User(BindingForm::Var(mir::VarBindingForm {
308308
binding_mode: BindingMode(ByRef::No, Mutability::Not),
309309
opt_ty_info: Some(sp),
310-
opt_match_place: _,
311-
pat_span: _,
310+
..
312311
})) => {
313312
if suggest {
314313
err.span_note(sp, "the binding is already a mutable borrow");
@@ -722,6 +721,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
722721
opt_ty_info: _,
723722
opt_match_place: _,
724723
pat_span,
724+
introductions: _,
725725
})) => pat_span,
726726
_ => local_decl.source_info.span,
727727
};

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ pub struct VarBindingForm<'tcx> {
926926
pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
927927
/// The span of the pattern in which this variable was bound.
928928
pub pat_span: Span,
929+
/// For each introduction place, record here the span and whether this was a shorthand pattern.
930+
pub introductions: Vec<(Span, /* is_shorthand */ bool)>,
929931
}
930932

931933
#[derive(Clone, Debug, TyEncodable, TyDecodable)]
@@ -1131,12 +1133,8 @@ impl<'tcx> LocalDecl<'tcx> {
11311133
matches!(
11321134
self.local_info(),
11331135
LocalInfo::User(
1134-
BindingForm::Var(VarBindingForm {
1135-
binding_mode: BindingMode(ByRef::No, _),
1136-
opt_ty_info: _,
1137-
opt_match_place: _,
1138-
pat_span: _,
1139-
}) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
1136+
BindingForm::Var(VarBindingForm { binding_mode: BindingMode(ByRef::No, _), .. })
1137+
| BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
11401138
)
11411139
)
11421140
}
@@ -1148,12 +1146,8 @@ impl<'tcx> LocalDecl<'tcx> {
11481146
matches!(
11491147
self.local_info(),
11501148
LocalInfo::User(
1151-
BindingForm::Var(VarBindingForm {
1152-
binding_mode: BindingMode(ByRef::No, _),
1153-
opt_ty_info: _,
1154-
opt_match_place: _,
1155-
pat_span: _,
1156-
}) | BindingForm::ImplicitSelf(_),
1149+
BindingForm::Var(VarBindingForm { binding_mode: BindingMode(ByRef::No, _), .. })
1150+
| BindingForm::ImplicitSelf(_),
11571151
)
11581152
)
11591153
}

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
204204
block,
205205
node,
206206
span,
207+
false,
207208
OutsideGuard,
208209
ScheduleDrops::Yes,
209210
);
@@ -296,6 +297,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
296297
block,
297298
node,
298299
span,
300+
false,
299301
OutsideGuard,
300302
ScheduleDrops::Yes,
301303
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'tcx> MatchPairTree<'tcx> {
160160
None
161161
}
162162

163-
PatKind::Binding { mode, var, ref subpattern, .. } => {
163+
PatKind::Binding { mode, var, is_shorthand, ref subpattern, .. } => {
164164
// In order to please the borrow checker, when lowering a pattern
165165
// like `x @ subpat` we must establish any bindings in `subpat`
166166
// before establishing the binding for `x`.
@@ -199,6 +199,7 @@ impl<'tcx> MatchPairTree<'tcx> {
199199
source,
200200
var_id: var,
201201
binding_mode: mode,
202+
is_shorthand,
202203
});
203204
}
204205

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
602602
block,
603603
var,
604604
irrefutable_pat.span,
605+
false,
605606
OutsideGuard,
606607
ScheduleDrops::Yes,
607608
);
@@ -631,6 +632,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
631632
block,
632633
var,
633634
irrefutable_pat.span,
635+
false,
634636
OutsideGuard,
635637
ScheduleDrops::Yes,
636638
);
@@ -823,6 +825,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
823825
block: BasicBlock,
824826
var: LocalVarId,
825827
span: Span,
828+
is_shorthand: bool,
826829
for_guard: ForGuard,
827830
schedule_drop: ScheduleDrops,
828831
) -> Place<'tcx> {
@@ -836,6 +839,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
836839
{
837840
self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
838841
}
842+
let local_info = self.local_decls[local_id].local_info.as_mut().unwrap_crate_local();
843+
if let LocalInfo::User(BindingForm::Var(var_info)) = &mut **local_info {
844+
var_info.introductions.push((span, is_shorthand));
845+
}
839846
Place::from(local_id)
840847
}
841848

@@ -1232,6 +1239,7 @@ struct Binding<'tcx> {
12321239
source: Place<'tcx>,
12331240
var_id: LocalVarId,
12341241
binding_mode: BindingMode,
1242+
is_shorthand: bool,
12351243
}
12361244

12371245
/// Indicates that the type of `source` must be a subtype of the
@@ -2694,6 +2702,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
26942702
block,
26952703
binding.var_id,
26962704
binding.span,
2705+
binding.is_shorthand,
26972706
RefWithinGuard,
26982707
schedule_drops,
26992708
);
@@ -2710,6 +2719,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
27102719
block,
27112720
binding.var_id,
27122721
binding.span,
2722+
binding.is_shorthand,
27132723
OutsideGuard,
27142724
schedule_drops,
27152725
);
@@ -2749,6 +2759,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
27492759
block,
27502760
binding.var_id,
27512761
binding.span,
2762+
binding.is_shorthand,
27522763
OutsideGuard,
27532764
schedule_drops,
27542765
),
@@ -2802,6 +2813,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
28022813
opt_ty_info: None,
28032814
opt_match_place,
28042815
pat_span,
2816+
introductions: Vec::new(),
28052817
},
28062818
)))),
28072819
};

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10451045
opt_ty_info: param.ty_span,
10461046
opt_match_place: Some((None, span)),
10471047
pat_span: span,
1048+
introductions: vec![(span, false)],
10481049
}))
10491050
};
10501051
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)