From d777cb84e2d27a5f44eab94854b74fea33034bb4 Mon Sep 17 00:00:00 2001 From: Ellen Date: Sat, 7 Aug 2021 18:44:36 +0100 Subject: [PATCH] less opt in const param of --- compiler/rustc_typeck/src/collect/type_of.rs | 7 ++++-- src/test/ui/const-generics/enum-variants.rs | 24 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/const-generics/enum-variants.rs diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 96b3fa9aa0143..7083b11f7d0ac 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -190,8 +190,12 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< // Try to use the segment resolution if it is valid, otherwise we // default to the path resolution. let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res); + use def::CtorOf; let generics = match res { - Res::Def(DefKind::Ctor(..), def_id) => { + Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => { + tcx.generics_of(tcx.parent(def_id).and_then(|def_id| tcx.parent(def_id)).unwrap()) + } + Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => { tcx.generics_of(tcx.parent(def_id).unwrap()) } // 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< DefKind::Struct | DefKind::Union | DefKind::Enum - | DefKind::Variant | DefKind::Trait | DefKind::OpaqueTy | DefKind::TyAlias diff --git a/src/test/ui/const-generics/enum-variants.rs b/src/test/ui/const-generics/enum-variants.rs new file mode 100644 index 0000000000000..a82db1c4b3217 --- /dev/null +++ b/src/test/ui/const-generics/enum-variants.rs @@ -0,0 +1,24 @@ +// check-pass +pub enum Foo { + Variant, + Variant2(), + Variant3{}, +} + +struct Bar; +struct Bar2(); +struct Bar3 {} + +fn main() { + let _ = Foo::Variant::<1>; + let _ = Foo::Variant2::<1>(); + let _ = Foo::Variant3::<1>{}; + + let _ = Foo::<1>::Variant; + let _ = Foo::<1>::Variant2(); + let _ = Foo::<1>::Variant3{}; + + let _ = Bar::<1>; + let _ = Bar2::<1>(); + let _ = Bar3::<1>{}; +}