Skip to content

Commit a385a43

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

File tree

12 files changed

+85
-31
lines changed

12 files changed

+85
-31
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
105105
// whether or not the right-hand side is a place expression
106106
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
107107
opt_match_place: Some((opt_match_place, match_span)),
108-
binding_mode: _,
109-
opt_ty_info: _,
110-
pat_span: _,
108+
..
111109
})) = *local_decl.local_info()
112110
{
113111
let stmt_source_info = self.body.source_info(location);

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 2 deletions
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: ty::BindingMode::BindByValue(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");

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,8 @@ pub struct VarBindingForm<'tcx> {
694694
pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
695695
/// The span of the pattern in which this variable was bound.
696696
pub pat_span: Span,
697+
/// For each introduction place, record here the span and whether this was a shorthand pattern.
698+
pub introductions: Vec<(Span, /* is_shorthand */ bool)>,
697699
}
698700

699701
#[derive(Clone, Debug, TyEncodable, TyDecodable)]
@@ -921,9 +923,7 @@ impl<'tcx> LocalDecl<'tcx> {
921923
LocalInfo::User(
922924
BindingForm::Var(VarBindingForm {
923925
binding_mode: ty::BindingMode::BindByValue(_),
924-
opt_ty_info: _,
925-
opt_match_place: _,
926-
pat_span: _,
926+
..
927927
}) | BindingForm::ImplicitSelf(ImplicitSelfKind::Imm),
928928
)
929929
)
@@ -938,9 +938,7 @@ impl<'tcx> LocalDecl<'tcx> {
938938
LocalInfo::User(
939939
BindingForm::Var(VarBindingForm {
940940
binding_mode: ty::BindingMode::BindByValue(_),
941-
opt_ty_info: _,
942-
opt_match_place: _,
943-
pat_span: _,
941+
..
944942
}) | BindingForm::ImplicitSelf(_),
945943
)
946944
)

compiler/rustc_middle/src/thir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ pub enum PatKind<'tcx> {
707707
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
708708
/// `HirId` of this pattern?
709709
is_primary: bool,
710+
is_shorthand: bool,
710711
},
711712

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

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'
224224
var: _,
225225
ty: _,
226226
is_primary: _,
227+
is_shorthand: _,
227228
name: _,
228229
} => visitor.visit_pat(&subpattern),
229230
Binding { .. } | Wild => {}

compiler/rustc_mir_build/src/build/block.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
242242
block,
243243
node,
244244
span,
245+
false,
245246
OutsideGuard,
246247
true,
247248
);
@@ -336,7 +337,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
336337
pattern,
337338
UserTypeProjections::none(),
338339
&mut |this, _, _, _, node, span, _, _| {
339-
this.storage_live_binding(block, node, span, OutsideGuard, true);
340+
this.storage_live_binding(
341+
block,
342+
node,
343+
span,
344+
false,
345+
OutsideGuard,
346+
true,
347+
);
340348
this.schedule_drop_for_binding(node, span, OutsideGuard);
341349
},
342350
)

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
482482
match irrefutable_pat.kind {
483483
// Optimize the case of `let x = ...` to write directly into `x`
484484
PatKind::Binding { mode: BindingMode::ByValue, var, subpattern: None, .. } => {
485-
let place =
486-
self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
485+
let place = self.storage_live_binding(
486+
block,
487+
var,
488+
irrefutable_pat.span,
489+
false,
490+
OutsideGuard,
491+
true,
492+
);
487493
unpack!(block = self.expr_into_dest(place, block, initializer));
488494

489495
// Inject a fake read, see comments on `FakeReadCause::ForLet`.
@@ -513,8 +519,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
513519
},
514520
ascription: thir::Ascription { ref annotation, variance: _ },
515521
} => {
516-
let place =
517-
self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
522+
let place = self.storage_live_binding(
523+
block,
524+
var,
525+
irrefutable_pat.span,
526+
false,
527+
OutsideGuard,
528+
true,
529+
);
518530
unpack!(block = self.expr_into_dest(place, block, initializer));
519531

520532
// Inject a fake read, see comments on `FakeReadCause::ForLet`.
@@ -682,6 +694,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
682694
block: BasicBlock,
683695
var: LocalVarId,
684696
span: Span,
697+
is_shorthand: bool,
685698
for_guard: ForGuard,
686699
schedule_drop: bool,
687700
) -> Place<'tcx> {
@@ -693,6 +706,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
693706
if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) && schedule_drop {
694707
self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
695708
}
709+
let local_info = self.local_decls[local_id].local_info.as_mut().assert_crate_local();
710+
if let LocalInfo::User(BindingForm::Var(var_info)) = &mut **local_info {
711+
var_info.introductions.push((span, is_shorthand));
712+
}
696713
Place::from(local_id)
697714
}
698715

