2022-11-09 11:21:42 -06:00
|
|
|
// unit-test: DataflowConstProp
|
|
|
|
|
2023-01-21 16:28:54 -06:00
|
|
|
#![feature(custom_mir, core_intrinsics, rustc_attrs)]
|
|
|
|
|
|
|
|
use std::intrinsics::mir::*;
|
|
|
|
|
2023-05-13 09:19:31 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2022-11-09 11:21:42 -06:00
|
|
|
enum E {
|
|
|
|
V1(i32),
|
|
|
|
V2(i32)
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:28:54 -06:00
|
|
|
// EMIT_MIR enum.simple.DataflowConstProp.diff
|
|
|
|
fn simple() {
|
2022-11-09 11:21:42 -06:00
|
|
|
let e = E::V1(0);
|
|
|
|
let x = match e { E::V1(x) => x, E::V2(x) => x };
|
|
|
|
}
|
2023-01-21 16:28:54 -06:00
|
|
|
|
2023-05-13 07:30:40 -05:00
|
|
|
// EMIT_MIR enum.constant.DataflowConstProp.diff
|
|
|
|
fn constant() {
|
|
|
|
const C: E = E::V1(0);
|
|
|
|
let e = C;
|
|
|
|
let x = match e { E::V1(x) => x, E::V2(x) => x };
|
|
|
|
}
|
|
|
|
|
2023-05-13 09:19:31 -05:00
|
|
|
// EMIT_MIR enum.statics.DataflowConstProp.diff
|
|
|
|
fn statics() {
|
|
|
|
static C: E = E::V1(0);
|
|
|
|
let e = C;
|
|
|
|
let x = match e { E::V1(x) => x, E::V2(x) => x };
|
|
|
|
|
|
|
|
static RC: &E = &E::V2(4);
|
|
|
|
let e = RC;
|
|
|
|
let x = match e { E::V1(x) => x, E::V2(x) => x };
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:28:54 -06:00
|
|
|
#[rustc_layout_scalar_valid_range_start(1)]
|
|
|
|
#[rustc_nonnull_optimization_guaranteed]
|
|
|
|
struct NonZeroUsize(usize);
|
|
|
|
|
|
|
|
// EMIT_MIR enum.mutate_discriminant.DataflowConstProp.diff
|
|
|
|
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
|
|
|
|
fn mutate_discriminant() -> u8 {
|
|
|
|
mir!(
|
|
|
|
let x: Option<NonZeroUsize>;
|
|
|
|
{
|
|
|
|
SetDiscriminant(x, 1);
|
|
|
|
// This assignment overwrites the niche in which the discriminant is stored.
|
|
|
|
place!(Field(Field(Variant(x, 1), 0), 0)) = 0_usize;
|
|
|
|
// So we cannot know the value of this discriminant.
|
|
|
|
let a = Discriminant(x);
|
|
|
|
match a {
|
|
|
|
0 => bb1,
|
|
|
|
_ => bad,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bb1 = {
|
|
|
|
RET = 1;
|
|
|
|
Return()
|
|
|
|
}
|
|
|
|
bad = {
|
|
|
|
RET = 2;
|
|
|
|
Unreachable()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-18 03:35:52 -06:00
|
|
|
// EMIT_MIR enum.multiple.DataflowConstProp.diff
|
|
|
|
fn multiple(x: bool, i: u8) {
|
|
|
|
let e = if x {
|
|
|
|
Some(i)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
// The dataflow state must have:
|
|
|
|
// discriminant(e) => Top
|
|
|
|
// (e as Some).0 => Top
|
|
|
|
let x = match e { Some(i) => i, None => 0 };
|
|
|
|
// Therefore, `x` should be `Top` here, and no replacement shall happen.
|
|
|
|
let y = x;
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:28:54 -06:00
|
|
|
fn main() {
|
|
|
|
simple();
|
2023-05-13 07:30:40 -05:00
|
|
|
constant();
|
2023-05-13 09:19:31 -05:00
|
|
|
statics();
|
2023-01-21 16:28:54 -06:00
|
|
|
mutate_discriminant();
|
2023-02-18 03:35:52 -06:00
|
|
|
multiple(false, 5);
|
2023-01-21 16:28:54 -06:00
|
|
|
}
|