Skip to content
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 a HIR pass to check consts for if, loop, etc. #66170

Merged
merged 14 commits into from
Nov 13, 2019

Conversation

ecstatic-morse
Copy link
Contributor

@ecstatic-morse ecstatic-morse commented Nov 6, 2019

Resolves #66125.

This PR adds a HIR pass to check for high-level control flow constructs that are forbidden in a const-context. The MIR const-checker is unable to provide good spans for these since they are lowered to control flow primitives (e.g., Goto and SwitchInt), and these often don't map back to the underlying statement as a whole. This PR is intended only to improve diagnostics once if and match become commonplace in constants (behind a feature flag). The MIR const-checker will continue to operate unchanged, and will catch anything this check might miss.

In this implementation, the HIR const-checking pass is run much earlier than the MIR one, so it will supersede any errors from the latter. I will need some mentoring if we wish to change this, since I'm not familiar with the diagnostics system. Moving this pass into the same phase as the MIR const-checker could also help keep backwards compatibility for items like const _: () = loop { break; };, which are currently (erroneously?) accepted by the MIR const-checker (see #62272).

r? @Centril
cc @eddyb (since they filed #62272)

@rust-highfive

This comment has been minimized.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 6, 2019
@ecstatic-morse

This comment has been minimized.

@eddyb
Copy link
Member

eddyb commented Nov 6, 2019

cc @rust-lang/compiler I was hoping we would implement const-checking for arbitrary CFGs and never have to deal with a HIR check on constants again, but I guess I was wrong.

@rust-highfive

This comment has been minimized.

Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checker code looks good aside from some nits and some thoughts for the future (cleanup tasks mostly unrelated to the PR but which I noticed along the way).

src/librustc_passes/error_codes.rs Outdated Show resolved Hide resolved
src/librustc_passes/error_codes.rs Outdated Show resolved Hide resolved
src/librustc/hir/map/mod.rs Outdated Show resolved Hide resolved
@@ -329,6 +329,11 @@ rustc_queries! {
desc { |tcx| "checking for unstable API usage in {}", key.describe_as_module(tcx) }
}

/// Checks the const bodies in the module for illegal operations (e.g. `if` or `loop`).
query check_mod_const_bodies(key: DefId) -> () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: I noticed that the categorization in this module wrt. Other, TypeChecking, and friends is rather messy and there doesn't seem to be a lot of logic to it? -- Maybe another C-cleanup issue...? (cc @Zoxc & @nikomatsakis)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this categorization was added as a very coarse grouping for profiling purposes IIRC, we can certainly clean things up here. cc @wesleywiser

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanups wrt categorization are definitely appreciated. I did the initial pass ~1yr ago and I don't think they've changed substantially since that time. Feel free to assign me to any such cleanup PRs.

src/librustc_passes/check_const.rs Outdated Show resolved Hide resolved
src/librustc_passes/check_const.rs Outdated Show resolved Hide resolved
src/librustc_passes/check_const.rs Outdated Show resolved Hide resolved
src/librustc_passes/check_const.rs Outdated Show resolved Hide resolved
src/librustc_passes/check_const.rs Outdated Show resolved Hide resolved
@Centril
Copy link
Contributor

Centril commented Nov 7, 2019

In this implementation, the HIR const-checking pass is run much earlier than the MIR one, so it will supersede any errors from the latter. I will need some mentoring if we wish to change this, since I'm not familiar with the diagnostics system. Moving this pass into the same phase as the MIR const-checker could also help keep backwards compatibility for items like const _: () = loop { break; };, which are currently (erroneously?) accepted by the MIR const-checker (see #62272).

I wouldn't expend any energy on making #62272 work; it's not very useful in any case and was never intended to be allowed on stable other than as a side-effect of how the compiler is structured. The check as written in this PR is good. :)

@Centril Centril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 7, 2019
@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Nov 7, 2019

Okay, I fixed most of @Centril's suggestions, blessed the back-compat breakages, and added an explanation to them. Let me know if you're okay with the new error messages.

This should pass CI now. I'd like people to weigh in on where to put this in the compilation pipeline; I think it should go alongside the MIR const-checker if possible. I'll also look into checking array length expressions. (done)

@Centril
Copy link
Contributor

Centril commented Nov 7, 2019

On my end, the code & diagnostics look good, so r=me on that aspect when you feel comfortable.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@@ -13,7 +11,6 @@ fn set_editor(_: Value) {}

