-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add #[default_method_body_is_const] #86857
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2db927d
Add #[default_method_body_is_const]
fee1-dead 3660a4e
Applied suggestions
fee1-dead d8d4cc3
Treat trait fns marked with the attr as const
fee1-dead 56d79ad
Skip check for calling functions in same trait
fee1-dead 032cbe4
Check if the attribute is applied correctly
fee1-dead 89d190f
Add impl_constness query
fee1-dead 27e863b
functions marked with attr are not const
fee1-dead 554fad7
Permit calls to default const fns of impl const
fee1-dead 5e695bb
Update CTFE to allow fns marked with the attr
fee1-dead a79e08c
Update tests
fee1-dead 88b29f5
Test for misusing attribute
fee1-dead 7c9e214
Update `DepNode`'s size
fee1-dead 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
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
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
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
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
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
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
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 @@ | ||
#![feature(const_trait_impl)] | ||
#![allow(incomplete_features)] | ||
|
||
#[default_method_body_is_const] //~ ERROR attribute should be applied | ||
trait A { | ||
#[default_method_body_is_const] //~ ERROR attribute should be applied | ||
fn no_body(self); | ||
|
||
#[default_method_body_is_const] | ||
fn correct_use(&self) {} | ||
} | ||
|
||
#[default_method_body_is_const] //~ ERROR attribute should be applied | ||
fn main() {} |
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,32 @@ | ||
error: attribute should be applied to a trait method with body | ||
--> $DIR/attr-misuse.rs:4:1 | ||
| | ||
LL | #[default_method_body_is_const] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | / trait A { | ||
LL | | #[default_method_body_is_const] | ||
LL | | fn no_body(self); | ||
LL | | | ||
LL | | #[default_method_body_is_const] | ||
LL | | fn correct_use(&self) {} | ||
LL | | } | ||
| |_- not a trait method or missing a body | ||
|
||
error: attribute should be applied to a trait method with body | ||
--> $DIR/attr-misuse.rs:13:1 | ||
| | ||
LL | #[default_method_body_is_const] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | fn main() {} | ||
| ------------ not a trait method or missing a body | ||
|
||
error: attribute should be applied to a trait method with body | ||
--> $DIR/attr-misuse.rs:6:5 | ||
| | ||
LL | #[default_method_body_is_const] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | fn no_body(self); | ||
| ----------------- not a trait method or missing a body | ||
|
||
error: aborting due to 3 previous errors | ||
|
31 changes: 31 additions & 0 deletions
31
src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.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,31 @@ | ||
#![feature(const_trait_impl)] | ||
#![feature(const_fn_trait_bound)] // FIXME is this needed? | ||
#![allow(incomplete_features)] | ||
|
||
trait ConstDefaultFn: Sized { | ||
fn b(self); | ||
|
||
#[default_method_body_is_const] | ||
fn a(self) { | ||
self.b(); | ||
} | ||
} | ||
|
||
struct NonConstImpl; | ||
struct ConstImpl; | ||
|
||
impl ConstDefaultFn for NonConstImpl { | ||
fn b(self) {} | ||
} | ||
|
||
impl const ConstDefaultFn for ConstImpl { | ||
fn b(self) {} | ||
} | ||
|
||
const fn test() { | ||
NonConstImpl.a(); | ||
//~^ ERROR calls in constant functions are limited to constant functions, tuple structs and tuple variants | ||
ConstImpl.a(); | ||
} | ||
|
||
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.
this is ok for now, it's exactly what i meant. in the future we should figure out whether we can "just" generate the appropriate bounds in typeck. basically i think in a perfect world, we'd get a "trait not implemented" error, with the nonconst impl listed and suggesting to make it const.