Skip to content

Commit bd827d7

Browse files
committed
Split visit_primary_bindings into two variants
The existing method does some non-obvious extra work to collect user types and build user-type projections, which is specifically needed by `declare_bindings` and not by the other two callers.
1 parent 8ec773a commit bd827d7

7 files changed

+67
-56
lines changed

compiler/rustc_middle/src/thir.rs

+4
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,12 @@ pub enum PatKind<'tcx> {
783783
var: LocalVarId,
784784
ty: Ty<'tcx>,
785785
subpattern: Option<Box<Pat<'tcx>>>,
786+
786787
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
787788
/// `HirId` of this pattern?
789+
///
790+
/// (The same binding can occur multiple times in different branches of
791+
/// an or-pattern, but only one of them will be primary.)
788792
is_primary: bool,
789793
},
790794

compiler/rustc_mir_build/src/builder/block.rs

+19-27
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
199199
None,
200200
Some((Some(&destination), initializer_span)),
201201
);
202-
this.visit_primary_bindings(
203-
pattern,
204-
UserTypeProjections::none(),
205-
&mut |this, _, _, node, span, _, _| {
206-
this.storage_live_binding(
207-
block,
208-
node,
209-
span,
210-
OutsideGuard,
211-
ScheduleDrops::Yes,
212-
);
213-
},
214-
);
202+
this.visit_primary_bindings(pattern, &mut |this, node, span| {
203+
this.storage_live_binding(
204+
block,
205+
node,
206+
span,
207+
OutsideGuard,
208+
ScheduleDrops::Yes,
209+
);
210+
});
215211
let else_block_span = this.thir[*else_block].span;
216212
let (matching, failure) =
217213
this.in_if_then_scope(last_remainder_scope, else_block_span, |this| {
@@ -295,20 +291,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
295291
});
296292

297293
debug!("ast_block_stmts: pattern={:?}", pattern);
298-
this.visit_primary_bindings(
299-
pattern,
300-
UserTypeProjections::none(),
301-
&mut |this, _, _, node, span, _, _| {
302-
this.storage_live_binding(
303-
block,
304-
node,
305-
span,
306-
OutsideGuard,
307-
ScheduleDrops::Yes,
308-
);
309-
this.schedule_drop_for_binding(node, span, OutsideGuard);
310-
},
311-
)
294+
this.visit_primary_bindings(pattern, &mut |this, node, span| {
295+
this.storage_live_binding(
296+
block,
297+
node,
298+
span,
299+
OutsideGuard,
300+
ScheduleDrops::Yes,
301+
);
302+
this.schedule_drop_for_binding(node, span, OutsideGuard);
303+
})
312304
}
313305

314306
// Enter the visibility scope, after evaluating the initializer.

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

