-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Check discriminant during const eval #73146
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
Comments
The generator MIR does this intentionally. Situations can arise where a piece of code won't know which variant a generator is in, but does know it's valid to project through a downcast to get to a particular field. That's because the field exists in multiple variants, and is in the same location regardless of which variant you access it through. Example: let _ = || {
let mut count = 0usize;
loop {
count += 1; // <-- What variant should we use?
if count % 2 == 0 {
yield 2; // Suspend0
}
yield count; // Suspend1
}
} At the point where |
Also, I think MIR usually initializes fields of a variant before setting the discriminant, so we would have to reverse that. |
@Aaron1011 I am not sure I fully understand what you are suggesting to check here (and this two-line snippet is not very readable and about 4 times as wide as my screen^^). Is the proposal to alter the semantics of |
Yes, that's correct
My intention was for this to catch 'obviously wrong' downcasts, like |
IMO UB should be motivated by optimizations. Catching bugs in our MIR building is not a good enough reason to add a new class of UB. |
Given what I said above, I am in favor of closing this issue -- currently the intended semantics of Downcast clearly involve it being legal to downcast to "other" variants as long as you only access fields common to both the actual and the downcast-to variant. |
In #73137 (comment), an invalid
Downcast
is being generated for the generator state enum (the wrong variant is used). The MIR interpreter code doesn't actually verify that the expected and actual discriminants match when performing aDowncast
. While doing this would not have caught the previous issue, it would allow us to detect U.B. at the point where it actually occurs, rather than needing to wait for invalid data to be read due to the wrong downcast.This would require us to do several operations where we currently just change the layout of an
MPlaceTy
, so it might make sense to only run this check in Miri (not normal const-eval).The text was updated successfully, but these errors were encountered: