Skip to content

Commit dffa3a0

Browse files
committed
Record for each MIR local where it has been introduced.
1 parent f2e348c commit dffa3a0

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");
@@ -738,6 +737,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
738737
opt_ty_info: _,
739738
opt_match_place: _,
740739
pat_span,
740+
introductions: _,
741741
})) => pat_span,
742742
_ => local_decl.source_info.span,
743743
};

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
@@ -777,6 +777,7 @@ pub enum PatKind<'tcx> {
777777
/// (The same binding can occur multiple times in different branches of
778778
/// an or-pattern, but only one of them will be primary.)
779779
is_primary: bool,
780+
is_shorthand: bool,
780781
},
781782

782783
/// `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
@@ -600,6 +600,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
600600
block,
601601
var,
602602
irrefutable_pat.span,
603+
false,
603604
OutsideGuard,
604605
ScheduleDrops::Yes,
605606
);
@@ -629,6 +630,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
629630
block,
630631
var,
631632
irrefutable_pat.span,
633+
false,
632634
OutsideGuard,
633635
ScheduleDrops::Yes,
634636
);
@@ -821,6 +823,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
821823
block: BasicBlock,
822824
var: LocalVarId,
823825
span: Span,
826+
is_shorthand: bool,
824827
for_guard: ForGuard,
825828
schedule_drop: ScheduleDrops,
826829
) -> Place<'tcx> {
@@ -834,6 +837,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
834837
{
835838
self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
836839
}
840+
let local_info = self.local_decls[local_id].local_info.as_mut().unwrap_crate_local();
841+
if let LocalInfo::User(BindingForm::Var(var_info)) = &mut **local_info {
842+
var_info.introductions.push((span, is_shorthand));
843+
}
837844
Place::from(local_id)
838845
}
839846

@@ -1230,6 +1237,7 @@ struct Binding<'tcx> {
12301237
source: Place<'tcx>,
12311238
var_id: LocalVarId,
12321239
binding_mode: BindingMode,
1240+
is_shorthand: bool,
12331241
}
12341242

12351243
/// Indicates that the type of `source` must be a subtype of the
@@ -2693,6 +2701,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
26932701
block,
26942702
binding.var_id,
26952703
binding.span,
2704+
binding.is_shorthand,
26962705
RefWithinGuard,
26972706
schedule_drops,
26982707
);
@@ -2709,6 +2718,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
27092718
block,
27102719
binding.var_id,
27112720
binding.span,
2721+
binding.is_shorthand,
27122722
OutsideGuard,
27132723
schedule_drops,
27142724
);
@@ -2748,6 +2758,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
27482758
block,
27492759
binding.var_id,
27502760
binding.span,
2761+
binding.is_shorthand,
27512762
OutsideGuard,
27522763
schedule_drops,
27532764
),
@@ -2801,6 +2812,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
28012812
opt_ty_info: None,
28022813
opt_match_place,
28032814
pat_span,
2815+
introductions: Vec::new(),
28042816
},
28052817
)))),
28062818
};

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10421042
opt_ty_info: param.ty_span,
10431043
opt_match_place: Some((None, span)),
10441044
pat_span: span,
1045+
introductions: vec![(span, false)],
10451046
}))
10461047
};
10471048
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
@@ -368,6 +368,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
368368
ty: var_ty,
369369
subpattern: self.lower_opt_pattern(sub),
370370
is_primary: id == pat.hir_id,
371+
is_shorthand: false,
371372
}
372373
}
373374

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

compiler/rustc_mir_build/src/thir/print.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,14 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
677677
self.print_pat(subpattern, depth_lvl + 3);
678678
print_indented!(self, "}", depth_lvl + 1);
679679
}
680-
PatKind::Binding { name, mode, var, ty, subpattern, is_primary } => {
680+
PatKind::Binding { name, mode, var, ty, subpattern, is_primary, is_shorthand } => {
681681
print_indented!(self, "Binding {", depth_lvl + 1);
682682
print_indented!(self, format!("name: {:?}", name), depth_lvl + 2);
683683
print_indented!(self, format!("mode: {:?}", mode), depth_lvl + 2);
684684
print_indented!(self, format!("var: {:?}", var), depth_lvl + 2);
685685
print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 2);
686686
print_indented!(self, format!("is_primary: {:?}", is_primary), depth_lvl + 2);
687+
print_indented!(self, format!("is_shorthand: {:?}", is_shorthand), depth_lvl + 2);
687688

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

tests/ui/thir-print/thir-tree-match.stdout

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ params: [
1616
var: LocalVarId(HirId(DefId(0:16 ~ thir_tree_match[fcf8]::has_match).2))
1717
ty: Foo
1818
is_primary: true
19+
is_shorthand: false
1920
subpattern: None
2021
}
2122
}

0 commit comments

Comments
 (0)