-
Notifications
You must be signed in to change notification settings - Fork 13k
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
opportunistically evaluate constants while populating mir::Body::required_consts
#71802
Comments
Ideally that would actually dispatch through the Same goes for the const error reporting that codegen is doing, FWIW. Which makes me wonder, can we move this impl block to |
hmm right, I think nothing from librustc uses this anymore. We can't move the impl block, but we can make the functions free functions. |
Yeah something like that. So we also use this issue to track cleaning up the error reporting story more generally? If we also include cleaning up this then I think we can close #67191, AFAIK that's all that's left. |
mir::Body::required_consts
mir::Body::required_consts
+ CTFE error cleanup
rust-lang/miri#1382 is also related, this is Miri lacking the code that codegen has to properly report post-monomorphization errors. |
mir::Body::required_consts
+ CTFE error cleanupmir::Body::required_consts
I realized that the cleanup here is not actually made easier by opportunistically evaluating things, because promoteds can still fail to evaluate later. Thus I moved the cleanup to a separate issue: #75461. |
Evaluating and replacing constants in MIR before promotion interacts with const qualification. Evaluated constants are currently qualified based solely on their type, which is less precise than value based analysis that might be used otherwise. For example, promotion fails for use std::cell::Cell;
const X: Option<Cell<i32>> = None;
fn main() {
let x: &'static _ = &X;
} |
Your example already compiles. We compute the qualification for every constant based on its body and then use that cached qualification to decide whether to promote references to it. Unless the unevaluated constant is too generic to get its true body (for associated consts of generic parameters), then we indeed just look at the type. |
Evaluated constants are treated as opaque so the example stops to compile. This seems almost contradictory that we perform value based analysis only when we don't know the precise value yet. |
Oh wow. Now I get what you mean. Yea... I see how that would be problematic |
We can evaluate all constants that do not depend on generic parameters in
rust/src/librustc_mir/transform/required_consts.rs
Line 19 in 0b95879
We make that a
MutVisitor
that evaluates all constants that it can and replaces them in-place and only adds those that it couldn't evaluate to therequired_const
list.This way we never have to evaluate another constant in the entire MIR optimization pipeline, because we know none can be evaluated (modulo those that we already evaluate for merging
required_const
s during inlining.cc @rust-lang/wg-mir-opt I think I like having the above invariant. It will make working with constants in MIR much simpler and make any kind of error handling that would have to be implemented unified into a single location.
The text was updated successfully, but these errors were encountered: