-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 ~const
bounds on trait assoc functions
#88418
Merged
bors
merged 2 commits into
rust-lang:master
from
fee1-dead-contrib:trait-assoc-tilde-const
Aug 31, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
41 changes: 41 additions & 0 deletions
41
src/test/ui/rfc-2632-const-trait-impl/trait-where-clause-run.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,41 @@ | ||
// run-pass | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(const_fn_trait_bound)] | ||
|
||
trait Bar { | ||
fn bar() -> u8; | ||
} | ||
|
||
trait Foo { | ||
#[default_method_body_is_const] | ||
fn foo() -> u8 where Self: ~const Bar { | ||
<Self as Bar>::bar() * 6 | ||
} | ||
} | ||
|
||
struct NonConst; | ||
struct Const; | ||
|
||
impl Bar for NonConst { | ||
fn bar() -> u8 { | ||
3 | ||
} | ||
} | ||
|
||
impl Foo for NonConst {} | ||
|
||
impl const Bar for Const { | ||
fn bar() -> u8 { | ||
4 | ||
} | ||
} | ||
|
||
impl const Foo for Const {} | ||
|
||
fn main() { | ||
const ANS1: u8 = Const::foo(); | ||
let ans2 = NonConst::foo(); | ||
|
||
assert_eq!(ANS1 + ans2, 42); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/ui/rfc-2632-const-trait-impl/trait-where-clause-self-referential.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,24 @@ | ||
// check-pass | ||
|
||
#![feature(const_trait_impl)] | ||
#![feature(const_fn_trait_bound)] | ||
|
||
trait Foo { | ||
fn bar() where Self: ~const Foo; | ||
} | ||
|
||
struct S; | ||
|
||
impl Foo for S { | ||
fn bar() {} | ||
} | ||
|
||
fn baz<T: Foo>() { | ||
T::bar(); | ||
} | ||
|
||
const fn qux<T: ~const Foo>() { | ||
T::bar(); | ||
} | ||
|
||
fn main() {} |
40 changes: 40 additions & 0 deletions
40
src/test/ui/rfc-2632-const-trait-impl/trait-where-clause.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,40 @@ | ||
#![feature(const_fn_trait_bound)] | ||
#![feature(const_trait_impl)] | ||
|
||
trait Bar {} | ||
|
||
trait Foo { | ||
fn a(); | ||
fn b() where Self: ~const Bar; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few more tests I think we should have:
|
||
fn c<T: ~const Bar>(); | ||
} | ||
|
||
const fn test1<T: ~const Foo + Bar>() { | ||
T::a(); | ||
T::b(); | ||
//~^ ERROR the trait bound | ||
T::c::<T>(); | ||
//~^ ERROR the trait bound | ||
} | ||
|
||
const fn test2<T: ~const Foo + ~const Bar>() { | ||
T::a(); | ||
T::b(); | ||
T::c::<T>(); | ||
} | ||
|
||
fn test3<T: Foo>() { | ||
T::a(); | ||
T::b(); | ||
//~^ ERROR the trait bound | ||
T::c::<T>(); | ||
//~^ ERROR the trait bound | ||
} | ||
|
||
fn test4<T: Foo + Bar>() { | ||
T::a(); | ||
T::b(); | ||
T::c::<T>(); | ||
} | ||
|
||
fn main() {} |
67 changes: 67 additions & 0 deletions
67
src/test/ui/rfc-2632-const-trait-impl/trait-where-clause.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,67 @@ | ||
error[E0277]: the trait bound `T: Bar` is not satisfied | ||
--> $DIR/trait-where-clause.rs:14:5 | ||
| | ||
LL | T::b(); | ||
| ^^^^ the trait `Bar` is not implemented for `T` | ||
| | ||
note: required by `Foo::b` | ||
--> $DIR/trait-where-clause.rs:8:5 | ||
| | ||
LL | fn b() where Self: ~const Bar; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: consider further restricting this bound | ||
| | ||
LL | const fn test1<T: ~const Foo + Bar + Bar>() { | ||
| +++++ | ||
|
||
error[E0277]: the trait bound `T: Bar` is not satisfied | ||
--> $DIR/trait-where-clause.rs:16:5 | ||
| | ||
LL | T::c::<T>(); | ||
| ^^^^^^^^^ the trait `Bar` is not implemented for `T` | ||
| | ||
note: required by `Foo::c` | ||
--> $DIR/trait-where-clause.rs:9:5 | ||
| | ||
LL | fn c<T: ~const Bar>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
help: consider further restricting this bound | ||
| | ||
LL | const fn test1<T: ~const Foo + Bar + Bar>() { | ||
| +++++ | ||
|
||
error[E0277]: the trait bound `T: Bar` is not satisfied | ||
--> $DIR/trait-where-clause.rs:28:5 | ||
| | ||
LL | T::b(); | ||
| ^^^^ the trait `Bar` is not implemented for `T` | ||
| | ||
note: required by `Foo::b` | ||
--> $DIR/trait-where-clause.rs:8:5 | ||
| | ||
LL | fn b() where Self: ~const Bar; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: consider further restricting this bound | ||
| | ||
LL | fn test3<T: Foo + Bar>() { | ||
| +++++ | ||
|
||
error[E0277]: the trait bound `T: Bar` is not satisfied | ||
--> $DIR/trait-where-clause.rs:30:5 | ||
| | ||
LL | T::c::<T>(); | ||
| ^^^^^^^^^ the trait `Bar` is not implemented for `T` | ||
| | ||
note: required by `Foo::c` | ||
--> $DIR/trait-where-clause.rs:9:5 | ||
| | ||
LL | fn c<T: ~const Bar>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
help: consider further restricting this bound | ||
| | ||
LL | fn test3<T: Foo + Bar>() { | ||
| +++++ | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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.
beautiful