Skip to content

Commit 641ffac

Browse files
committed
Record for each MIR local where it has been introduced.
1 parent e78913b commit 641ffac

File tree

11 files changed

+57
-21
lines changed

11 files changed

+57
-21
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
134134
// whether or not the right-hand side is a place expression
135135
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
136136
opt_match_place: Some((opt_match_place, match_span)),
137-
binding_mode: _,
138-
opt_ty_info: _,
139-
pat_span: _,
137+
..
140138
})) = *local_decl.local_info()
141139
{
142140
let stmt_source_info = self.body.source_info(location);

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
305305
LocalInfo::User(BindingForm::Var(mir::VarBindingForm {
306306
binding_mode: BindingAnnotation(ByRef::No, Mutability::Not),
307307
opt_ty_info: Some(sp),
308-
opt_match_place: _,
309-
pat_span: _,
308+
..
310309
})) => {
311310
if suggest {
312311
err.span_note(sp, "the binding is already a mutable borrow");
@@ -729,6 +728,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
729728
opt_ty_info: _,
730729
opt_match_place: _,
731730
pat_span,
731+
introductions: _,
732732
})) => pat_span,
733733
_ => local_decl.source_info.span,
734734
};

compiler/rustc_middle/src/mir/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,8 @@ pub struct VarBindingForm<'tcx> {
999999
pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
10001000
/// The span of the pattern in which this variable was bound.
10011001
pub pat_span: Span,
1002+
/// For each introduction place, record here the span and whether this was a shorthand pattern.
1003+
pub introductions: Vec<(Span, /* is_shorthand */ bool)>,
10021004
}
10031005

10041006
#[derive(Clone, Debug, TyEncodable, TyDecodable)]
@@ -1208,9 +1210,7 @@ impl<'tcx> LocalDecl<'tcx> {
12081210
LocalInfo::User(
12091211
BindingForm::Var(VarBindingForm {
12101212
binding_mode: BindingAnnotation(ByRef::No, _),
1211-
opt_ty_info: _,
1212-
opt_match_place: _,
1213-
pat_span: _,
1213+
..
12141214
}) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
12151215
)
12161216
)
@@ -1225,9 +1225,7 @@ impl<'tcx> LocalDecl<'tcx> {
12251225
LocalInfo::User(
12261226
BindingForm::Var(VarBindingForm {
12271227
binding_mode: BindingAnnotation(ByRef::No, _),
1228-
opt_ty_info: _,
1229-
opt_match_place: _,
1230-
pat_span: _,
1228+
..
12311229
}) | BindingForm::ImplicitSelf(_),
12321230
)
12331231
)

compiler/rustc_middle/src/thir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ pub enum PatKind<'tcx> {
738738
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
739739
/// `HirId` of this pattern?
740740
is_primary: bool,
741+
is_shorthand: bool,
741742
},
742743

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

compiler/rustc_mir_build/src/build/block.rs

+9-1
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
true,
209210
);
@@ -290,7 +291,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
290291
pattern,
291292
UserTypeProjections::none(),
292293
&mut |this, _, _, node, span, _, _| {
293-
this.storage_live_binding(block, node, span, OutsideGuard, true);
294+
this.storage_live_binding(
295+
block,
296+
node,
297+
span,
298+
false,
299+
OutsideGuard,
300+
true,
301+
);
294302
this.schedule_drop_for_binding(node, span, OutsideGuard);
295303
},
296304
)

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

+26-4
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
627627
subpattern: None,
628628
..
629629
} => {
630-
let place =
631-
self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
630+
let place = self.storage_live_binding(
631+
block,
632+
var,
633+
irrefutable_pat.span,
634+
false,
635+
OutsideGuard,
636+
true,
637+
);
632638
unpack!(block = self.expr_into_dest(place, block, initializer_id));
633639

634640
// Inject a fake read, see comments on `FakeReadCause::ForLet`.
@@ -661,8 +667,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
661667
},
662668
ascription: thir::Ascription { ref annotation, variance: _ },
663669
} => {
664-
let place =
665-
self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
670+
let place = self.storage_live_binding(
671+
block,
672+
var,
673+
irrefutable_pat.span,
674+
false,
675+
OutsideGuard,
676+
true,
677+
);
666678
unpack!(block = self.expr_into_dest(place, block, initializer_id));
667679

668680
// Inject a fake read, see comments on `FakeReadCause::ForLet`.
@@ -855,6 +867,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
855867
block: BasicBlock,
856868
var: LocalVarId,
857869
span: Span,
870+
is_shorthand: bool,
858871
for_guard: ForGuard,
859872
schedule_drop: bool,
860873
) -> Place<'tcx> {
@@ -868,6 +881,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
868881
{
869882
self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
870883
}
884+
let local_info = self.local_decls[local_id].local_info.as_mut().assert_crate_local();
885+
if let LocalInfo::User(BindingForm::Var(var_info)) = &mut **local_info {
886+
var_info.introductions.push((span, is_shorthand));
887+
}
871888
Place::from(local_id)
872889
}
873890

