mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 00:34:06 +00:00
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:
commit
21265dd0d2
@ -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(_)
|
||||
|
@ -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() {}
|
@ -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`.
|
@ -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() {}
|
@ -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
|
||||
|
55
src/test/ui/specialization/const_trait_impl.rs
Normal file
55
src/test/ui/specialization/const_trait_impl.rs
Normal 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() {}
|
Loading…
Reference in New Issue
Block a user