66use hir:: def_id:: { DefId , LocalDefId } ;
77use rustc_hir as hir;
88use rustc_hir:: def:: DefKind ;
9- use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
9+ use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeVisitableExt } ;
1010use rustc_middle:: ty:: { GenericArgKind , GenericArgsRef } ;
1111
1212use super :: terms:: VarianceTerm :: * ;
@@ -78,6 +78,12 @@ pub fn add_constraints_from_crate<'a, 'tcx>(
7878 }
7979 }
8080 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+ }
8187 _ => { }
8288 }
8389 }
@@ -101,7 +107,18 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
101107
102108 let inferred_start = self . terms_cx . inferred_starts [ & def_id] ;
103109 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 ( ) {
105122 ty:: Adt ( def, _) => {
106123 // Not entirely obvious: constraints on structs/enums do not
107124 // affect the variance of their type parameters. See discussion
@@ -127,6 +144,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
127144 }
128145
129146 ty:: Error ( _) => { }
147+
130148 _ => {
131149 span_bug ! (
132150 tcx. def_span( def_id) ,
@@ -252,10 +270,14 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
252270 self . add_constraints_from_args ( current, def. did ( ) , args, variance) ;
253271 }
254272
255- ty:: Alias ( _ , ref data) => {
273+ ty:: Alias ( ty :: Projection | ty :: Inherent | ty :: Opaque , ref data) => {
256274 self . add_constraints_from_invariant_args ( current, data. args , variance) ;
257275 }
258276
277+ ty:: Alias ( ty:: Weak , ref data) => {
278+ self . add_constraints_from_args ( current, data. def_id , data. args , variance) ;
279+ }
280+
259281 ty:: Dynamic ( data, r, _) => {
260282 // The type `dyn Trait<T> +'a` is covariant w/r/t `'a`:
261283 self . add_constraints_from_region ( current, r, variance) ;
0 commit comments