Auto merge of #102224 - fee1-dead-contrib:const_trait_impl_specialization, r=oli-obk

Allow specializing on const trait bounds
This commit is contained in:
bors 2022-09-26 08:08:35 +00:00
commit 21265dd0d2
6 changed files with 143 additions and 7 deletions

View File

@ -423,13 +423,10 @@ fn trait_predicate_kind<'tcx>(
predicate: ty::Predicate<'tcx>, predicate: ty::Predicate<'tcx>,
) -> Option<TraitSpecializationKind> { ) -> Option<TraitSpecializationKind> {
match predicate.kind().skip_binder() { match predicate.kind().skip_binder() {
ty::PredicateKind::Trait(ty::TraitPredicate { ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref, constness: _, polarity: _ }) => {
trait_ref, Some(tcx.trait_def(trait_ref.def_id).specialization_kind)
constness: ty::BoundConstness::NotConst, }
polarity: _, ty::PredicateKind::RegionOutlives(_)
}) => Some(tcx.trait_def(trait_ref.def_id).specialization_kind),
ty::PredicateKind::Trait(_)
| ty::PredicateKind::RegionOutlives(_)
| ty::PredicateKind::TypeOutlives(_) | ty::PredicateKind::TypeOutlives(_)
| ty::PredicateKind::Projection(_) | ty::PredicateKind::Projection(_)
| ty::PredicateKind::WellFormed(_) | ty::PredicateKind::WellFormed(_)

View File

@ -0,0 +1,31 @@
#![feature(const_trait_impl, min_specialization, rustc_attrs)]
#[rustc_specialization_trait]
#[const_trait]
pub trait Sup {}
impl const Sup for () {}
#[const_trait]
pub trait A {
fn a() -> u32;
}
impl<T: Default> A for T {
default fn a() -> u32 {
2
}
}
impl<T: Default + ~const Sup> const A for T {
fn a() -> u32 {
3
}
}
const fn generic<T: Default>() {
<T as A>::a();
//~^ ERROR: the trait bound `T: ~const Sup` is not satisfied
}
fn main() {}

View File

@ -0,0 +1,19 @@
error[E0277]: the trait bound `T: ~const Sup` is not satisfied
--> $DIR/specializing-constness-2.rs:27:5
|
LL | <T as A>::a();
| ^^^^^^^^^^^^^ the trait `~const Sup` is not implemented for `T`
|
note: required for `T` to implement `~const A`
--> $DIR/specializing-constness-2.rs:20:37
|
LL | impl<T: Default + ~const Sup> const A for T {
| ^ ^
help: consider further restricting this bound
|
LL | const fn generic<T: Default + ~const Sup>() {
| ++++++++++++
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.

View File

@ -0,0 +1,26 @@
#![feature(const_trait_impl, min_specialization, rustc_attrs)]
#[rustc_specialization_trait]
#[const_trait]
pub trait Sup {}
impl const Sup for () {}
#[const_trait]
pub trait A {
fn a() -> u32;
}
impl<T: ~const Default> const A for T {
default fn a() -> u32 {
2
}
}
impl<T: Default + Sup> A for T { //~ ERROR: cannot specialize
fn a() -> u32 {
3
}
}
fn main() {}

View File

@ -0,0 +1,8 @@
error: cannot specialize on trait `Default`
--> $DIR/specializing-constness.rs:20:9
|
LL | impl<T: Default + Sup> A for T {
| ^^^^^^^
error: aborting due to previous error

View File

@ -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() {}