Add very basic "comptime" fn implementation#148820
Add very basic "comptime" fn implementation#148820oli-obk wants to merge 7 commits intorust-lang:mainfrom
Conversation
|
Some changes occurred in compiler/rustc_attr_parsing Some changes occurred in compiler/rustc_passes/src/check_attr.rs Some changes occurred in compiler/rustc_hir/src/attrs Some changes occurred to the CTFE machinery Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt |
|
rustbot has assigned @JonathanBrouwer. Use |
This comment has been minimized.
This comment has been minimized.
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
This comment has been minimized.
This comment has been minimized.
|
Some changes occurred in src/tools/clippy cc @rust-lang/clippy Some changes occurred to constck cc @fee1-dead |
This comment has been minimized.
This comment has been minimized.
|
I have some review comments to address and some heavy rebasing to do :D |
| #[rustc_comptime] | ||
| fn always_const<T: const Trait>() { | ||
| T::method() | ||
| } |
There was a problem hiding this comment.
No Action Required: Makes me wish the current const modifier was actually named const?, to imply it can be called in comptime, and can be called at runtime. That way, const would mean must be called in comptime, and the absence of const means can't be called in comptime. It doesn't really matter, since this is a compiler feature, but it'd be nicer for users to understand what's going on.
| #[rustc_comptime] | ||
| const fn foo() {} | ||
| //~^ ERROR a function cannot be both `comptime` and `const` |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
It's actually easier this way for the compiler, because it's now trivial to reject [const] bounds
There was a problem hiding this comment.
It's also nowhere near the final syntax, just sth to enable the feature
There was a problem hiding this comment.
Ahh ok, I assumed this was mostly a stylistic choice. If it's more consistent with the rest of the compiler that makes sense.
| let num_args = fields.len(); | ||
| let func = | ||
| if context == hir::Constness::Const { called_in_const } else { called_at_rt }; | ||
| if context == hir::Constness::Maybe { called_in_const } else { called_at_rt }; |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment has been minimized.
This comment has been minimized.
01088b9 to
d092d4e
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…d in comptime fn
…rgetted at what it does
View all comments
Implements functions that cannot be called at runtime (and thus also not from normal const functions, as those could be called from runtime).
This is done via the internal attribute
rustc_comptimethat can be added to normal functions, turning them into compile-time-only functions.Because @fee1-dead and @compiler-errors did amazing work, we even get trait bounds that work inside comptime fns: via unconditionally-const
const Traitbounds.Use cases are
project goal issue: rust-lang/rust-project-goals#406
no tracking issue until we have a feature gate and some sort of syntax
cc @scottmcm as the T-lang goal champion