Skip to content

Commit

Permalink
Allow specializing on const trait bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Sep 24, 2022
1 parent e1c28e0 commit a480ab6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
11 changes: 4 additions & 7 deletions compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,10 @@ fn trait_predicate_kind<'tcx>(
predicate: ty::Predicate<'tcx>,
) -> Option<TraitSpecializationKind> {
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(_)
Expand Down
55 changes: 55 additions & 0 deletions src/test/ui/specialization/const_trait_impl.rs
Original file line number Diff line number Diff line change
@@ -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<T: ~const Default> const A for T {
default fn a() -> u32 {
2
}
}

impl<T: ~const Default + ~const Sup> const A for T {
default fn a() -> u32 {
3
}
}

impl<T: ~const Default + ~const Sub> const A for T {
fn a() -> u32 {
T::foo()
}
}

const _: () = assert!(<()>::a() == 42);
const _: () = assert!(<u8>::a() == 3);
const _: () = assert!(<u16>::a() == 2);

fn main() {}

0 comments on commit a480ab6

Please sign in to comment.