-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow specialized const trait impls.
- Loading branch information
Showing
9 changed files
with
286 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/test/ui/rfc-2632-const-trait-impl/specialization/const-default-const-specialized.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Tests that a const default trait impl can be specialized by another const | ||
// trait impl and that the specializing impl will be used during const-eval. | ||
|
||
// run-pass | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(min_specialization)] | ||
|
||
trait Value { | ||
fn value() -> u32; | ||
} | ||
|
||
const fn get_value<T: ~const Value>() -> u32 { | ||
T::value() | ||
} | ||
|
||
impl<T> const Value for T { | ||
default fn value() -> u32 { | ||
0 | ||
} | ||
} | ||
|
||
struct FortyTwo; | ||
|
||
impl const Value for FortyTwo { | ||
fn value() -> u32 { | ||
42 | ||
} | ||
} | ||
|
||
const ZERO: u32 = get_value::<()>(); | ||
|
||
const FORTY_TWO: u32 = get_value::<FortyTwo>(); | ||
|
||
fn main() { | ||
assert_eq!(ZERO, 0); | ||
assert_eq!(FORTY_TWO, 42); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/test/ui/rfc-2632-const-trait-impl/specialization/const-default-non-const-specialized.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Tests that a const default trait impl can be specialized by a non-const trait | ||
// impl, but that the specializing impl cannot be used in a const context. | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(min_specialization)] | ||
|
||
trait Value { | ||
fn value() -> u32; | ||
} | ||
|
||
const fn get_value<T: ~const Value>() -> u32 { | ||
T::value() | ||
//~^ ERROR any use of this value will cause an error [const_err] | ||
//~| WARNING this was previously accepted | ||
} | ||
|
||
impl<T> const Value for T { | ||
default fn value() -> u32 { | ||
0 | ||
} | ||
} | ||
|
||
struct FortyTwo; | ||
|
||
impl Value for FortyTwo { | ||
fn value() -> u32 { | ||
println!("You can't do that (constly)"); | ||
42 | ||
} | ||
} | ||
|
||
const ZERO: u32 = get_value::<()>(); | ||
|
||
const FORTY_TWO: u32 = | ||
get_value::<FortyTwo>(); // This is the line that causes the error, but it gets reported above | ||
|
||
fn main() {} |
37 changes: 37 additions & 0 deletions
37
...st/ui/rfc-2632-const-trait-impl/specialization/const-default-non-const-specialized.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
error: any use of this value will cause an error | ||
--> $DIR/const-default-non-const-specialized.rs:12:5 | ||
| | ||
LL | T::value() | ||
| ^^^^^^^^^^ | ||
| | | ||
| calling non-const function `<FortyTwo as Value>::value` | ||
| inside `get_value::<FortyTwo>` at $DIR/const-default-non-const-specialized.rs:12:5 | ||
| inside `FORTY_TWO` at $DIR/const-default-non-const-specialized.rs:35:5 | ||
... | ||
LL | const FORTY_TWO: u32 = | ||
| -------------------- | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800> | ||
|
||
error: aborting due to previous error | ||
|
||
Future incompatibility report: Future breakage diagnostic: | ||
error: any use of this value will cause an error | ||
--> $DIR/const-default-non-const-specialized.rs:12:5 | ||
| | ||
LL | T::value() | ||
| ^^^^^^^^^^ | ||
| | | ||
| calling non-const function `<FortyTwo as Value>::value` | ||
| inside `get_value::<FortyTwo>` at $DIR/const-default-non-const-specialized.rs:12:5 | ||
| inside `FORTY_TWO` at $DIR/const-default-non-const-specialized.rs:35:5 | ||
... | ||
LL | const FORTY_TWO: u32 = | ||
| -------------------- | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800> | ||
|
14 changes: 14 additions & 0 deletions
14
src/test/ui/rfc-2632-const-trait-impl/specialization/default-keyword.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// check-pass | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(min_specialization)] | ||
|
||
trait Foo { | ||
fn foo(); | ||
} | ||
|
||
impl const Foo for u32 { | ||
default fn foo() {} | ||
} | ||
|
||
fn main() {} |
34 changes: 34 additions & 0 deletions
34
...test/ui/rfc-2632-const-trait-impl/specialization/issue-95186-specialize-on-tilde-const.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Tests that `~const` trait bounds can be used to specialize const trait impls. | ||
|
||
// check-pass | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(rustc_attrs)] | ||
#![feature(min_specialization)] | ||
|
||
#[rustc_specialization_trait] | ||
trait Specialize {} | ||
|
||
trait Foo {} | ||
|
||
impl<T> const Foo for T {} | ||
|
||
impl<T> const Foo for T | ||
where | ||
T: ~const Specialize, | ||
{} | ||
|
||
trait Bar {} | ||
|
||
impl<T> const Bar for T | ||
where | ||
T: ~const Foo, | ||
{} | ||
|
||
impl<T> const Bar for T | ||
where | ||
T: ~const Foo, | ||
T: ~const Specialize, | ||
{} | ||
|
||
fn main() {} |
28 changes: 28 additions & 0 deletions
28
...-2632-const-trait-impl/specialization/issue-95187-same-trait-bound-different-constness.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Tests that `T: ~const Foo` and `T: Foo` are treated as equivalent for the | ||
// purposes of min_specialization. | ||
|
||
// check-pass | ||
|
||
#![feature(rustc_attrs)] | ||
#![feature(min_specialization)] | ||
#![feature(const_trait_impl)] | ||
|
||
#[rustc_specialization_trait] | ||
trait Specialize {} | ||
|
||
trait Foo {} | ||
|
||
trait Bar {} | ||
|
||
impl<T> const Bar for T | ||
where | ||
T: ~const Foo, | ||
{} | ||
|
||
impl<T> Bar for T | ||
where | ||
T: Foo, | ||
T: Specialize, | ||
{} | ||
|
||
fn main() {} |
34 changes: 34 additions & 0 deletions
34
src/test/ui/rfc-2632-const-trait-impl/specialization/non-const-default-const-specialized.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Tests that a non-const default impl can be specialized by a const trait impl, | ||
// but that the default impl cannot be used in a const context. | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(min_specialization)] | ||
|
||
trait Value { | ||
fn value() -> u32; | ||
} | ||
|
||
const fn get_value<T: ~const Value>() -> u32 { | ||
T::value() | ||
} | ||
|
||
impl<T> Value for T { | ||
default fn value() -> u32 { | ||
println!("You can't do that (constly)"); | ||
0 | ||
} | ||
} | ||
|
||
struct FortyTwo; | ||
|
||
impl const Value for FortyTwo { | ||
fn value() -> u32 { | ||
42 | ||
} | ||
} | ||
|
||
const ZERO: u32 = get_value::<()>(); //~ ERROR the trait bound `(): ~const Value` is not satisfied | ||
|
||
const FORTY_TWO: u32 = get_value::<FortyTwo>(); | ||
|
||
fn main() {} |
20 changes: 20 additions & 0 deletions
20
...st/ui/rfc-2632-const-trait-impl/specialization/non-const-default-const-specialized.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error[E0277]: the trait bound `(): ~const Value` is not satisfied | ||
--> $DIR/non-const-default-const-specialized.rs:30:31 | ||
| | ||
LL | const ZERO: u32 = get_value::<()>(); | ||
| ^^ the trait `~const Value` is not implemented for `()` | ||
| | ||
note: the trait `Value` is implemented for `()`, but that implementation is not `const` | ||
--> $DIR/non-const-default-const-specialized.rs:30:31 | ||
| | ||
LL | const ZERO: u32 = get_value::<()>(); | ||
| ^^ | ||
note: required by a bound in `get_value` | ||
--> $DIR/non-const-default-const-specialized.rs:11:23 | ||
| | ||
LL | const fn get_value<T: ~const Value>() -> u32 { | ||
| ^^^^^^^^^^^^ required by this bound in `get_value` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |