Skip to content

Commit be65d77

Browse files
authored
Rollup merge of rust-lang#87854 - BoxyUwU:var-None, r=oli-obk
correctly handle enum variants in `opt_const_param_of` Fixes rust-lang#87542 `opt_const_param_of` was returning `None` for args on an enum variant `Enum::Variant::<10>` because we called `generics_of` on the enum variant which has no generics. r? ```@oli-obk```
2 parents dd31fc3 + 5f61271 commit be65d77

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

compiler/rustc_typeck/src/collect/type_of.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
190190
// Try to use the segment resolution if it is valid, otherwise we
191191
// default to the path resolution.
192192
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
193+
use def::CtorOf;
193194
let generics = match res {
194-
Res::Def(DefKind::Ctor(..), def_id) => {
195+
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => tcx.generics_of(
196+
tcx.parent(def_id).and_then(|def_id| tcx.parent(def_id)).unwrap(),
197+
),
198+
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
195199
tcx.generics_of(tcx.parent(def_id).unwrap())
196200
}
197201
// Other `DefKind`s don't have generics and would ICE when calling
@@ -200,7 +204,6 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
200204
DefKind::Struct
201205
| DefKind::Union
202206
| DefKind::Enum
203-
| DefKind::Variant
204207
| DefKind::Trait
205208
| DefKind::OpaqueTy
206209
| DefKind::TyAlias
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// check-pass
2+
enum Foo<const N: usize> {
3+
Variant,
4+
Variant2(),
5+
Variant3{},
6+
}
7+
8+
struct Bar<const N: usize>;
9+
struct Bar2<const N: usize>();
10+
struct Bar3<const N: usize> {}
11+
12+
fn main() {
13+
let _ = Foo::Variant::<1>;
14+
let _ = Foo::Variant2::<1>();
15+
let _ = Foo::Variant3::<1>{};
16+
17+
let _ = Foo::<1>::Variant;
18+
let _ = Foo::<1>::Variant2();
19+
let _ = Foo::<1>::Variant3{};
20+
21+
let _ = Bar::<1>;
22+
let _ = Bar2::<1>();
23+
let _ = Bar3::<1>{};
24+
}

0 commit comments

Comments
 (0)