forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#120882 - RalfJung:set-discriminant, r=compi…
…ler-errors interpret/write_discriminant: when encoding niched variant, ensure the stored value matches Cc rust-lang/unsafe-code-guidelines#487
- Loading branch information
Showing
6 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/tools/miri/tests/fail/enum-set-discriminant-niche-variant-wrong.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(custom_mir)] | ||
|
||
use std::intrinsics::mir::*; | ||
use std::num::NonZeroI32; | ||
|
||
// We define our own option type so that we can control the varian indices. | ||
#[allow(unused)] | ||
enum Option<T> { | ||
None, | ||
Some(T), | ||
} | ||
use Option::*; | ||
|
||
#[custom_mir(dialect = "runtime", phase = "optimized")] | ||
fn set_discriminant(ptr: &mut Option<NonZeroI32>) { | ||
mir! { | ||
{ | ||
// We set the discriminant to `Some`, which is a NOP since this is the niched variant. | ||
// However, the enum is actually encoding `None` currently! That's not good... | ||
SetDiscriminant(*ptr, 1); | ||
//~^ ERROR: trying to set discriminant of a Option<std::num::NonZero<i32>> to the niched variant, but the value does not match | ||
Return() | ||
} | ||
} | ||
} | ||
|
||
pub fn main() { | ||
let mut v = None; | ||
set_discriminant(&mut v); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/tools/miri/tests/fail/enum-set-discriminant-niche-variant-wrong.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: Undefined Behavior: trying to set discriminant of a Option<std::num::NonZero<i32>> to the niched variant, but the value does not match | ||
--> $DIR/enum-set-discriminant-niche-variant-wrong.rs:LL:CC | ||
| | ||
LL | SetDiscriminant(*ptr, 1); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ trying to set discriminant of a Option<std::num::NonZero<i32>> to the niched variant, but the value does not match | ||
| | ||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
= note: BACKTRACE: | ||
= note: inside `set_discriminant` at $DIR/enum-set-discriminant-niche-variant-wrong.rs:LL:CC | ||
note: inside `main` | ||
--> $DIR/enum-set-discriminant-niche-variant-wrong.rs:LL:CC | ||
| | ||
LL | set_discriminant(&mut v); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|