+42-23
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
755755
guard: Option<ExprId>,
756756
opt_match_place: Option<(Option<&Place<'tcx>>, Span)>,
757757
) -> Option<SourceScope> {
758-
self.visit_primary_bindings(
758+
self.visit_primary_bindings_special(
759759
pattern,
760760
UserTypeProjections::none(),
761761
&mut |this, name, mode, var, span, ty, user_ty| {
@@ -846,10 +846,32 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
846846
}
847847
}
848848

849-
/// Visit all of the primary bindings in a patterns, that is, visit the
850-
/// leftmost occurrence of each variable bound in a pattern. A variable
851-
/// will occur more than once in an or-pattern.
849+
/// Visits all of the "primary" bindings in a pattern, i.e. the leftmost
850+
/// occurrence of each variable bound by the pattern.
851+
/// See [`PatKind::Binding::is_primary`] for more context.
852+
///
853+
/// This variant provides only the limited subset of binding data needed
854+
/// by its callers, and should be a "pure" visit without side-effects.
852855
pub(super) fn visit_primary_bindings(
856+
&mut self,
857+
pattern: &Pat<'tcx>,
858+
f: &mut impl FnMut(&mut Self, LocalVarId, Span),
859+
) {
860+
pattern.walk_always(|pat| {
861+
if let PatKind::Binding { var, is_primary: true, .. } = pat.kind {
862+
f(self, var, pat.span);
863+
}
864+
})
865+
}
866+
867+
/// Visits all of the "primary" bindings in a pattern, while preparing
868+
/// additional user-type-annotation data needed by `declare_bindings`.
869+
///
870+
/// This also has the side-effect of pushing all user type annotations
871+
/// onto `canonical_user_type_annotations`, so that they end up in MIR
872+
/// even if they aren't associated with any bindings.
873+
#[instrument(level = "debug", skip(self, f))]
874+
fn visit_primary_bindings_special(
853875
&mut self,
854876
pattern: &Pat<'tcx>,
855877
pattern_user_ty: UserTypeProjections,
@@ -863,17 +885,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
863885
UserTypeProjections,
864886
),
865887
) {
866-
debug!(
867-
"visit_primary_bindings: pattern={:?} pattern_user_ty={:?}",
868-
pattern, pattern_user_ty
869-
);
888+
// Avoid having to write the full method name at each recursive call.
889+
let visit_subpat = |this: &mut Self, subpat, user_tys, f: &mut _| {
890+
this.visit_primary_bindings_special(subpat, user_tys, f)
891+
};
892+
870893
match pattern.kind {
871894
PatKind::Binding { name, mode, var, ty, ref subpattern, is_primary, .. } => {
872895
if is_primary {
873896
f(self, name, mode, var, pattern.span, ty, pattern_user_ty.clone());
874897
}
875898
if let Some(subpattern) = subpattern.as_ref() {
876-
self.visit_primary_bindings(subpattern, pattern_user_ty, f);
899+
visit_subpat(self, subpattern, pattern_user_ty, f);
877900
}
878901
}
879902

@@ -882,17 +905,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
882905
let from = u64::try_from(prefix.len()).unwrap();
883906
let to = u64::try_from(suffix.len()).unwrap();
884907
for subpattern in prefix.iter() {
885-
self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
908+
visit_subpat(self, subpattern, pattern_user_ty.clone().index(), f);
886909
}
887910
if let Some(subpattern) = slice {
888-
self.visit_primary_bindings(
889-
subpattern,
890-
pattern_user_ty.clone().subslice(from, to),
891-
f,
892-
);
911+
visit_subpat(self, subpattern, pattern_user_ty.clone().subslice(from, to), f);
893912
}
894913
for subpattern in suffix.iter() {
895-
self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
914+
visit_subpat(self, subpattern, pattern_user_ty.clone().index(), f);
896915
}
897916
}
898917

@@ -903,11 +922,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
903922
| PatKind::Error(_) => {}
904923

905924
PatKind::Deref { ref subpattern } => {
906-
self.visit_primary_bindings(subpattern, pattern_user_ty.deref(), f);
925+
visit_subpat(self, subpattern, pattern_user_ty.deref(), f);
907926
}
908927

909928
PatKind::DerefPattern { ref subpattern, .. } => {
910-
self.visit_primary_bindings(subpattern, UserTypeProjections::none(), f);
929+
visit_subpat(self, subpattern, UserTypeProjections::none(), f);
911930
}
912931

913932
PatKind::AscribeUserType {
@@ -925,26 +944,26 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
925944

926945
let base_user_ty = self.canonical_user_type_annotations.push(annotation.clone());
927946
let subpattern_user_ty = pattern_user_ty.push_user_type(base_user_ty);
928-
self.visit_primary_bindings(subpattern, subpattern_user_ty, f)
947+
visit_subpat(self, subpattern, subpattern_user_ty, f)
929948
}
930949

931950
PatKind::ExpandedConstant { ref subpattern, .. } => {
932-
self.visit_primary_bindings(subpattern, pattern_user_ty, f)
951+
visit_subpat(self, subpattern, pattern_user_ty, f)
933952
}
934953

935954
PatKind::Leaf { ref subpatterns } => {
936955
for subpattern in subpatterns {
937956
let subpattern_user_ty = pattern_user_ty.clone().leaf(subpattern.field);
938957
debug!("visit_primary_bindings: subpattern_user_ty={:?}", subpattern_user_ty);
939-
self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f);
958+
visit_subpat(self, &subpattern.pattern, subpattern_user_ty, f);
940959
}
941960
}
942961

943962
PatKind::Variant { adt_def, args: _, variant_index, ref subpatterns } => {
944963
for subpattern in subpatterns {
945964
let subpattern_user_ty =
946965
pattern_user_ty.clone().variant(adt_def, variant_index, subpattern.field);
947-
self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f);
966+
visit_subpat(self, &subpattern.pattern, subpattern_user_ty, f);
948967
}
949968
}
950969
PatKind::Or { ref pats } => {
@@ -953,7 +972,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
953972
// `let (x | y) = ...`, the primary binding of `y` occurs in
954973
// the right subpattern
955974
for subpattern in pats.iter() {
956-
self.visit_primary_bindings(subpattern, pattern_user_ty.clone(), f);
975+
visit_subpat(self, subpattern, pattern_user_ty.clone(), f);
957976
}
958977
}
959978
}

