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 speculative const-checking and remove qualify_min_const_fn.rs #77128

Conversation

ecstatic-morse
Copy link
Contributor

@ecstatic-morse ecstatic-morse commented Sep 23, 2020

cc #76618

This removes qualify_min_const_fn.rs entirely. It remained after #76850 because it was still used for a clippy lint. To continue to support clippy, I added a speculative mode to the const-checker, which is exposed via the non_const_fn_could_be_made_stable_const_fn method.

r? @oli-obk

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 23, 2020
@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Sep 23, 2020

I haven't run the clippy tests locally (I'm not sure how), so this probably won't pass CI. I'm working on it.

Also, you might have noticed that I use early returns instead of else-if whenever possible. Usually I find this much more readable, but that might not be the case here.

Also removes some checks that were overzealous.
{
return;
}
},
FnKind::Closure(..) => return,
}

let mir = cx.tcx.optimized_mir(def_id);
let mir = cx.tcx.mir_const(WithOptConstParam::unknown(def_id)).borrow();
Copy link
Contributor

Choose a reason for hiding this comment

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

whoa, how does this work? Couldn't it already have been stolen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clippy tests don't run on PR CI, and I still can't run them locally due to some error about "two copies of the regex crate".

I don't know if clippy has a mechanism for ordering lints. It's not correct to const-check optimized MIR, since optimizations (especially drop elaboration) could have removed invalid operations. This was true for qualify_min_const_fn as well. Can someone from @rust-lang/clippy weigh in here?

Copy link
Contributor Author

@ecstatic-morse ecstatic-morse Sep 24, 2020

Choose a reason for hiding this comment

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

Okay, ./x.py clean seemed to work. Besides being incorrect, running const-checking on optimized MIR causes a bunch of assertions to fire, since we don't reason about promoteds inside const-checking. I guess we'll have to disable the lint until someone tells us how to order the execution of clippy lints.

Copy link
Contributor

Choose a reason for hiding this comment

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

clippy could override the query that steals mir_const and just not steal it. Right now we're doing a hacky thing (

// FIXME: #4825; This is required, because Clippy lints that are based on MIR have to be
) but maybe we could just inject a different query.

cc @rust-lang/clippy are you ok with turning off the "this could be const fn" lint until we figure out how to get it to use exactly the same analysis as the one used by rustc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This passes clippy tests locally now that I've commented out a single assertion. It was just the one ICE in every test. I think we could live with it (that assertion was more of a sanity check), but it doesn't feel great.

Copy link
Member

Choose a reason for hiding this comment

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

Clippy is tested by running ./x.py test src/tools/clippy

If it works like Miri, test --stage 0 should also work (and be much faster as rustc is built only once).

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 tried started with stage 0 but got the bug about two rlibs for regex. Can anyone reproduce? Doing a stage 1 build just to run clippy tests is pretty painful, especially since they don't run on CI.

Copy link
Contributor

Choose a reason for hiding this comment

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

the regex thing is a longstanding problem with compiletest-rs that we've not really been able to fix. I don't know if https://crates.io/crates/trybuild fares better, maybe we should try it out

Copy link
Member

Choose a reason for hiding this comment

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

Miri also uses compiletest and its stage 0 tests work fine...

Copy link
Contributor

Choose a reason for hiding this comment

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

in miri we don't use any crates in our unit tests I think

I really hate having to do this, since I don't think unstable compiler
APIs should have to work around misuse by external crates. However, this
is the only way this change will pass CI without a ton of modifications
to clippy and/or involvement of the clippy team.
@RalfJung
Copy link
Member

Cc @rust-lang/wg-const-eval

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Sep 25, 2020

Okay, so I'm gonna close this for now. I'll let someone from clippy move qualify_min_const_fn into tree and/or disable the lint, since I don't really know what's involved. When clippy figures out how to work around stolen queries, we can reopen it.

Also Clippy has set the mir-opt-flag to 0, so no MIR optimizations are run for Clippy lints.

@flip1995 This statement is misleading, and the misunderstanding underlying it will likely cause issues in the future, definitely with this lint and maybe with others. At mir-opt-level=0, we still run all the post_borrowck_cleanup passes, which remove dead code (SimplifyBranches, SimplifyCfg) and do drop elaboration, along with some other nontrivial stuff. We also do constant propagation at mir-opt-level=0, although this is more like a bug in rustc.

@RalfJung
Copy link
Member

Should we have an issue to track this pending cleanup?

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Sep 25, 2020

Added a note to #76618 (comment) and rewrote rust-lang/rust-clippy#6080.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants