-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Comptime inherent impls #157824
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
Merged
Merged
Comptime inherent impls #157824
Changes from all commits
Commits
Show all changes
3 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| #![feature(rustc_attrs, stmt_expr_attributes)] | ||
|
|
||
| const _: () = { | ||
| let f = #[rustc_comptime] | ||
| //~^ ERROR: `#[rustc_comptime]` attribute cannot be used on closures | ||
| || (); | ||
|
|
||
| // FIXME(comptime): closures should work, too. | ||
| f(); | ||
| //~^ ERROR: cannot call non-const closure in constants | ||
| }; | ||
|
|
||
| fn main() {} |
This file contains hidden or 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: `#[rustc_comptime]` attribute cannot be used on closures | ||
| --> $DIR/comptime_closure.rs:4:13 | ||
| | | ||
| LL | let f = #[rustc_comptime] | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: `#[rustc_comptime]` can be applied to functions, inherent impl blocks, and inherent methods | ||
|
|
||
| error[E0015]: cannot call non-const closure in constants | ||
| --> $DIR/comptime_closure.rs:9:5 | ||
| | | ||
| LL | f(); | ||
| | ^^^ | ||
| | | ||
| = note: closures need an RFC before allowed to be called in constants | ||
| = note: calls in constants are limited to constant functions, tuple structs and tuple variants | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0015`. |
This file contains hidden or 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,35 @@ | ||
| #![feature(rustc_attrs, const_trait_impl)] | ||
|
|
||
| const trait Foo { | ||
| fn foo(&self); | ||
|
|
||
| fn bar(&self) {} | ||
| } | ||
|
|
||
| struct Bar; | ||
|
|
||
| #[rustc_comptime] | ||
| impl Bar { | ||
| fn boo(&self) {} | ||
| } | ||
|
|
||
| #[rustc_comptime] | ||
| //~^ ERROR: cannot be used on trait impl | ||
| impl Foo for Bar { | ||
| fn foo(&self) { | ||
| comptime_fn(); | ||
| } | ||
| } | ||
|
|
||
| #[rustc_comptime] | ||
| fn comptime_fn() {} | ||
|
|
||
| const _: () = { | ||
| Bar.boo(); | ||
| Bar.foo(); | ||
| //~^ ERROR: `Bar: const Foo` is not satisfied | ||
| Bar.bar(); | ||
| //~^ ERROR: `Bar: const Foo` is not satisfied | ||
| }; | ||
|
|
||
| fn main() {} |
This file contains hidden or 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,33 @@ | ||
| error: `#[rustc_comptime]` attribute cannot be used on trait impl blocks | ||
| --> $DIR/comptime_impl.rs:16:1 | ||
| | | ||
| LL | #[rustc_comptime] | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: `#[rustc_comptime]` can be applied to functions and inherent impl blocks | ||
|
|
||
| error[E0277]: the trait bound `Bar: const Foo` is not satisfied | ||
| --> $DIR/comptime_impl.rs:29:9 | ||
| | | ||
| LL | Bar.foo(); | ||
| | ^^^ | ||
| | | ||
| help: make the `impl` of trait `Foo` `const` | ||
| | | ||
| LL | impl const Foo for Bar { | ||
| | +++++ | ||
|
|
||
| error[E0277]: the trait bound `Bar: const Foo` is not satisfied | ||
| --> $DIR/comptime_impl.rs:31:9 | ||
| | | ||
| LL | Bar.bar(); | ||
| | ^^^ | ||
| | | ||
| help: make the `impl` of trait `Foo` `const` | ||
| | | ||
| LL | impl const Foo for Bar { | ||
| | +++++ | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0277`. |
This file contains hidden or 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,17 @@ | ||
| #![feature(rustc_attrs)] | ||
|
|
||
| struct Bar; | ||
|
|
||
| #[rustc_comptime] | ||
| impl Bar { | ||
| fn boo(&self) {} | ||
| } | ||
|
|
||
| const _: () = { | ||
| Bar.boo(); | ||
| }; | ||
|
|
||
| fn main() { | ||
| Bar.boo(); | ||
| //~^ ERROR: comptime fns can only be called at compile time | ||
| } |
This file contains hidden or 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: comptime fns can only be called at compile time | ||
| --> $DIR/comptime_method.rs:15:5 | ||
| | | ||
| LL | Bar.boo(); | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
This file contains hidden or 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(rustc_attrs, const_trait_impl)] | ||
|
|
||
| struct Bar<T>(T); | ||
|
|
||
| const trait Trait { | ||
| fn method(&self) {} | ||
| } | ||
|
|
||
| #[rustc_comptime] | ||
| impl<T: const Trait> Bar<T> { | ||
| fn boo(&self) { | ||
| self.0.method() | ||
| } | ||
| } | ||
|
|
||
| const impl Trait for () {} | ||
|
|
||
| const _: () = { | ||
| Bar(()).boo(); | ||
| }; | ||
|
|
||
| fn main() {} |
This file contains hidden or 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 @@ | ||
| #![feature(rustc_attrs, const_trait_impl, trait_alias)] | ||
|
|
||
| #[rustc_comptime] | ||
| //~^ ERROR: `#[rustc_comptime]` attribute cannot be used on traits | ||
| trait Trait { | ||
| fn method(&self) {} | ||
| } | ||
|
|
||
| const impl Trait for () {} | ||
|
|
||
| #[rustc_comptime] | ||
| //~^ ERROR: `#[rustc_comptime]` attribute cannot be used on trait impl | ||
| impl Trait for u32 { | ||
| fn method(&self) { | ||
| comptime_fn(); | ||
| } | ||
| } | ||
|
|
||
| #[rustc_comptime] | ||
| fn comptime_fn() {} | ||
|
|
||
| #[rustc_comptime] | ||
| //~^ ERROR: `#[rustc_comptime]` attribute cannot be used on trait aliases | ||
| trait TraitAlias = const Trait; | ||
|
|
||
| #[rustc_comptime] | ||
| fn func<T: const TraitAlias>(t: &T) { | ||
| t.method() | ||
| //~^ ERROR: cannot call non-const method `<T as Trait>::method` in constants | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or 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,35 @@ | ||
| error: `#[rustc_comptime]` attribute cannot be used on traits | ||
| --> $DIR/comptime_trait.rs:3:1 | ||
| | | ||
| LL | #[rustc_comptime] | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: `#[rustc_comptime]` can be applied to functions and inherent impl blocks | ||
|
|
||
| error: `#[rustc_comptime]` attribute cannot be used on trait impl blocks | ||
| --> $DIR/comptime_trait.rs:11:1 | ||
| | | ||
| LL | #[rustc_comptime] | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: `#[rustc_comptime]` can be applied to functions and inherent impl blocks | ||
|
|
||
| error: `#[rustc_comptime]` attribute cannot be used on trait aliases | ||
| --> $DIR/comptime_trait.rs:22:1 | ||
| | | ||
| LL | #[rustc_comptime] | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: `#[rustc_comptime]` can be applied to functions and inherent impl blocks | ||
|
|
||
| error[E0015]: cannot call non-const method `<T as Trait>::method` in constants | ||
| --> $DIR/comptime_trait.rs:28:7 | ||
| | | ||
| LL | t.method() | ||
| | ^^^^^^^^ | ||
| | | ||
| = note: calls in constants are limited to constant functions, tuple structs and tuple variants | ||
|
|
||
| error: aborting due to 4 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0015`. |
This file contains hidden or 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
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.
It might make sense to clarify in a comment here that
ComptimeParserundercompiler/rustc_attr_parsing/src/attributes/semantics.rsactually controls where a Comptime constness is actually allowed.