-
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
Enable if
and match
in constants behind a feature flag
#66507
Conversation
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Added the feature gate label. @ecstatic-morse Could you please mark relevant past PRs that lead up to this one with that label as well? (It's very helpful for tracking implementation history for stabilization report purposes.) |
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 love how simple this is to do after all the refactorings and cleanups you did. Except for a diagnostic nit and some questions this looks ready to go to me
@ecstatic-morse this is really exciting! I still want to contribute tests but my laptop is currently broken so I'll be helping only sporadically. Where would I start? I see you added tests in |
05a67b6
to
5e7a552
Compare
I've rebased with @Centril's changes to the HIR const-checker applied. I now use |
Does this PR still count as WIP? Seems ready to me |
if
and match
in constants behind a feature flagif
and match
in constants behind a feature flag
@oli-obk Yeah, I think this is ready to go. I'd like to have a few more tests for interior mutability/ |
Oh, this will have to be merged after #66524. They shouldn't conflict though. |
@ecstatic-morse would be good to address #66507 (comment) first tho. |
☔ The latest upstream changes (presumably #66578) made this pull request unmergeable. Please resolve the merge conflicts. |
The `//[X]~` syntax filters errors for tests that are run across multiple cfgs with `// revisions:`. This commit extends that syntax to accept `//[X,Y]~`, which will match multiple cfgs to the same error annotation. This is functionally the same as writing two comments, `//[X]~` and `//[Y]~`, but can fit on a single line.
This test does not actually emit any warnings, since `#![allow(warnings)]` was specified. `compiletest` was erroneously ignoring `//~` tests and looking only for `//[X]~` ones. As a result of the changes in the previous commit, we now look for `//~` comments in incremental tests and expect them to appear in *all* revisions.
These are generated when matching on enum variants to extract the value within. We should have no problem evaluating these, but care should be taken that we aren't accidentally allowing some other operation.
cdabe87
to
3bea998
Compare
@@ -529,6 +529,9 @@ declare_features! ( | |||
/// Allows using the `#[register_attr]` attribute. | |||
(active, register_tool, "1.41.0", Some(66079), None), | |||
|
|||
/// Allows the use of `if` and `match` in constants. | |||
(active, const_if_match, "1.41.0", Some(49146), None), |
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.
Btw: a useful trick for the future, if you want to avoid rebasing due to this, is to move this before never_type_fallback
.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
This creates a new test directory, `ui/consts/control-flow` to hold tests related to control flow in a const context. It also blesses all existing tests with the new error messages, and adds new tests for the `const_if_match` feature.
3bea998
to
b09bb15
Compare
@oli-obk Can you take a final look at this when you have the time? The inside Rust post should be ready as well, but it would also benefit from a review. |
@bors r+ |
📌 Commit b09bb15 has been approved by |
Enable `if` and `match` in constants behind a feature flag This PR is an initial implementation of #49146. It introduces a `const_if_match` feature flag and does the following if it is enabled: - Allows `Downcast` projections, `SwitchInt` terminators and `FakeRead`s for matched places through the MIR const-checker. - Allows `if` and `match` expressions through the HIR const-checker. - Stops converting `&&` to `&` and `||` to `|` in `const` and `static` items. As a result, the following operations are now allowed in a const context behind the feature flag: - `if` and `match` - short circuiting logic operators (`&&` and `||`) - the `assert` and `debug_assert` macros (if the `const_panic` feature flag is also enabled) However, the following operations remain forbidden: - `while`, `loop` and `for` (see #52000) - the `?` operator (calls `From::from` on its error variant) - the `assert_eq` and `assert_ne` macros, along with their `debug` variants (calls `fmt::Debug`) This PR is possible now that we use dataflow for const qualification (see #64470 and #66385). r? @oli-obk cc @rust-lang/wg-const-eval @eddyb
☀️ Test successful - checks-azure |
Enable `loop` and `while` in constants behind a feature flag This PR is an initial implementation of rust-lang#52000. It adds a `const_loop` feature gate, which allows `while` and `loop` expressions through both HIR and MIR const-checkers if enabled. `for` expressions remain forbidden by the HIR const-checker, since they desugar to a call to `IntoIterator::into_iter`, which will be rejected anyways. `while` loops also require [`#![feature(const_if_match)]`](rust-lang#66507), since they have a conditional built into them. The diagnostics from the HIR const checker will suggest this to the user. r? @oli-obk cc @rust-lang/wg-const-eval
Enable `loop` and `while` in constants behind a feature flag This PR is an initial implementation of #52000. It adds a `const_loop` feature gate, which allows `while` and `loop` expressions through both HIR and MIR const-checkers if enabled. `for` expressions remain forbidden by the HIR const-checker, since they desugar to a call to `IntoIterator::into_iter`, which will be rejected anyways. `while` loops also require [`#![feature(const_if_match)]`](#66507), since they have a conditional built into them. The diagnostics from the HIR const checker will suggest this to the user. r? @oli-obk cc @rust-lang/wg-const-eval
With rust-lang/rust#66507 merged, `Prop::kind_flags` can be `const fn` and the `prop!` macro can use this method to get the kind flags for a given prop.
This PR is an initial implementation of #49146. It introduces a
const_if_match
feature flag and does the following if it is enabled:Downcast
projections,SwitchInt
terminators andFakeRead
s for matched places through the MIR const-checker.if
andmatch
expressions through the HIR const-checker.&&
to&
and||
to|
inconst
andstatic
items.As a result, the following operations are now allowed in a const context behind the feature flag:
if
andmatch
&&
and||
)assert
anddebug_assert
macros (if theconst_panic
feature flag is also enabled)However, the following operations remain forbidden:
while
,loop
andfor
(see Tracking issue for RFC 2344, "Allowloop
in constant evaluation" #52000)?
operator (callsFrom::from
on its error variant)assert_eq
andassert_ne
macros, along with theirdebug
variants (callsfmt::Debug
)This PR is possible now that we use dataflow for const qualification (see #64470 and #66385).
r? @oli-obk
cc @rust-lang/wg-const-eval @eddyb