fn main() {
let settings_data = from_string(settings_dir);
//~^ ERROR cannot move out of static item `settings_dir` [E0507]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that this error no longer occurs. I believe it's simply because the compiler stops working before the move checker runs?


fn main() {
[FOO; { let x; loop { x = 5; break; } x }];
[FOO; { let x; loop { x = 5; break; } x }]; //~ ERROR `loop` is not allowed in a `const`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we feel about the term const for an array initializer? Is there a preexisting convention here? It's pretty easy to change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @estebank has thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer us to be correct mentioning the array length is a const context, but this should be ok for now, we have plenty of other places where we are inconsistent about calling this out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, also, I think const generic arguments are also anon_const, so this isn't that easy. Probably the right way is to add an AnonConstKind to AnonConst in hir/mod.rs. With that in mind, I'm inclined to fix this later :).

@ecstatic-morse
Copy link
Contributor Author

I've rebased to clean up the history a bit and (hopefully) blessed the remaining tests. I've left some comments where the new error messages aren't a clear improvement.

This is still waiting for @rust-lang/compiler to weigh in on whether breaking #62272 is okay. It might need an FCP/crater run?

Also, I would still like for someone to review the relative order of the check_mod_const_bodies query. It would be nice run this at the same time as the MIR const-checker instead of superseding it, so we could leave the min_const_fn tests unchanged. We could then change the if/match errors in the MIR const-checker to delay_span_bugs (I think).

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

Control-flow expressions are not allowed inside a const context.

At the moment, `if` and `match`, as well as the looping constructs `for`,
`while`, and `loop`, are forbidden inside a `const`, `static`, or `const fn`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon re-reading this it occurred to me that we could link to the tracking issues... should we perhaps do that?
cc @estebank @oli-obk

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do this in a follow-up PR since the bulk of this one is ready.

@Centril
Copy link
Contributor

Centril commented Nov 8, 2019

Also, I would still like for someone to review the relative order of the check_mod_const_bodies query. It would be nice run this at the same time as the MIR const-checker instead of superseding it, so we could leave the min_const_fn tests unchanged. We could then change the if/match errors in the MIR const-checker to delay_span_bugs (I think).

This part is unfortunately out of my expertise to mentor; I'll defer to folks on T-Compiler.

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Nov 13, 2019

Once again broken by a refactoring. @Centril can I make this p=1? I guess it won't matter since this is old enough to be at the top of the queue now.

@bors r=Centril,oli-obk

@bors
Copy link
Contributor

bors commented Nov 13, 2019

📌 Commit 7552bd6 has been approved by Centril,oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 13, 2019
@bors
Copy link
Contributor

bors commented Nov 13, 2019

⌛ Testing commit 7552bd6 with merge ded5ee0...

bors added a commit that referenced this pull request Nov 13, 2019
Add a HIR pass to check consts for `if`, `loop`, etc.

Resolves #66125.

This PR adds a HIR pass to check for high-level control flow constructs that are forbidden in a const-context. The MIR const-checker is unable to provide good spans for these since they are lowered to control flow primitives (e.g., `Goto` and `SwitchInt`), and these often don't map back to the underlying statement as a whole. This PR is intended only to improve diagnostics once `if` and `match` become commonplace in constants (behind a feature flag). The MIR const-checker will continue to operate unchanged, and will catch anything this check might miss.

In this implementation, the HIR const-checking pass is run much earlier than the MIR one, so it will supersede any errors from the latter. I will need some mentoring if we wish to change this, since I'm not familiar with the diagnostics system. Moving this pass into the same phase as the MIR const-checker could also help keep backwards compatibility for items like `const _: () = loop { break; };`, which are currently (erroneously?) accepted by the MIR const-checker (see #62272).

r? @Centril
cc @eddyb (since they filed #62272)
@bors
Copy link
Contributor

bors commented Nov 13, 2019

☀️ Test successful - checks-azure
Approved by: Centril,oli-obk
Pushing ded5ee0 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 13, 2019
@bors bors merged commit 7552bd6 into rust-lang:master Nov 13, 2019
@rust-lang rust-lang deleted a comment from bors Nov 13, 2019
@rust-lang rust-lang deleted a comment from bors Nov 13, 2019
@rust-lang rust-lang deleted a comment from bors Nov 13, 2019
tmandry added a commit to tmandry/rust that referenced this pull request Nov 14, 2019
…msg, r=estebank

Link to tracking issue in HIR const-check error code description

This is a follow up to rust-lang#66170 that addresses [this comment](rust-lang#66170 (comment)).

As an aside, the only other error code whose description uses the phrase "tracking issue" is `E0202`. We may want to come up with standards around this, especially since error codes are now centralized and easier to keep track of (thanks @GuillaumeGomez).

r? @estebank (since they +1'ed the comment)
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Nov 15, 2019
…msg, r=estebank

Link to tracking issue in HIR const-check error code description

This is a follow up to rust-lang#66170 that addresses [this comment](rust-lang#66170 (comment)).

As an aside, the only other error code whose description uses the phrase "tracking issue" is `E0202`. We may want to come up with standards around this, especially since error codes are now centralized and easier to keep track of (thanks @GuillaumeGomez).

r? @estebank (since they +1'ed the comment)
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Nov 15, 2019
…msg, r=estebank

Link to tracking issue in HIR const-check error code description

This is a follow up to rust-lang#66170 that addresses [this comment](rust-lang#66170 (comment)).

As an aside, the only other error code whose description uses the phrase "tracking issue" is `E0202`. We may want to come up with standards around this, especially since error codes are now centralized and easier to keep track of (thanks @GuillaumeGomez).

r? @estebank (since they +1'ed the comment)
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Nov 15, 2019
…msg, r=estebank

Link to tracking issue in HIR const-check error code description

This is a follow up to rust-lang#66170 that addresses [this comment](rust-lang#66170 (comment)).

As an aside, the only other error code whose description uses the phrase "tracking issue" is `E0202`. We may want to come up with standards around this, especially since error codes are now centralized and easier to keep track of (thanks @GuillaumeGomez).

r? @estebank (since they +1'ed the comment)
@ecstatic-morse ecstatic-morse deleted the hir-const-check branch October 6, 2020 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errors for if and match in a const are not descriptive
8 participants