@@ -51,7 +51,15 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
5151 // of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
5252 None
5353 } else if tcx. lazy_normalization ( ) {
54- if let Some ( param_id) = tcx. hir ( ) . opt_const_param_default_param_def_id ( hir_id) {
54+ let parent_node = tcx. hir ( ) . get_parent ( hir_id) ;
55+ if let Node :: Variant ( Variant { disr_expr : Some ( constant) , .. } ) = parent_node
56+ && constant. hir_id == hir_id
57+ {
58+ // enum variant discriminants are not allowed to use any kind of generics
59+ None
60+ } else if let Some ( param_id) =
61+ tcx. hir ( ) . opt_const_param_default_param_def_id ( hir_id)
62+ {
5563 // If the def_id we are calling generics_of on is an anon ct default i.e:
5664 //
5765 // struct Foo<const N: usize = { .. }>;
@@ -94,15 +102,15 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
94102 has_self : generics. has_self ,
95103 has_late_bound_regions : generics. has_late_bound_regions ,
96104 } ;
105+ } else {
106+ // HACK(eddyb) this provides the correct generics when
107+ // `feature(generic_const_expressions)` is enabled, so that const expressions
108+ // used with const generics, e.g. `Foo<{N+1}>`, can work at all.
109+ //
110+ // Note that we do not supply the parent generics when using
111+ // `min_const_generics`.
112+ Some ( parent_def_id. to_def_id ( ) )
97113 }
98-
99- // HACK(eddyb) this provides the correct generics when
100- // `feature(generic_const_expressions)` is enabled, so that const expressions
101- // used with const generics, e.g. `Foo<{N+1}>`, can work at all.
102- //
103- // Note that we do not supply the parent generics when using
104- // `min_const_generics`.
105- Some ( parent_def_id. to_def_id ( ) )
106114 } else {
107115 let parent_node = tcx. hir ( ) . get_parent ( hir_id) ;
108116 match parent_node {
@@ -115,11 +123,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
115123 {
116124 Some ( parent_def_id. to_def_id ( ) )
117125 }
118- Node :: Variant ( Variant { disr_expr : Some ( constant) , .. } )
119- if constant. hir_id == hir_id =>
120- {
121- Some ( parent_def_id. to_def_id ( ) )
122- }
123126 Node :: Expr ( & Expr { kind : ExprKind :: ConstBlock ( _) , .. } ) => {
124127 Some ( tcx. typeck_root_def_id ( def_id. to_def_id ( ) ) )
125128 }
0 commit comments