tests/mir-opt/building/user_type_annotations.let_else.built.after.mir

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
| User Type Annotations
44
| 0: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:35:20: 35:45, inferred_ty: (u32, u64, &char)
55
| 1: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:35:20: 35:45, inferred_ty: (u32, u64, &char)
6-
| 2: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:35:20: 35:45, inferred_ty: (u32, u64, &char)
76
|
87
fn let_else() -> () {
98
let mut _0: ();
@@ -51,7 +50,7 @@ fn let_else() -> () {
5150
}
5251

5352
bb4: {
54-
AscribeUserType(_5, +, UserTypeProjection { base: UserType(2), projs: [] });
53+
AscribeUserType(_5, +, UserTypeProjection { base: UserType(1), projs: [] });
5554
_2 = copy (_5.0: u32);
5655
_3 = copy (_5.1: u64);
5756
_4 = copy (_5.2: &char);

tests/mir-opt/building/user_type_annotations.let_else_bindless.built.after.mir

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
| User Type Annotations
44
| 0: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:40:20: 40:45, inferred_ty: (u32, u64, &char)
55
| 1: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:40:20: 40:45, inferred_ty: (u32, u64, &char)
6-
| 2: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:40:20: 40:45, inferred_ty: (u32, u64, &char)
76
|
87
fn let_else_bindless() -> () {
98
let mut _0: ();
@@ -42,7 +41,7 @@ fn let_else_bindless() -> () {
4241
}
4342

4443
bb4: {
45-
AscribeUserType(_2, +, UserTypeProjection { base: UserType(2), projs: [] });
44+
AscribeUserType(_2, +, UserTypeProjection { base: UserType(1), projs: [] });
4645
StorageDead(_4);
4746
StorageDead(_2);
4847
_0 = const ();

tests/mir-opt/building/user_type_annotations.let_uninit.built.after.mir

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
| User Type Annotations
44
| 0: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:15:20: 15:45, inferred_ty: (u32, u64, &char)
5-
| 1: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:15:20: 15:45, inferred_ty: (u32, u64, &char)
65
|
76
fn let_uninit() -> () {
87
let mut _0: ();

tests/mir-opt/building/user_type_annotations.let_uninit_bindless.built.after.mir

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
| User Type Annotations
44
| 0: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:20:20: 20:45, inferred_ty: (u32, u64, &char)
5-
| 1: user_ty: Canonical { value: Ty((u32, u64, &'static char)), max_universe: U0, variables: [] }, span: $DIR/user_type_annotations.rs:20:20: 20:45, inferred_ty: (u32, u64, &char)
65
|
76
fn let_uninit_bindless() -> () {
87
let mut _0: ();

0 commit comments

Comments
 (0)