@@ -27,9 +27,12 @@ use syntax::ast::{self, Name};
2727use syntax:: symbol:: InternedString ;
2828use syntax_pos:: { Span , DUMMY_SP } ;
2929use 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 } ;
3231use ty:: layout:: VariantIdx ;
32+ use ty:: {
33+ self , AdtDef , CanonicalUserTypeAnnotations , ClosureSubsts , GeneratorSubsts , Region , Ty , TyCtxt ,
34+ UserTypeAnnotationIndex , UserTypeAnnotation ,
35+ } ;
3336use util:: ppaux;
3437
3538pub use mir:: interpret:: AssertMessage ;
@@ -121,6 +124,9 @@ pub struct Mir<'tcx> {
121124 /// variables and temporaries.
122125 pub local_decls : LocalDecls < ' tcx > ,
123126
127+ /// User type annotations
128+ pub user_type_annotations : CanonicalUserTypeAnnotations < ' tcx > ,
129+
124130 /// Number of arguments this function takes.
125131 ///
126132 /// Starting at local 1, `arg_count` locals will be provided by the caller
@@ -161,7 +167,8 @@ impl<'tcx> Mir<'tcx> {
161167 source_scope_local_data : ClearCrossCrate < IndexVec < SourceScope , SourceScopeLocalData > > ,
162168 promoted : IndexVec < Promoted , Mir < ' tcx > > ,
163169 yield_ty : Option < Ty < ' tcx > > ,
164- local_decls : IndexVec < Local , LocalDecl < ' tcx > > ,
170+ local_decls : LocalDecls < ' tcx > ,
171+ user_type_annotations : CanonicalUserTypeAnnotations < ' tcx > ,
165172 arg_count : usize ,
166173 upvar_decls : Vec < UpvarDecl > ,
167174 span : Span ,
@@ -185,6 +192,7 @@ impl<'tcx> Mir<'tcx> {
185192 generator_drop : None ,
186193 generator_layout : None ,
187194 local_decls,
195+ user_type_annotations,
188196 arg_count,
189197 upvar_decls,
190198 spread_arg : None ,
@@ -418,6 +426,7 @@ impl_stable_hash_for!(struct Mir<'tcx> {
418426 generator_drop,
419427 generator_layout,
420428 local_decls,
429+ user_type_annotations,
421430 arg_count,
422431 upvar_decls,
423432 spread_arg,
@@ -2232,7 +2241,7 @@ pub enum AggregateKind<'tcx> {
22322241 & ' tcx AdtDef ,
22332242 VariantIdx ,
22342243 & ' tcx Substs < ' tcx > ,
2235- Option < UserTypeAnnotation < ' tcx > > ,
2244+ Option < UserTypeAnnotationIndex > ,
22362245 Option < usize > ,
22372246 ) ,
22382247
@@ -2446,38 +2455,11 @@ pub struct Constant<'tcx> {
24462455 /// indicate that `Vec<_>` was explicitly specified.
24472456 ///
24482457 /// Needed for NLL to impose user-given type constraints.
2449- pub user_ty : Option < UserTypeAnnotation < ' tcx > > ,
2458+ pub user_ty : Option < UserTypeAnnotationIndex > ,
24502459
24512460 pub literal : & ' tcx ty:: Const < ' tcx > ,
24522461}
24532462
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-
24812463/// A collection of projections into user types.
24822464///
24832465/// They are projections because a binding can occur a part of a
@@ -2537,6 +2519,48 @@ impl<'tcx> UserTypeProjections<'tcx> {
25372519 pub fn projections ( & self ) -> impl Iterator < Item =& UserTypeProjection < ' tcx > > {
25382520 self . contents . iter ( ) . map ( |& ( ref user_type, _span) | user_type)
25392521 }
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+ }
25402564}
25412565
25422566/// Encodes the effect of a user-supplied type annotation on the
@@ -2556,12 +2580,45 @@ impl<'tcx> UserTypeProjections<'tcx> {
25562580/// determined by finding the type of the `.0` field from `T`.
25572581#[ derive( Clone , Debug , PartialEq , Eq , Hash , RustcEncodable , RustcDecodable ) ]
25582582pub struct UserTypeProjection < ' tcx > {
2559- pub base : UserTypeAnnotation < ' tcx > ,
2583+ pub base : UserTypeAnnotationIndex ,
25602584 pub projs : Vec < ProjectionElem < ' tcx , ( ) , ( ) > > ,
25612585}
25622586
25632587impl < ' tcx > Copy for ProjectionKind < ' tcx > { }
25642588
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+
25652622CloneTypeFoldableAndLiftImpls ! { ProjectionKind <' tcx>, }
25662623
25672624impl < ' tcx > TypeFoldable < ' tcx > for UserTypeProjection < ' tcx > {
@@ -2970,6 +3027,7 @@ CloneTypeFoldableAndLiftImpls! {
29703027 SourceScope ,
29713028 SourceScopeData ,
29723029 SourceScopeLocalData ,
3030+ UserTypeAnnotationIndex ,
29733031}
29743032
29753033BraceStructTypeFoldableImpl ! {
@@ -2983,6 +3041,7 @@ BraceStructTypeFoldableImpl! {
29833041 generator_drop,
29843042 generator_layout,
29853043 local_decls,
3044+ user_type_annotations,
29863045 arg_count,
29873046 upvar_decls,
29883047 spread_arg,
0 commit comments