Skip to content

Commit 36d8432

Browse files
committed
Refactoring: added PatternTypeAnnotation wrapper around UserTypeAnnotation to ease future changes.
1 parent 54681b0 commit 36d8432

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

src/librustc_mir/build/matches/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
291291
},
292292
..
293293
},
294-
user_ty: ascription_user_ty,
294+
user_ty: pat_ascription_ty,
295295
user_ty_span,
296296
} => {
297297
let place =
@@ -316,7 +316,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
316316
kind: StatementKind::AscribeUserType(
317317
place,
318318
ty::Variance::Invariant,
319-
box ascription_user_ty,
319+
box pat_ascription_ty.user_ty(),
320320
),
321321
},
322322
);
@@ -491,7 +491,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
491491
pub(super) fn visit_bindings(
492492
&mut self,
493493
pattern: &Pattern<'tcx>,
494-
mut pattern_user_ty: Option<(UserTypeAnnotation<'tcx>, Span)>,
494+
mut pattern_user_ty: Option<(PatternTypeAnnotation<'tcx>, Span)>,
495495
f: &mut impl FnMut(
496496
&mut Self,
497497
Mutability,
@@ -500,7 +500,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
500500
NodeId,
501501
Span,
502502
Ty<'tcx>,
503-
Option<(UserTypeAnnotation<'tcx>, Span)>,
503+
Option<(PatternTypeAnnotation<'tcx>, Span)>,
504504
),
505505
) {
506506
match *pattern.kind {
@@ -626,7 +626,7 @@ struct Binding<'tcx> {
626626
struct Ascription<'tcx> {
627627
span: Span,
628628
source: Place<'tcx>,
629-
user_ty: UserTypeAnnotation<'tcx>,
629+
user_ty: PatternTypeAnnotation<'tcx>,
630630
}
631631

632632
#[derive(Clone, Debug)]
@@ -1323,7 +1323,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
13231323
kind: StatementKind::AscribeUserType(
13241324
ascription.source.clone(),
13251325
ty::Variance::Covariant,
1326-
box ascription.user_ty,
1326+
box ascription.user_ty.user_ty(),
13271327
),
13281328
},
13291329
);
@@ -1470,7 +1470,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14701470
num_patterns: usize,
14711471
var_id: NodeId,
14721472
var_ty: Ty<'tcx>,
1473-
user_var_ty: Option<(UserTypeAnnotation<'tcx>, Span)>,
1473+
user_var_ty: Option<(PatternTypeAnnotation<'tcx>, Span)>,
14741474
has_guard: ArmHasGuard,
14751475
opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
14761476
pat_span: Span,
@@ -1489,7 +1489,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14891489
let local = LocalDecl::<'tcx> {
14901490
mutability,
14911491
ty: var_ty,
1492-
user_ty: user_var_ty,
1492+
user_ty: user_var_ty.map(|(pat_ty, span)|(pat_ty.user_ty(), span)),
14931493
name: Some(name),
14941494
source_info,
14951495
visibility_scope,

src/librustc_mir/hair/cx/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
9191
ty: pattern.ty,
9292
span: pattern.span,
9393
kind: Box::new(PatternKind::AscribeUserType {
94-
user_ty: UserTypeAnnotation::Ty(user_ty),
94+
user_ty: PatternTypeAnnotation::from_c_ty(user_ty),
9595
user_ty_span: ty.span,
9696
subpattern: pattern
9797
})

src/librustc_mir/hair/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use self::cx::Cx;
2727
pub mod cx;
2828

2929
pub mod pattern;
30-
pub use self::pattern::{BindingMode, Pattern, PatternKind, FieldPattern};
30+
pub use self::pattern::{BindingMode, Pattern, PatternKind, PatternTypeAnnotation, FieldPattern};
3131

3232
mod util;
3333

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,26 @@ pub struct Pattern<'tcx> {
6464
pub kind: Box<PatternKind<'tcx>>,
6565
}
6666

67+
#[derive(Copy, Clone, Debug)]
68+
pub struct PatternTypeAnnotation<'tcx>(UserTypeAnnotation<'tcx>);
69+
70+
impl<'tcx> PatternTypeAnnotation<'tcx> {
71+
pub(crate) fn from_c_ty(c_ty: ty::CanonicalTy<'tcx>) -> Self {
72+
Self::from_u_ty(UserTypeAnnotation::Ty(c_ty))
73+
}
74+
pub(crate) fn from_u_ty(u_ty: UserTypeAnnotation<'tcx>) -> Self {
75+
PatternTypeAnnotation(u_ty)
76+
}
77+
78+
pub(crate) fn user_ty(self) -> UserTypeAnnotation<'tcx> { self.0 }
79+
}
80+
6781
#[derive(Clone, Debug)]
6882
pub enum PatternKind<'tcx> {
6983
Wild,
7084

7185
AscribeUserType {
72-
user_ty: UserTypeAnnotation<'tcx>,
86+
user_ty: PatternTypeAnnotation<'tcx>,
7387
subpattern: Pattern<'tcx>,
7488
user_ty_span: Span,
7589
},
@@ -690,9 +704,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
690704

691705
debug!("pattern user_ty = {:?} for pattern at {:?}", user_ty, span);
692706

707+
let pat_ty = PatternTypeAnnotation::from_u_ty(user_ty);
693708
kind = PatternKind::AscribeUserType {
694709
subpattern,
695-
user_ty,
710+
user_ty: pat_ty,
696711
user_ty_span: span,
697712
};
698713
}
@@ -980,7 +995,7 @@ macro_rules! CloneImpls {
980995
CloneImpls!{ <'tcx>
981996
Span, Field, Mutability, ast::Name, ast::NodeId, usize, &'tcx ty::Const<'tcx>,
982997
Region<'tcx>, Ty<'tcx>, BindingMode<'tcx>, &'tcx AdtDef,
983-
&'tcx Substs<'tcx>, &'tcx Kind<'tcx>, UserTypeAnnotation<'tcx>
998+
&'tcx Substs<'tcx>, &'tcx Kind<'tcx>, UserTypeAnnotation<'tcx>, PatternTypeAnnotation<'tcx>
984999
}
9851000

9861001
impl<'tcx> PatternFoldable<'tcx> for FieldPattern<'tcx> {

0 commit comments

Comments
 (0)