From a480ab6839ff88a770ca87faf4a4cadcb001bcbe Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sat, 24 Sep 2022 06:02:49 +0000 Subject: [PATCH] Allow specializing on const trait bounds --- .../src/impl_wf_check/min_specialization.rs | 11 ++-- .../ui/specialization/const_trait_impl.rs | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/specialization/const_trait_impl.rs diff --git a/compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs b/compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs index 2741d9f776ce2..5bebd7dee09b9 100644 --- a/compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs +++ b/compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs @@ -423,13 +423,10 @@ fn trait_predicate_kind<'tcx>( predicate: ty::Predicate<'tcx>, ) -> Option { match predicate.kind().skip_binder() { - ty::PredicateKind::Trait(ty::TraitPredicate { - trait_ref, - constness: ty::BoundConstness::NotConst, - polarity: _, - }) => Some(tcx.trait_def(trait_ref.def_id).specialization_kind), - ty::PredicateKind::Trait(_) - | ty::PredicateKind::RegionOutlives(_) + ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref, constness: _, polarity: _ }) => { + Some(tcx.trait_def(trait_ref.def_id).specialization_kind) + } + ty::PredicateKind::RegionOutlives(_) | ty::PredicateKind::TypeOutlives(_) | ty::PredicateKind::Projection(_) | ty::PredicateKind::WellFormed(_) diff --git a/src/test/ui/specialization/const_trait_impl.rs b/src/test/ui/specialization/const_trait_impl.rs new file mode 100644 index 0000000000000..05ba4c8d45d5b --- /dev/null +++ b/src/test/ui/specialization/const_trait_impl.rs @@ -0,0 +1,55 @@ +// check-pass +#![feature(const_trait_impl, min_specialization, rustc_attrs)] + +#[rustc_specialization_trait] +#[const_trait] +pub unsafe trait Sup { + fn foo() -> u32; +} + +#[rustc_specialization_trait] +#[const_trait] +pub unsafe trait Sub: ~const Sup {} + +unsafe impl const Sup for u8 { + default fn foo() -> u32 { + 1 + } +} + +unsafe impl const Sup for () { + fn foo() -> u32 { + 42 + } +} + +unsafe impl const Sub for () {} + +#[const_trait] +pub trait A { + fn a() -> u32; +} + +impl const A for T { + default fn a() -> u32 { + 2 + } +} + +impl const A for T { + default fn a() -> u32 { + 3 + } +} + +impl const A for T { + fn a() -> u32 { + T::foo() + } +} + +const _: () = assert!(<()>::a() == 42); +const _: () = assert!(::a() == 3); +const _: () = assert!(::a() == 2); + +fn main() {}