-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Adding generics affect whether code has UB or not, according to Miri #146803
Copy link
Copy link
Closed
Labels
A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsRelating to exhaustiveness / usefulness checking of patternsA-miriArea: The miri toolArea: The miri toolA-patternsRelating to patterns and pattern matchingRelating to patterns and pattern matchingC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem teamRelevant to the opsem team
Metadata
Metadata
Assignees
Labels
A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsRelating to exhaustiveness / usefulness checking of patternsA-miriArea: The miri toolArea: The miri toolA-patternsRelating to patterns and pattern matchingRelating to patterns and pattern matchingC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem teamRelevant to the opsem team
Type
Fields
Give feedbackNo fields configured for issues without a type.
I tried this code:
I expected the
concreteandgeneric::<Never>calls to behave identically. Instead, Miri detects UB only for thegeneric::<Never>call:It seems that, in
generic,Tis treated as inhabited, even if theTis later set toNever(which makes the type uninhabited inconcrete). Whether theThing::Nevervariant is inhabited then affects whether the enum discriminant is checked in the MIR.Note that, when I tried executing the program (in either debug mode or release mode), it printed
Inhabited, then printedNever. That is, the difference between the two functions can actually be observed outside of Miri.Probably related to #142394 and #146590, which involves uninhabited types affecting MIR generation.
See also #146430, where generics can affect MIR-building.
Meta
Reproducible on the playground with rust version
1.92.0-nightly (2025-09-19 0be8e16088894483a701)and Miri version0.1.0 (2025-09-19 0be8e16088)