@@ -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 ( ( ) ) ,
@@ -1979,27 +1977,23 @@ fn legacy_receiver_is_implemented<'tcx>(
1979
1977
}
1980
1978
}
1981
1979
1982
- fn check_variances_for_type_defn < ' tcx > (
1983
- tcx : TyCtxt < ' tcx > ,
1984
- item : & ' tcx hir:: Item < ' tcx > ,
1985
- hir_generics : & hir:: Generics < ' tcx > ,
1986
- ) {
1987
- match item. kind {
1988
- ItemKind :: Enum ( ..) | ItemKind :: Struct ( ..) | ItemKind :: Union ( ..) => {
1980
+ fn check_variances_for_type_defn < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : LocalDefId ) {
1981
+ match tcx. def_kind ( def_id) {
1982
+ DefKind :: Enum | DefKind :: Struct | DefKind :: Union => {
1989
1983
// Ok
1990
1984
}
1991
- ItemKind :: TyAlias ( .. ) => {
1985
+ DefKind :: TyAlias => {
1992
1986
assert ! (
1993
- tcx. type_alias_is_lazy( item . owner_id ) ,
1987
+ tcx. type_alias_is_lazy( def_id ) ,
1994
1988
"should not be computing variance of non-free type alias"
1995
1989
) ;
1996
1990
}
1997
- kind => span_bug ! ( item . span , "cannot compute the variances of {kind:?}" ) ,
1991
+ kind => span_bug ! ( tcx . def_span ( def_id ) , "cannot compute the variances of {kind:?}" ) ,
1998
1992
}
1999
1993
2000
- let ty_predicates = tcx. predicates_of ( item . owner_id ) ;
1994
+ let ty_predicates = tcx. predicates_of ( def_id ) ;
2001
1995
assert_eq ! ( ty_predicates. parent, None ) ;
2002
- let variances = tcx. variances_of ( item . owner_id ) ;
1996
+ let variances = tcx. variances_of ( def_id ) ;
2003
1997
2004
1998
let mut constrained_parameters: FxHashSet < _ > = variances
2005
1999
. iter ( )
@@ -2012,8 +2006,10 @@ fn check_variances_for_type_defn<'tcx>(
2012
2006
2013
2007
// Lazily calculated because it is only needed in case of an error.
2014
2008
let explicitly_bounded_params = LazyCell :: new ( || {
2015
- let icx = crate :: collect:: ItemCtxt :: new ( tcx, item. owner_id . def_id ) ;
2016
- hir_generics
2009
+ let icx = crate :: collect:: ItemCtxt :: new ( tcx, def_id) ;
2010
+ tcx. hir_node_by_def_id ( def_id)
2011
+ . generics ( )
2012
+ . unwrap ( )
2017
2013
. predicates
2018
2014
. iter ( )
2019
2015
. filter_map ( |predicate| match predicate. kind {
@@ -2028,18 +2024,20 @@ fn check_variances_for_type_defn<'tcx>(
2028
2024
. collect :: < FxHashSet < _ > > ( )
2029
2025
} ) ;
2030
2026
2031
- let ty_generics = tcx. generics_of ( item. owner_id ) ;
2032
-
2033
2027
for ( index, _) in variances. iter ( ) . enumerate ( ) {
2034
2028
let parameter = Parameter ( index as u32 ) ;
2035
2029
2036
2030
if constrained_parameters. contains ( & parameter) {
2037
2031
continue ;
2038
2032
}
2039
2033
2040
- let ty_param = & ty_generics. own_params [ index] ;
2034
+ let node = tcx. hir_node_by_def_id ( def_id) ;
2035
+ let item = node. expect_item ( ) ;
2036
+ let hir_generics = node. generics ( ) . unwrap ( ) ;
2041
2037
let hir_param = & hir_generics. params [ index] ;
2042
2038
2039
+ let ty_param = & tcx. generics_of ( item. owner_id ) . own_params [ index] ;
2040
+
2043
2041
if ty_param. def_id != hir_param. def_id . into ( ) {
2044
2042
// Valid programs always have lifetimes before types in the generic parameter list.
2045
2043
// ty_generics are normalized to be in this required order, and variances are built
@@ -2057,7 +2055,7 @@ fn check_variances_for_type_defn<'tcx>(
2057
2055
2058
2056
// Look for `ErrorGuaranteed` deeply within this type.
2059
2057
if let ControlFlow :: Break ( ErrorGuaranteed { .. } ) = tcx
2060
- . type_of ( item . owner_id )
2058
+ . type_of ( def_id )
2061
2059
. instantiate_identity ( )
2062
2060
. visit_with ( & mut HasErrorDeep { tcx, seen : Default :: default ( ) } )
2063
2061
{
0 commit comments