@@ -293,26 +293,24 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
293293 }
294294 hir:: ItemKind :: Fn { ident, sig, .. } => check_item_fn ( tcx, def_id, ident, sig. decl ) ,
295295 hir:: ItemKind :: Const ( _, _, ty, _) => check_const_item ( tcx, def_id, ty. span , item. span ) ,
296- hir:: ItemKind :: Struct ( _ , generics , _ ) => {
296+ hir:: ItemKind :: Struct ( .. ) => {
297297 let res = check_type_defn ( tcx, item, false ) ;
298- check_variances_for_type_defn ( tcx, item , generics ) ;
298+ check_variances_for_type_defn ( tcx, def_id ) ;
299299 res
300300 }
301- hir:: ItemKind :: Union ( _ , generics , _ ) => {
301+ hir:: ItemKind :: Union ( .. ) => {
302302 let res = check_type_defn ( tcx, item, true ) ;
303- check_variances_for_type_defn ( tcx, item , generics ) ;
303+ check_variances_for_type_defn ( tcx, def_id ) ;
304304 res
305305 }
306- hir:: ItemKind :: Enum ( _ , generics , _ ) => {
306+ hir:: ItemKind :: Enum ( .. ) => {
307307 let res = check_type_defn ( tcx, item, true ) ;
308- check_variances_for_type_defn ( tcx, item , generics ) ;
308+ check_variances_for_type_defn ( tcx, def_id ) ;
309309 res
310310 }
311311 hir:: ItemKind :: Trait ( ..) => check_trait ( tcx, item) ,
312312 hir:: ItemKind :: TraitAlias ( ..) => check_trait ( tcx, item) ,
313- // `ForeignItem`s are handled separately.
314- hir:: ItemKind :: ForeignMod { .. } => Ok ( ( ) ) ,
315- hir:: ItemKind :: TyAlias ( _, generics, hir_ty) if tcx. type_alias_is_lazy ( item. owner_id ) => {
313+ hir:: ItemKind :: TyAlias ( .., hir_ty) if tcx. type_alias_is_lazy ( item. owner_id ) => {
316314 let res = enter_wf_checking_ctxt ( tcx, def_id, |wfcx| {
317315 let ty = tcx. type_of ( def_id) . instantiate_identity ( ) ;
318316 let item_ty =
@@ -325,7 +323,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
325323 check_where_clauses ( wfcx, item. span , def_id) ;
326324 Ok ( ( ) )
327325 } ) ;
328- check_variances_for_type_defn ( tcx, item , generics ) ;
326+ check_variances_for_type_defn ( tcx, def_id ) ;
329327 res
330328 }
331329 _ => Ok ( ( ) ) ,
@@ -2039,27 +2037,23 @@ fn legacy_receiver_is_implemented<'tcx>(
20392037 }
20402038}
20412039
2042- fn check_variances_for_type_defn < ' tcx > (
2043- tcx : TyCtxt < ' tcx > ,
2044- item : & ' tcx hir:: Item < ' tcx > ,
2045- hir_generics : & hir:: Generics < ' tcx > ,
2046- ) {
2047- match item. kind {
2048- ItemKind :: Enum ( ..) | ItemKind :: Struct ( ..) | ItemKind :: Union ( ..) => {
2040+ fn check_variances_for_type_defn < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : LocalDefId ) {
2041+ match tcx. def_kind ( def_id) {
2042+ DefKind :: Enum | DefKind :: Struct | DefKind :: Union => {
20492043 // Ok
20502044 }
2051- ItemKind :: TyAlias ( .. ) => {
2045+ DefKind :: TyAlias => {
20522046 assert ! (
2053- tcx. type_alias_is_lazy( item . owner_id ) ,
2047+ tcx. type_alias_is_lazy( def_id ) ,
20542048 "should not be computing variance of non-free type alias"
20552049 ) ;
20562050 }
2057- kind => span_bug ! ( item . span , "cannot compute the variances of {kind:?}" ) ,
2051+ kind => span_bug ! ( tcx . def_span ( def_id ) , "cannot compute the variances of {kind:?}" ) ,
20582052 }
20592053
2060- let ty_predicates = tcx. predicates_of ( item . owner_id ) ;
2054+ let ty_predicates = tcx. predicates_of ( def_id ) ;
20612055 assert_eq ! ( ty_predicates. parent, None ) ;
2062- let variances = tcx. variances_of ( item . owner_id ) ;
2056+ let variances = tcx. variances_of ( def_id ) ;
20632057
20642058 let mut constrained_parameters: FxHashSet < _ > = variances
20652059 . iter ( )
@@ -2072,8 +2066,10 @@ fn check_variances_for_type_defn<'tcx>(
20722066
20732067 // Lazily calculated because it is only needed in case of an error.
20742068 let explicitly_bounded_params = LazyCell :: new ( || {
2075- let icx = crate :: collect:: ItemCtxt :: new ( tcx, item. owner_id . def_id ) ;
2076- hir_generics
2069+ let icx = crate :: collect:: ItemCtxt :: new ( tcx, def_id) ;
2070+ tcx. hir_node_by_def_id ( def_id)
2071+ . generics ( )
2072+ . unwrap ( )
20772073 . predicates
20782074 . iter ( )
20792075 . filter_map ( |predicate| match predicate. kind {
@@ -2088,18 +2084,20 @@ fn check_variances_for_type_defn<'tcx>(
20882084 . collect :: < FxHashSet < _ > > ( )
20892085 } ) ;
20902086
2091- let ty_generics = tcx. generics_of ( item. owner_id ) ;
2092-
20932087 for ( index, _) in variances. iter ( ) . enumerate ( ) {
20942088 let parameter = Parameter ( index as u32 ) ;
20952089
20962090 if constrained_parameters. contains ( & parameter) {
20972091 continue ;
20982092 }
20992093
2100- let ty_param = & ty_generics. own_params [ index] ;
2094+ let node = tcx. hir_node_by_def_id ( def_id) ;
2095+ let item = node. expect_item ( ) ;
2096+ let hir_generics = node. generics ( ) . unwrap ( ) ;
21012097 let hir_param = & hir_generics. params [ index] ;
21022098
2099+ let ty_param = & tcx. generics_of ( item. owner_id ) . own_params [ index] ;
2100+
21032101 if ty_param. def_id != hir_param. def_id . into ( ) {
21042102 // Valid programs always have lifetimes before types in the generic parameter list.
21052103 // ty_generics are normalized to be in this required order, and variances are built
@@ -2117,7 +2115,7 @@ fn check_variances_for_type_defn<'tcx>(
21172115
21182116 // Look for `ErrorGuaranteed` deeply within this type.
21192117 if let ControlFlow :: Break ( ErrorGuaranteed { .. } ) = tcx
2120- . type_of ( item . owner_id )
2118+ . type_of ( def_id )
21212119 . instantiate_identity ( )
21222120 . visit_with ( & mut HasErrorDeep { tcx, seen : Default :: default ( ) } )
21232121 {
0 commit comments