-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow specialized const trait impls. #95292
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5c25d30
Allow specialized const trait impls.
BGR360 ce03d25
Disallow specializing on const impls with non-const impls.
BGR360 d492b9b
Add #[const_trait] where needed in tests.
BGR360 c0ae62e
Require `~const` qualifier on trait bounds in specializing impls if p…
BGR360 fe53cac
Apply PR feedback.
BGR360 94f67e6
Oops, bless this test.
BGR360 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 46 additions & 0 deletions
46
...c-2632-const-trait-impl/specialization/const-default-bound-non-const-specialized-bound.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,46 @@ | ||
// Tests that trait bounds on specializing trait impls must be `~const` if the | ||
// same bound is present on the default impl and is `~const` there. | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(rustc_attrs)] | ||
#![feature(min_specialization)] | ||
|
||
#[rustc_specialization_trait] | ||
trait Specialize {} | ||
|
||
#[const_trait] | ||
trait Foo {} | ||
|
||
#[const_trait] | ||
trait Bar {} | ||
|
||
// bgr360: I was only able to exercise the code path that raises the | ||
// "missing ~const qualifier" error by making this base impl non-const, even | ||
// though that doesn't really make sense to do. As seen below, if the base impl | ||
// is made const, rustc fails earlier with an overlapping impl failure. | ||
impl<T> Bar for T | ||
where | ||
T: ~const Foo, | ||
{} | ||
|
||
impl<T> Bar for T | ||
where | ||
T: Foo, //~ ERROR missing `~const` qualifier | ||
T: Specialize, | ||
{} | ||
|
||
#[const_trait] | ||
trait Baz {} | ||
|
||
impl<T> const Baz for T | ||
where | ||
T: ~const Foo, | ||
{} | ||
|
||
impl<T> const Baz for T //~ ERROR conflicting implementations of trait `Baz` | ||
where | ||
T: Foo, | ||
T: Specialize, | ||
{} | ||
|
||
fn main() {} |
18 changes: 18 additions & 0 deletions
18
...32-const-trait-impl/specialization/const-default-bound-non-const-specialized-bound.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,18 @@ | ||
error: missing `~const` qualifier for specialization | ||
--> $DIR/const-default-bound-non-const-specialized-bound.rs:28:8 | ||
| | ||
LL | T: Foo, | ||
| ^^^ | ||
|
||
error[E0119]: conflicting implementations of trait `Baz` | ||
--> $DIR/const-default-bound-non-const-specialized-bound.rs:40:1 | ||
| | ||
LL | impl<T> const Baz for T | ||
| ----------------------- first implementation here | ||
... | ||
LL | impl<T> const Baz for T | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0119`. |
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
// 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)] | ||
|
||
#[const_trait] | ||
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); | ||
} |
26 changes: 26 additions & 0 deletions
26
...rfc-2632-const-trait-impl/specialization/const-default-impl-non-const-specialized-impl.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,26 @@ | ||
// Tests that specializing trait impls must be at least as const as the default impl. | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(min_specialization)] | ||
|
||
#[const_trait] | ||
trait Value { | ||
fn value() -> u32; | ||
} | ||
|
||
impl<T> const Value for T { | ||
default fn value() -> u32 { | ||
0 | ||
} | ||
} | ||
|
||
struct FortyTwo; | ||
|
||
impl Value for FortyTwo { //~ ERROR cannot specialize on const impl with non-const impl | ||
fn value() -> u32 { | ||
println!("You can't do that (constly)"); | ||
42 | ||
} | ||
} | ||
|
||
fn main() {} |
8 changes: 8 additions & 0 deletions
8
...2632-const-trait-impl/specialization/const-default-impl-non-const-specialized-impl.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,8 @@ | ||
error: cannot specialize on const impl with non-const impl | ||
--> $DIR/const-default-impl-non-const-specialized-impl.rs:19:1 | ||
| | ||
LL | impl Value for FortyTwo { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
// check-pass | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(min_specialization)] | ||
|
||
#[const_trait] | ||
trait Foo { | ||
fn foo(); | ||
} | ||
|
||
impl const Foo for u32 { | ||
default fn foo() {} | ||
} | ||
|
||
fn main() {} |
37 changes: 37 additions & 0 deletions
37
...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,37 @@ | ||
// 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)] | ||
|
||
#[const_trait] | ||
#[rustc_specialization_trait] | ||
trait Specialize {} | ||
|
||
#[const_trait] | ||
trait Foo {} | ||
|
||
impl<T> const Foo for T {} | ||
|
||
impl<T> const Foo for T | ||
where | ||
T: ~const Specialize, | ||
{} | ||
|
||
#[const_trait] | ||
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() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts?