@@ -1441,6 +1441,32 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
14411441 // of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
14421442 None
14431443 } else if tcx. lazy_normalization ( ) {
1444+ // Only provide backwards declared generics to cg defaults (#83938)
1445+ if let Node :: GenericParam ( GenericParam {
1446+ hir_id : param_id,
1447+ kind : GenericParamKind :: Const { .. } ,
1448+ ..
1449+ } ) = tcx. hir ( ) . get ( tcx. hir ( ) . get_parent_node ( hir_id) )
1450+ {
1451+ let item_id = tcx. hir ( ) . get_parent_node ( * param_id) ;
1452+ let item_def_id = tcx. hir ( ) . local_def_id ( item_id) ;
1453+ let generics = tcx. generics_of ( item_def_id. to_def_id ( ) ) ;
1454+ let param_def = tcx. hir ( ) . local_def_id ( * param_id) . to_def_id ( ) ;
1455+ let param_def_idx = generics. param_def_id_to_index [ & param_def] ;
1456+ let params = generics. params [ ..param_def_idx as usize ] . to_owned ( ) ;
1457+ let param_def_id_to_index =
1458+ params. iter ( ) . map ( |param| ( param. def_id , param. index ) ) . collect ( ) ;
1459+
1460+ return ty:: Generics {
1461+ parent : generics. parent ,
1462+ parent_count : generics. parent_count ,
1463+ params,
1464+ param_def_id_to_index,
1465+ has_self : generics. has_self ,
1466+ has_late_bound_regions : generics. has_late_bound_regions ,
1467+ } ;
1468+ }
1469+
14441470 // HACK(eddyb) this provides the correct generics when
14451471 // `feature(const_generics)` is enabled, so that const expressions
14461472 // used with const generics, e.g. `Foo<{N+1}>`, can work at all.
@@ -2359,7 +2385,8 @@ fn trait_explicit_predicates_and_bounds(
23592385}
23602386
23612387fn explicit_predicates_of ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> ty:: GenericPredicates < ' _ > {
2362- if let DefKind :: Trait = tcx. def_kind ( def_id) {
2388+ let def_kind = tcx. def_kind ( def_id) ;
2389+ if let DefKind :: Trait = def_kind {
23632390 // Remove bounds on associated types from the predicates, they will be
23642391 // returned by `explicit_item_bounds`.
23652392 let predicates_and_bounds = tcx. trait_explicit_predicates_and_bounds ( def_id. expect_local ( ) ) ;
@@ -2404,6 +2431,21 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
24042431 }
24052432 }
24062433 } else {
2434+ if matches ! ( def_kind, DefKind :: AnonConst ) && tcx. lazy_normalization ( ) {
2435+ // Provide predicates of parent item of cg defaults manually
2436+ // as generics_of doesn't return a parent for the generics
2437+ let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id. expect_local ( ) ) ;
2438+ if let Node :: GenericParam ( hir:: GenericParam {
2439+ hir_id : param_id,
2440+ kind : hir:: GenericParamKind :: Const { .. } ,
2441+ ..
2442+ } ) = tcx. hir ( ) . get ( tcx. hir ( ) . get_parent_node ( hir_id) )
2443+ {
2444+ let item_id = tcx. hir ( ) . get_parent_node ( * param_id) ;
2445+ let item_def_id = tcx. hir ( ) . local_def_id ( item_id) . to_def_id ( ) ;
2446+ return tcx. explicit_predicates_of ( item_def_id) ;
2447+ }
2448+ }
24072449 gather_explicit_predicates_of ( tcx, def_id)
24082450 }
24092451}
0 commit comments