-
Notifications
You must be signed in to change notification settings - Fork 13.3k
exhaustiveness checker mishandles const reference to ADT in match pattern #53708
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
cc @varkor you know this code very well. any pointers? |
Okay, this is actually more interesting than first appears from the example: the matching code dealing with constants behind references is completely broken. fn main() {
const C: &bool = &true;
let x = match C {
C => "C",
&true => "true",
&false => "false",
};
println!("{}", x);
} If you miss off the |
I think the problem here is this: rust/src/librustc_mir/hair/pattern/_match.rs Lines 209 to 221 in af2be23
When we encounter a constant, we try to turn it into a pattern, but the handling for references is wrong. We convert it into a deref pattern, but the underlying constant value it refers to still has type &T (and corresponding pointer value), rather than T .I.e. the box PatternKind::Constant { value: value.clone() } line is incorrect, and should be referring to a dereferenced value instead.
@oli-obk: is there a way to dereference a |
Just got it in my app. Is there a workaround or matching with constants is just broken for now? |
@artemshein: I would try to avoid using constants in patterns right now; sorry – I haven't got around to fixing this yet. |
Get rid of the fake stack frame for reading from constants r? @RalfJung fixes the ice in rust-lang#53708 but still keeps around the wrong "non-exhaustive match" error cc @varkor
Get rid of the fake stack frame for reading from constants r? @RalfJung fixes the ice in rust-lang#53708 but still keeps around the wrong "non-exhaustive match" error cc @varkor
So, there's something that can be done, which is calling
I do not know if this is correct though. |
We don't want to report an error on this code. We should be accepting it as correct |
@oli-obk I believe we should be accepting it as correct, but emitting the E0277 instead of an ICE can be a stop gap in the meantime to allow my current Edit: Scratch that, changing the code to be
If that is the case, then the same error needs to be emitted for |
I think I figured how to correctly fix the ICE (by emitting the error in my previous comment) but the exhaustiveness check is still broken. |
won't the ICE reoccur if you add the suggested derives? |
In the linked commit I added a test for the struct with the derives and otherwise unchanged and we continue to get the uncovered patterns error, but otherwise things look fine. |
triage: P-medium. |
This appears to be fixed on nightly, probably due to #70743 |
(If there's already a test for this case, feel free to close again: I just want to be sure.) |
This issue was accidentally fixed recently, probably by rust-lang#70743
There wasn't. I just added one in #78072 |
Update from @pnkfelix:
Our behavior on this test case (playpen) has changed, but it is still not correct.
emits error:
If you work-around the exhaustiveness checker bug by e.g. adding an unreachable arm, then the compiler accepts the code (and as far as I can tell, assigns it the correct runtime semantics).
Original bug report follows:
gives:
error: internal compiler error: librustc_mir/hair/pattern/_match.rs:432: bad constructor ConstantValue(Const { ty: &S, val: Scalar(Ptr(Pointer { alloc_id: AllocId(1), offset: Size { raw: 0 } })) }) for adt S
The relevant stack is:
It looks like this will take some effort to fix, I patched
constructor_sub_pattern_tys
to returnvec![]
, but then rustc erroneously reportserror[E0004]: non-exhaustive patterns: '&S' not covered
and with an enum:
it hits:
error: internal compiler error: librustc\traits\codegen\mod.rs:169: Encountered errors '[FulfillmentError(Obligation(predicate=Binder(TraitPredicate(<E as std::cmp::PartialEq>)),depth=1),Unimplemented)]' resolving bounds after type-checking
The text was updated successfully, but these errors were encountered: