@@ -292,26 +292,24 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
292
292
}
293
293
hir:: ItemKind :: Fn { ident, sig, .. } => check_item_fn ( tcx, def_id, ident, sig. decl ) ,
294
294
hir:: ItemKind :: Const ( _, _, ty, _) => check_const_item ( tcx, def_id, ty. span , item. span ) ,
295
- hir:: ItemKind :: Struct ( _ , generics , _ ) => {
295
+ hir:: ItemKind :: Struct ( .. ) => {
296
296
let res = check_type_defn ( tcx, item, false ) ;
297
- check_variances_for_type_defn ( tcx, item , generics ) ;
297
+ check_variances_for_type_defn ( tcx, def_id ) ;
298
298
res
299
299
}
300
- hir:: ItemKind :: Union ( _ , generics , _ ) => {
300
+ hir:: ItemKind :: Union ( .. ) => {
301
301
let res = check_type_defn ( tcx, item, true ) ;
302
- check_variances_for_type_defn ( tcx, item , generics ) ;
302
+ check_variances_for_type_defn ( tcx, def_id ) ;
303
303
res
304
304
}
305
- hir:: ItemKind :: Enum ( _ , generics , _ ) => {
305
+ hir:: ItemKind :: Enum ( .. ) => {
306
306
let res = check_type_defn ( tcx, item, true ) ;
307
- check_variances_for_type_defn ( tcx, item , generics ) ;
307
+ check_variances_for_type_defn ( tcx, def_id ) ;
308
308
res
309
309
}
310
310
hir:: ItemKind :: Trait ( ..) => check_trait ( tcx, item) ,
311
311
hir:: ItemKind :: TraitAlias ( ..) => check_trait ( tcx, item) ,
312
- // `ForeignItem`s are handled separately.
313
- hir:: ItemKind :: ForeignMod { .. } => Ok ( ( ) ) ,
314
- hir:: ItemKind :: TyAlias ( _, generics, hir_ty) if tcx. type_alias_is_lazy ( item. owner_id ) => {
312
+ hir:: ItemKind :: TyAlias ( .., hir_ty) if tcx. type_alias_is_lazy ( item. owner_id ) => {
315
313
let res = enter_wf_checking_ctxt ( tcx, def_id, |wfcx| {
316
314
let ty = tcx. type_of ( def_id) . instantiate_identity ( ) ;
317
315
let item_ty =
@@ -324,7 +322,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
324
322
check_where_clauses ( wfcx, item. span , def_id) ;
325
323
Ok ( ( ) )
326
324
} ) ;
327
- check_variances_for_type_defn ( tcx, item , generics ) ;
325
+ check_variances_for_type_defn ( tcx, def_id ) ;
328
326
res
329
327
}
330
328
_ => Ok ( ( ) ) ,
@@ -1971,27 +1969,23 @@ fn legacy_receiver_is_implemented<'tcx>(
1971
1969
}
1972
1970
}
1973
1971
1974
- fn check_variances_for_type_defn < ' tcx > (
1975
- tcx : TyCtxt < ' tcx > ,
1976
- item : & ' tcx hir:: Item < ' tcx > ,
1977
- hir_generics : & hir:: Generics < ' tcx > ,
1978
- ) {
1979
- match item. kind {
1980
- ItemKind :: Enum ( ..) | ItemKind :: Struct ( ..) | ItemKind :: Union ( ..) => {
1972
+ fn check_variances_for_type_defn < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : LocalDefId ) {
1973
+ match tcx. def_kind ( def_id) {
1974
+ DefKind :: Enum | DefKind :: Struct | DefKind :: Union => {
1981
1975
// Ok
1982
1976
}
1983
- ItemKind :: TyAlias ( .. ) => {
1977
+ DefKind :: TyAlias => {
1984
1978
assert ! (
1985
- tcx. type_alias_is_lazy( item . owner_id ) ,
1979
+ tcx. type_alias_is_lazy( def_id ) ,
1986
1980
"should not be computing variance of non-free type alias"
1987
1981
) ;
1988
1982
}
1989
- kind => span_bug ! ( item . span , "cannot compute the variances of {kind:?}" ) ,
1983
+ kind => span_bug ! ( tcx . def_span ( def_id ) , "cannot compute the variances of {kind:?}" ) ,
1990
1984
}
1991
1985
1992
- let ty_predicates = tcx. predicates_of ( item . owner_id ) ;
1986
+ let ty_predicates = tcx. predicates_of ( def_id ) ;
1993
1987
assert_eq ! ( ty_predicates. parent, None ) ;
1994
- let variances = tcx. variances_of ( item . owner_id ) ;
1988
+ let variances = tcx. variances_of ( def_id ) ;
1995
1989
1996
1990
let mut constrained_parameters: FxHashSet < _ > = variances
1997
1991
. iter ( )
@@ -2004,8 +1998,10 @@ fn check_variances_for_type_defn<'tcx>(
2004
1998
2005
1999
// Lazily calculated because it is only needed in case of an error.
2006
2000
let explicitly_bounded_params = LazyCell :: new ( || {
2007
- let icx = crate :: collect:: ItemCtxt :: new ( tcx, item. owner_id . def_id ) ;
2008
- hir_generics
2001
+ let icx = crate :: collect:: ItemCtxt :: new ( tcx, def_id) ;
2002
+ tcx. hir_node_by_def_id ( def_id)
2003
+ . generics ( )
2004
+ . unwrap ( )
2009
2005
. predicates
2010
2006
. iter ( )
2011
2007
. filter_map ( |predicate| match predicate. kind {
@@ -2020,18 +2016,20 @@ fn check_variances_for_type_defn<'tcx>(
2020
2016
. collect :: < FxHashSet < _ > > ( )
2021
2017
} ) ;
2022
2018
2023
- let ty_generics = tcx. generics_of ( item. owner_id ) ;
2024
-
2025
2019
for ( index, _) in variances. iter ( ) . enumerate ( ) {
2026
2020
let parameter = Parameter ( index as u32 ) ;
2027
2021
2028
2022
if constrained_parameters. contains ( & parameter) {
2029
2023
continue ;
2030
2024
}
2031
2025
2032
- let ty_param = & ty_generics. own_params [ index] ;
2026
+ let node = tcx. hir_node_by_def_id ( def_id) ;
2027
+ let item = node. expect_item ( ) ;
2028
+ let hir_generics = node. generics ( ) . unwrap ( ) ;
2033
2029
let hir_param = & hir_generics. params [ index] ;
2034
2030
2031
+ let ty_param = & tcx. generics_of ( item. owner_id ) . own_params [ index] ;
2032
+
2035
2033
if ty_param. def_id != hir_param. def_id . into ( ) {
2036
2034
// Valid programs always have lifetimes before types in the generic parameter list.
2037
2035
// ty_generics are normalized to be in this required order, and variances are built
@@ -2049,7 +2047,7 @@ fn check_variances_for_type_defn<'tcx>(
2049
2047
2050
2048
// Look for `ErrorGuaranteed` deeply within this type.
2051
2049
if let ControlFlow :: Break ( ErrorGuaranteed { .. } ) = tcx
2052
- . type_of ( item . owner_id )
2050
+ . type_of ( def_id )
2053
2051
. instantiate_identity ( )
2054
2052
. visit_with ( & mut HasErrorDeep { tcx, seen : Default :: default ( ) } )
2055
2053
{
0 commit comments