@@ -914,6 +931,7 @@ struct Binding<'tcx> {
914931
source: Place<'tcx>,
915932
var_id: LocalVarId,
916933
binding_mode: BindingMode,
934+
is_shorthand: bool,
917935
}
918936

919937
/// Indicates that the type of `source` must be a subtype of the
@@ -2124,6 +2142,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
21242142
block,
21252143
binding.var_id,
21262144
binding.span,
2145+
binding.is_shorthand,
21272146
RefWithinGuard,
21282147
schedule_drops,
21292148
);
@@ -2137,6 +2156,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
21372156
block,
21382157
binding.var_id,
21392158
binding.span,
2159+
binding.is_shorthand,
21402160
OutsideGuard,
21412161
schedule_drops,
21422162
);
@@ -2175,6 +2195,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
21752195
block,
21762196
binding.var_id,
21772197
binding.span,
2198+
binding.is_shorthand,
21782199
OutsideGuard,
21792200
schedule_drops,
21802201
)
@@ -2234,6 +2255,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
22342255
opt_ty_info: None,
22352256
opt_match_place,
22362257
pat_span,
2258+
introductions: Vec::new(),
22372259
},
22382260
)))),
22392261
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
181181
ty: _,
182182
ref subpattern,
183183
is_primary: _,
184+
is_shorthand,
184185
} => {
185186
if let Some(source) = match_pair.place.try_to_place(self) {
186187
candidate.bindings.push(Binding {
187188
span: match_pair.pattern.span,
188189
source,
189190
var_id: var,
190191
binding_mode: mode,
192+
is_shorthand,
191193
});
192194
}
193195

compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
897897
opt_ty_info: param.ty_span,
898898
opt_match_place: Some((None, span)),
899899
pat_span: span,
900+
introductions: vec![(span, false)],
900901
}))
901902
};
902903
self.var_indices.insert(var, LocalsForNode::One(local));

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
313313
ty: var_ty,
314314
subpattern: self.lower_opt_pattern(sub),
315315
is_primary: id == pat.hir_id,
316+
is_shorthand: false,
316317
}
317318
}
318319

@@ -330,9 +331,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
330331
let res = self.typeck_results.qpath_res(qpath, pat.hir_id);
331332
let subpatterns = fields
332333
.iter()
333-
.map(|field| FieldPat {
334-
field: self.typeck_results.field_index(field.hir_id),
335-
pattern: self.lower_pattern(&field.pat),
334+
.map(|field| {
335+
let mut pattern = self.lower_pattern(&field.pat);
336+
if let PatKind::Binding { ref mut is_shorthand, .. } = pattern.kind {
337+
*is_shorthand = field.is_shorthand;
338+
}
339+
let field = self.typeck_results.field_index(field.hir_id);
340+
FieldPat { field, pattern }
336341
})
337342
.collect();
338343

@@ -793,17 +798,25 @@ impl<'tcx> PatternFoldable<'tcx> for PatKind<'tcx> {
793798
subpattern: subpattern.fold_with(folder),
794799
ascription: Ascription { annotation: annotation.fold_with(folder), variance },
795800
},
796-
PatKind::Binding { mutability, name, mode, var, ty, ref subpattern, is_primary } => {
797-
PatKind::Binding {
798-
mutability: mutability.fold_with(folder),
799-
name: name.fold_with(folder),
800-
mode: mode.fold_with(folder),
801-
var: var.fold_with(folder),
802-
ty: ty.fold_with(folder),
803-
subpattern: subpattern.fold_with(folder),
804-
is_primary,
805-
}
806-
}
801+
PatKind::Binding {
802+
mutability,
803+
name,
804+
mode,
805+
var,
806+
ty,
807+
ref subpattern,
808+
is_primary,
809+
is_shorthand,
810+
} => PatKind::Binding {
811+
mutability: mutability.fold_with(folder),
812+
name: name.fold_with(folder),
813+
mode: mode.fold_with(folder),
814+
var: var.fold_with(folder),
815+
ty: ty.fold_with(folder),
816+
subpattern: subpattern.fold_with(folder),
817+
is_primary,
818+
is_shorthand,
819+
},
807820
PatKind::Variant { adt_def, substs, variant_index, ref subpatterns } => {
808821
PatKind::Variant {
809822
adt_def: adt_def.fold_with(folder),

0 commit comments

Comments
 (0)