@@ -27,9 +27,12 @@ use syntax::ast::{self, Name};
27
27
use syntax:: symbol:: InternedString ;
28
28
use syntax_pos:: { Span , DUMMY_SP } ;
29
29
use ty:: fold:: { TypeFoldable , TypeFolder , TypeVisitor } ;
30
- use ty:: subst:: { CanonicalUserSubsts , Subst , Substs } ;
31
- use ty:: { self , AdtDef , CanonicalTy , ClosureSubsts , GeneratorSubsts , Region , Ty , TyCtxt } ;
30
+ use ty:: subst:: { Subst , Substs } ;
32
31
use ty:: layout:: VariantIdx ;
32
+ use ty:: {
33
+ self , AdtDef , CanonicalUserTypeAnnotations , ClosureSubsts , GeneratorSubsts , Region , Ty , TyCtxt ,
34
+ UserTypeAnnotationIndex , UserTypeAnnotation ,
35
+ } ;
33
36
use util:: ppaux;
34
37
35
38
pub use mir:: interpret:: AssertMessage ;
@@ -121,6 +124,9 @@ pub struct Mir<'tcx> {
121
124
/// variables and temporaries.
122
125
pub local_decls : LocalDecls < ' tcx > ,
123
126
127
+ /// User type annotations
128
+ pub user_type_annotations : CanonicalUserTypeAnnotations < ' tcx > ,
129
+
124
130
/// Number of arguments this function takes.
125
131
///
126
132
/// Starting at local 1, `arg_count` locals will be provided by the caller
@@ -161,7 +167,8 @@ impl<'tcx> Mir<'tcx> {
161
167
source_scope_local_data : ClearCrossCrate < IndexVec < SourceScope , SourceScopeLocalData > > ,
162
168
promoted : IndexVec < Promoted , Mir < ' tcx > > ,
163
169
yield_ty : Option < Ty < ' tcx > > ,
164
- local_decls : IndexVec < Local , LocalDecl < ' tcx > > ,
170
+ local_decls : LocalDecls < ' tcx > ,
171
+ user_type_annotations : CanonicalUserTypeAnnotations < ' tcx > ,
165
172
arg_count : usize ,
166
173
upvar_decls : Vec < UpvarDecl > ,
167
174
span : Span ,
@@ -185,6 +192,7 @@ impl<'tcx> Mir<'tcx> {
185
192
generator_drop : None ,
186
193
generator_layout : None ,
187
194
local_decls,
195
+ user_type_annotations,
188
196
arg_count,
189
197
upvar_decls,
190
198
spread_arg : None ,
@@ -418,6 +426,7 @@ impl_stable_hash_for!(struct Mir<'tcx> {
418
426
generator_drop,
419
427
generator_layout,
420
428
local_decls,
429
+ user_type_annotations,
421
430
arg_count,
422
431
upvar_decls,
423
432
spread_arg,
@@ -2232,7 +2241,7 @@ pub enum AggregateKind<'tcx> {
2232
2241
& ' tcx AdtDef ,
2233
2242
VariantIdx ,
2234
2243
& ' tcx Substs < ' tcx > ,
2235
- Option < UserTypeAnnotation < ' tcx > > ,
2244
+ Option < UserTypeAnnotationIndex > ,
2236
2245
Option < usize > ,
2237
2246
) ,
2238
2247
@@ -2446,38 +2455,11 @@ pub struct Constant<'tcx> {
2446
2455
/// indicate that `Vec<_>` was explicitly specified.
2447
2456
///
2448
2457
/// Needed for NLL to impose user-given type constraints.
2449
- pub user_ty : Option < UserTypeAnnotation < ' tcx > > ,
2458
+ pub user_ty : Option < UserTypeAnnotationIndex > ,
2450
2459
2451
2460
pub literal : & ' tcx ty:: Const < ' tcx > ,
2452
2461
}
2453
2462
2454
- /// A user-given type annotation attached to a constant. These arise
2455
- /// from constants that are named via paths, like `Foo::<A>::new` and
2456
- /// so forth.
2457
- #[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , RustcEncodable , RustcDecodable ) ]
2458
- pub enum UserTypeAnnotation < ' tcx > {
2459
- Ty ( CanonicalTy < ' tcx > ) ,
2460
-
2461
- /// The canonical type is the result of `type_of(def_id)` with the
2462
- /// given substitutions applied.
2463
- TypeOf ( DefId , CanonicalUserSubsts < ' tcx > ) ,
2464
- }
2465
-
2466
- EnumTypeFoldableImpl ! {
2467
- impl <' tcx> TypeFoldable <' tcx> for UserTypeAnnotation <' tcx> {
2468
- ( UserTypeAnnotation :: Ty ) ( ty) ,
2469
- ( UserTypeAnnotation :: TypeOf ) ( def, substs) ,
2470
- }
2471
- }
2472
-
2473
- EnumLiftImpl ! {
2474
- impl <' a, ' tcx> Lift <' tcx> for UserTypeAnnotation <' a> {
2475
- type Lifted = UserTypeAnnotation <' tcx>;
2476
- ( UserTypeAnnotation :: Ty ) ( ty) ,
2477
- ( UserTypeAnnotation :: TypeOf ) ( def, substs) ,
2478
- }
2479
- }
2480
-
2481
2463
/// A collection of projections into user types.
2482
2464
///
2483
2465
/// They are projections because a binding can occur a part of a
@@ -2537,6 +2519,48 @@ impl<'tcx> UserTypeProjections<'tcx> {
2537
2519
pub fn projections ( & self ) -> impl Iterator < Item =& UserTypeProjection < ' tcx > > {
2538
2520
self . contents . iter ( ) . map ( |& ( ref user_type, _span) | user_type)
2539
2521
}
2522
+
2523
+ pub fn push_projection (
2524
+ mut self ,
2525
+ user_ty : & UserTypeProjection < ' tcx > ,
2526
+ span : Span ,
2527
+ ) -> Self {
2528
+ self . contents . push ( ( user_ty. clone ( ) , span) ) ;
2529
+ self
2530
+ }
2531
+
2532
+ fn map_projections (
2533
+ mut self ,
2534
+ mut f : impl FnMut ( UserTypeProjection < ' tcx > ) -> UserTypeProjection < ' tcx >
2535
+ ) -> Self {
2536
+ self . contents = self . contents . drain ( ..) . map ( |( proj, span) | ( f ( proj) , span) ) . collect ( ) ;
2537
+ self
2538
+ }
2539
+
2540
+ pub fn index ( self ) -> Self {
2541
+ self . map_projections ( |pat_ty_proj| pat_ty_proj. index ( ) )
2542
+ }
2543
+
2544
+ pub fn subslice ( self , from : u32 , to : u32 ) -> Self {
2545
+ self . map_projections ( |pat_ty_proj| pat_ty_proj. subslice ( from, to) )
2546
+ }
2547
+
2548
+ pub fn deref ( self ) -> Self {
2549
+ self . map_projections ( |pat_ty_proj| pat_ty_proj. deref ( ) )
2550
+ }
2551
+
2552
+ pub fn leaf ( self , field : Field ) -> Self {
2553
+ self . map_projections ( |pat_ty_proj| pat_ty_proj. leaf ( field) )
2554
+ }
2555
+
2556
+ pub fn variant (
2557
+ self ,
2558
+ adt_def : & ' tcx AdtDef ,
2559
+ variant_index : VariantIdx ,
2560
+ field : Field ,
2561
+ ) -> Self {
2562
+ self . map_projections ( |pat_ty_proj| pat_ty_proj. variant ( adt_def, variant_index, field) )
2563
+ }
2540
2564
}
2541
2565
2542
2566
/// Encodes the effect of a user-supplied type annotation on the
@@ -2556,12 +2580,45 @@ impl<'tcx> UserTypeProjections<'tcx> {
2556
2580
/// determined by finding the type of the `.0` field from `T`.
2557
2581
#[ derive( Clone , Debug , PartialEq , Eq , Hash , RustcEncodable , RustcDecodable ) ]
2558
2582
pub struct UserTypeProjection < ' tcx > {
2559
- pub base : UserTypeAnnotation < ' tcx > ,
2583
+ pub base : UserTypeAnnotationIndex ,
2560
2584
pub projs : Vec < ProjectionElem < ' tcx , ( ) , ( ) > > ,
2561
2585
}
2562
2586
2563
2587
impl < ' tcx > Copy for ProjectionKind < ' tcx > { }
2564
2588
2589
+ impl < ' tcx > UserTypeProjection < ' tcx > {
2590
+ pub ( crate ) fn index ( mut self ) -> Self {
2591
+ self . projs . push ( ProjectionElem :: Index ( ( ) ) ) ;
2592
+ self
2593
+ }
2594
+
2595
+ pub ( crate ) fn subslice ( mut self , from : u32 , to : u32 ) -> Self {
2596
+ self . projs . push ( ProjectionElem :: Subslice { from, to } ) ;
2597
+ self
2598
+ }
2599
+
2600
+ pub ( crate ) fn deref ( mut self ) -> Self {
2601
+ self . projs . push ( ProjectionElem :: Deref ) ;
2602
+ self
2603
+ }
2604
+
2605
+ pub ( crate ) fn leaf ( mut self , field : Field ) -> Self {
2606
+ self . projs . push ( ProjectionElem :: Field ( field, ( ) ) ) ;
2607
+ self
2608
+ }
2609
+
2610
+ pub ( crate ) fn variant (
2611
+ mut self ,
2612
+ adt_def : & ' tcx AdtDef ,
2613
+ variant_index : VariantIdx ,
2614
+ field : Field ,
2615
+ ) -> Self {
2616
+ self . projs . push ( ProjectionElem :: Downcast ( adt_def, variant_index) ) ;
2617
+ self . projs . push ( ProjectionElem :: Field ( field, ( ) ) ) ;
2618
+ self
2619
+ }
2620
+ }
2621
+
2565
2622
CloneTypeFoldableAndLiftImpls ! { ProjectionKind <' tcx>, }
2566
2623
2567
2624
impl < ' tcx > TypeFoldable < ' tcx > for UserTypeProjection < ' tcx > {
@@ -2970,6 +3027,7 @@ CloneTypeFoldableAndLiftImpls! {
2970
3027
SourceScope ,
2971
3028
SourceScopeData ,
2972
3029
SourceScopeLocalData ,
3030
+ UserTypeAnnotationIndex ,
2973
3031
}
2974
3032
2975
3033
BraceStructTypeFoldableImpl ! {
@@ -2983,6 +3041,7 @@ BraceStructTypeFoldableImpl! {
2983
3041
generator_drop,
2984
3042
generator_layout,
2985
3043
local_decls,
3044
+ user_type_annotations,
2986
3045
arg_count,
2987
3046
upvar_decls,
2988
3047
spread_arg,
0 commit comments