-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Add very basic "comptime" fn implementation #148820
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
base: main
Are you sure you want to change the base?
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
da63f2f to
785eebe
Compare
|
All reviews have been addressed |
|
☔ The latest upstream changes (presumably #149140) made this pull request unmergeable. Please resolve the merge conflicts. |
…d in comptime fn
…rgetted at what it does
|
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. |
| } | ||
| } | ||
| } else if ccx.tcx.constness(callee) != hir::Constness::Const { | ||
| } else if ccx.tcx.constness(callee) != hir::Constness::Maybe { |
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 a bit sus
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.
What part? The rename or the inequality?
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.
The inequality. I feel like this should be if == hir::Constness::NotConst?
| pub fn is_const(&self) -> bool { | ||
| matches!(self.constness, Constness::Const) | ||
| matches!(self.constness, Constness::Maybe) | ||
| } |
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.
I think we might want to audit usages of this. Leave a FIXME?
| if should_encode_constness(def_kind) { | ||
| let constness = self.tcx.constness(def_id); | ||
| self.tables.constness.set(def_id.index, constness); | ||
| self.tables.constness.set(def_id.index, constness) |
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.
? (please revert)
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.
Rebase fallout
| ( Maybe ) | ||
| ( Never ) | ||
| ( Always ) |
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.
| ( Maybe ) | |
| ( Never ) | |
| ( Always ) | |
| ( Maybe ) | |
| ( Never ) | |
| ( Always ) |
| 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 }; |
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.
I feel like we shouldn't add another variant to hir::Constness given how this changes a lot of other things which might be a headache to update if we were to add comptime impls in the future, etc. Would it make sense to keep the hir:Constness::{,Not}Const, and extract the comptime enum specifically to a new enum? Might be a bit more work to do but worth it IMO, and might be better than doing it in a followup.
|
Reminder, once the PR becomes ready for a review, use |
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