@@ -1149,6 +1166,7 @@ struct Binding<'tcx> {
11491166
source: Place<'tcx>,
11501167
var_id: LocalVarId,
11511168
binding_mode: BindingAnnotation,
1169+
is_shorthand: bool,
11521170
}
11531171

11541172
/// Indicates that the type of `source` must be a subtype of the
@@ -2332,6 +2350,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
23322350
block,
23332351
binding.var_id,
23342352
binding.span,
2353+
binding.is_shorthand,
23352354
RefWithinGuard,
23362355
schedule_drops,
23372356
);
@@ -2345,6 +2364,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
23452364
block,
23462365
binding.var_id,
23472366
binding.span,
2367+
binding.is_shorthand,
23482368
OutsideGuard,
23492369
schedule_drops,
23502370
);
@@ -2384,6 +2404,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
23842404
block,
23852405
binding.var_id,
23862406
binding.span,
2407+
binding.is_shorthand,
23872408
OutsideGuard,
23882409
schedule_drops,
23892410
)
@@ -2437,6 +2458,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
24372458
opt_ty_info: None,
24382459
opt_match_place,
24392460
pat_span,
2461+
introductions: Vec::new(),
24402462
},
24412463
)))),
24422464
};

compiler/rustc_mir_build/src/build/matches/util.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,13 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
154154
TestCase::Irrefutable { ascription, binding: None }
155155
}
156156

157-
PatKind::Binding { mode, var, ref subpattern, .. } => {
157+
PatKind::Binding { mode, var, ref subpattern, is_shorthand, .. } => {
158158
let binding = place.map(|source| super::Binding {
159159
span: pattern.span,
160160
source,
161161
var_id: var,
162162
binding_mode: mode,
163+
is_shorthand,
163164
});
164165

165166
if let Some(subpattern) = subpattern.as_ref() {

compiler/rustc_mir_build/src/build/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
947947
opt_ty_info: param.ty_span,
948948
opt_match_place: Some((None, span)),
949949
pat_span: span,
950+
introductions: vec![(span, false)],
950951
}))
951952
};
952953
self.var_indices.insert(var, LocalsForNode::One(local));

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
311311
ty: var_ty,
312312
subpattern: self.lower_opt_pattern(sub),
313313
is_primary: id == pat.hir_id,
314+
is_shorthand: false,
314315
}
315316
}
316317

@@ -328,9 +329,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
328329
let res = self.typeck_results.qpath_res(qpath, pat.hir_id);
329330
let subpatterns = fields
330331
.iter()
331-
.map(|field| FieldPat {
332-
field: self.typeck_results.field_index(field.hir_id),
333-
pattern: self.lower_pattern(field.pat),
332+
.map(|field| {
333+
let mut pattern = self.lower_pattern(field.pat);
334+
if let PatKind::Binding { ref mut is_shorthand, .. } = pattern.kind {
335+
*is_shorthand = field.is_shorthand;
336+
}
337+
let field = self.typeck_results.field_index(field.hir_id);
338+
FieldPat { field, pattern }
334339
})
335340
.collect();
336341

compiler/rustc_mir_build/src/thir/print.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -635,13 +635,14 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
635635
self.print_pat(subpattern, depth_lvl + 3);
636636
print_indented!(self, "}", depth_lvl + 1);
637637
}
638-
PatKind::Binding { name, mode, var, ty, subpattern, is_primary } => {
638+
PatKind::Binding { name, mode, var, ty, subpattern, is_primary, is_shorthand } => {
639639
print_indented!(self, "Binding {", depth_lvl + 1);
640640
print_indented!(self, format!("name: {:?}", name), depth_lvl + 2);
641641
print_indented!(self, format!("mode: {:?}", mode), depth_lvl + 2);
642642
print_indented!(self, format!("var: {:?}", var), depth_lvl + 2);
643643
print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 2);
644644
print_indented!(self, format!("is_primary: {:?}", is_primary), depth_lvl + 2);
645+
print_indented!(self, format!("is_shorthand: {:?}", is_shorthand), depth_lvl + 2);
645646

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

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

+1
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)