6
6
use hir:: def_id:: { DefId , LocalDefId } ;
7
7
use rustc_hir as hir;
8
8
use rustc_hir:: def:: DefKind ;
9
- use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
9
+ use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeVisitableExt } ;
10
10
use rustc_middle:: ty:: { GenericArgKind , GenericArgsRef } ;
11
11
12
12
use super :: terms:: VarianceTerm :: * ;
@@ -78,6 +78,12 @@ pub fn add_constraints_from_crate<'a, 'tcx>(
78
78
}
79
79
}
80
80
DefKind :: Fn | DefKind :: AssocFn => constraint_cx. build_constraints_for_item ( def_id) ,
81
+ DefKind :: TyAlias
82
+ if tcx. features ( ) . lazy_type_alias
83
+ || tcx. type_of ( def_id) . instantiate_identity ( ) . has_opaque_types ( ) =>
84
+ {
85
+ constraint_cx. build_constraints_for_item ( def_id)
86
+ }
81
87
_ => { }
82
88
}
83
89
}
@@ -101,7 +107,18 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
101
107
102
108
let inferred_start = self . terms_cx . inferred_starts [ & def_id] ;
103
109
let current_item = & CurrentItem { inferred_start } ;
104
- match tcx. type_of ( def_id) . instantiate_identity ( ) . kind ( ) {
110
+ let ty = tcx. type_of ( def_id) . instantiate_identity ( ) ;
111
+
112
+ // The type as returned by `type_of` is the underlying type and generally not a weak projection.
113
+ // Therefore we need to check the `DefKind` first.
114
+ if let DefKind :: TyAlias = tcx. def_kind ( def_id)
115
+ && ( tcx. features ( ) . lazy_type_alias || ty. has_opaque_types ( ) )
116
+ {
117
+ self . add_constraints_from_ty ( current_item, ty, self . covariant ) ;
118
+ return ;
119
+ }
120
+
121
+ match ty. kind ( ) {
105
122
ty:: Adt ( def, _) => {
106
123
// Not entirely obvious: constraints on structs/enums do not
107
124
// affect the variance of their type parameters. See discussion
@@ -127,6 +144,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
127
144
}
128
145
129
146
ty:: Error ( _) => { }
147
+
130
148
_ => {
131
149
span_bug ! (
132
150
tcx. def_span( def_id) ,
@@ -252,10 +270,14 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
252
270
self . add_constraints_from_args ( current, def. did ( ) , args, variance) ;
253
271
}
254
272
255
- ty:: Alias ( _ , ref data) => {
273
+ ty:: Alias ( ty :: Projection | ty :: Inherent | ty :: Opaque , ref data) => {
256
274
self . add_constraints_from_invariant_args ( current, data. args , variance) ;
257
275
}
258
276
277
+ ty:: Alias ( ty:: Weak , ref data) => {
278
+ self . add_constraints_from_args ( current, data. def_id , data. args , variance) ;
279
+ }
280
+
259
281
ty:: Dynamic ( data, r, _) => {
260
282
// The type `dyn Trait<T> +'a` is covariant w/r/t `'a`:
261
283
self . add_constraints_from_region ( current, r, variance) ;
0 commit comments