@@ -51,7 +51,15 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
51
51
// of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
52
52
None
53
53
} 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
+ {
55
63
// If the def_id we are calling generics_of on is an anon ct default i.e:
56
64
//
57
65
// struct Foo<const N: usize = { .. }>;
@@ -94,15 +102,15 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
94
102
has_self : generics. has_self ,
95
103
has_late_bound_regions : generics. has_late_bound_regions ,
96
104
} ;
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 ( ) )
97
113
}
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 ( ) )
106
114
} else {
107
115
let parent_node = tcx. hir ( ) . get_parent ( hir_id) ;
108
116
match parent_node {
@@ -115,11 +123,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
115
123
{
116
124
Some ( parent_def_id. to_def_id ( ) )
117
125
}
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
- }
123
126
Node :: Expr ( & Expr { kind : ExprKind :: ConstBlock ( _) , .. } ) => {
124
127
Some ( tcx. typeck_root_def_id ( def_id. to_def_id ( ) ) )
125
128
}
0 commit comments