Rollup merge of #43650 - RalfJung:mir-validate, r=arielb1

test MIR validation statements in closures

r? @nikomatsakis
This commit is contained in:
Guillaume Gomez 2017-08-11 10:20:18 +02:00 committed by GitHub
commit 96c09f8550
3 changed files with 71 additions and 11 deletions

View File

@ -9,7 +9,7 @@
// except according to those terms. // except according to those terms.
// ignore-tidy-linelength // ignore-tidy-linelength
// compile-flags: -Z verbose -Z mir-emit-validate=1 // compile-flags: -Z verbose -Z mir-emit-validate=1 -Z span_free_formats
struct Test(i32); struct Test(i32);
@ -20,16 +20,13 @@ impl Test {
fn main() { fn main() {
let mut x = 0; let mut x = 0;
Test(0).foo(&mut x); Test(0).foo(&mut x); // just making sure we do not panic when there is a tuple struct ctor
// Also test closures // Also test closures
let c = |x: &mut i32| { let y = &*x; *y }; let c = |x: &mut i32| { let y = &*x; *y };
c(&mut x); c(&mut x);
} }
// FIXME: Also test code generated inside the closure, make sure it has validation. Unfortunately,
// the interesting lines of code also contain name of the source file, so we cannot test for it.
// END RUST SOURCE // END RUST SOURCE
// START rustc.node12.EraseRegions.after.mir // START rustc.node12.EraseRegions.after.mir
// bb0: { // bb0: {
@ -57,3 +54,24 @@ fn main() {
// } // }
// } // }
// END rustc.node23.EraseRegions.after.mir // END rustc.node23.EraseRegions.after.mir
// START rustc.node50.EraseRegions.after.mir
// fn main::{{closure}}(_1: &ReErased [closure@NodeId(50)], _2: &ReErased mut i32) -> i32 {
// bb0: {
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483663) => validate_1/8cd878b::main[0]::{{closure}}[0] }, "BrEnv") [closure@NodeId(50)], _2: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483663) => validate_1/8cd878b::main[0]::{{closure}}[0] }, BrAnon(1)) mut i32]);
// StorageLive(_3);
// _3 = _2;
// StorageLive(_4);
// Validate(Suspend(ReScope(Remainder(BlockRemainder { block: NodeId(41), first_statement_index: 0 }))), [(*_3): i32]);
// _4 = &ReErased (*_3);
// Validate(Acquire, [(*_4): i32/ReScope(Remainder(BlockRemainder { block: NodeId(41), first_statement_index: 0 })) (imm)]);
// StorageLive(_5);
// _5 = (*_4);
// _0 = _5;
// StorageDead(_5);
// StorageDead(_4);
// EndRegion(ReScope(Remainder(BlockRemainder { block: NodeId(41), first_statement_index: 0 })));
// StorageDead(_3);
// return;
// }
// }
// END rustc.node50.EraseRegions.after.mir

View File

@ -9,7 +9,7 @@
// except according to those terms. // except according to those terms.
// ignore-tidy-linelength // ignore-tidy-linelength
// compile-flags: -Z verbose -Z mir-emit-validate=1 // compile-flags: -Z verbose -Z mir-emit-validate=1 -Z span_free_formats
// Make sure unsafe fns and fns with an unsafe block only get restricted validation. // Make sure unsafe fns and fns with an unsafe block only get restricted validation.
@ -45,6 +45,19 @@ fn main() {
// } // }
// } // }
// END rustc.node4.EraseRegions.after.mir // END rustc.node4.EraseRegions.after.mir
// START rustc.node22.EraseRegions.after.mir
// fn write_42::{{closure}}(_1: &ReErased [closure@NodeId(22)], _2: *mut i32) -> () {
// bb0: {
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483659) => validate_4/8cd878b::write_42[0]::{{closure}}[0] }, "BrEnv") [closure@NodeId(22)], _2: *mut i32]);
// Validate(Release, [_1: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483659) => validate_4/8cd878b::write_42[0]::{{closure}}[0] }, "BrEnv") [closure@NodeId(22)], _2: *mut i32]);
// StorageLive(_3);
// _3 = _2;
// (*_3) = const 23i32;
// StorageDead(_3);
// return;
// }
// }
// END rustc.node22.EraseRegions.after.mir
// START rustc.node31.EraseRegions.after.mir // START rustc.node31.EraseRegions.after.mir
// fn test(_1: &ReErased mut i32) -> () { // fn test(_1: &ReErased mut i32) -> () {
// bb0: { // bb0: {
@ -58,3 +71,13 @@ fn main() {
// } // }
// } // }
// END rustc.node31.EraseRegions.after.mir // END rustc.node31.EraseRegions.after.mir
// START rustc.node60.EraseRegions.after.mir
// fn main::{{closure}}(_1: &ReErased [closure@NodeId(60)], _2: &ReErased mut i32) -> bool {
// bb0: {
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483663) => validate_4/8cd878b::main[0]::{{closure}}[0] }, "BrEnv") [closure@NodeId(60)], _2: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483663) => validate_4/8cd878b::main[0]::{{closure}}[0] }, BrAnon(1)) mut i32]);
// Validate(Release, [_1: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483663) => validate_4/8cd878b::main[0]::{{closure}}[0] }, "BrEnv") [closure@NodeId(60)], _2: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483663) => validate_4/8cd878b::main[0]::{{closure}}[0] }, BrAnon(1)) mut i32]);
// StorageLive(_3);
// _0 = const write_42(_4) -> bb1;
// }
// }
// END rustc.node60.EraseRegions.after.mir

View File

@ -9,9 +9,9 @@
// except according to those terms. // except according to those terms.
// ignore-tidy-linelength // ignore-tidy-linelength
// compile-flags: -Z verbose -Z mir-emit-validate=2 // compile-flags: -Z verbose -Z mir-emit-validate=2 -Z span_free_formats
// Make sure unsafe fns and fns with an unsafe block only get full validation. // Make sure unsafe fns and fns with an unsafe block still get full validation.
unsafe fn write_42(x: *mut i32) -> bool { unsafe fn write_42(x: *mut i32) -> bool {
*x = 42; *x = 42;
@ -26,12 +26,12 @@ fn main() {
test(&mut 0); test(&mut 0);
let test_closure = unsafe { |x: &mut i32| write_42(x) }; let test_closure = unsafe { |x: &mut i32| write_42(x) };
// Note that validation will fail if this is executed: The closure keeps the lock on
// x, so the write in write_42 fails. This test just checks code generation,
// so the UB doesn't matter.
test_closure(&mut 0); test_closure(&mut 0);
} }
// FIXME: Also test code generated inside the closure, make sure it has validation. Unfortunately,
// the interesting lines of code also contain name of the source file, so we cannot test for it.
// END RUST SOURCE // END RUST SOURCE
// START rustc.node17.EraseRegions.after.mir // START rustc.node17.EraseRegions.after.mir
// fn test(_1: &ReErased mut i32) -> () { // fn test(_1: &ReErased mut i32) -> () {
@ -42,3 +42,22 @@ fn main() {
// } // }
// } // }
// END rustc.node17.EraseRegions.after.mir // END rustc.node17.EraseRegions.after.mir
// START rustc.node46.EraseRegions.after.mir
// fn main::{{closure}}(_1: &ReErased [closure@NodeId(46)], _2: &ReErased mut i32) -> bool {
// bb0: {
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483660) => validate_5/8cd878b::main[0]::{{closure}}[0] }, "BrEnv") [closure@NodeId(46)], _2: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(2147483660) => validate_5/8cd878b::main[0]::{{closure}}[0] }, BrAnon(1)) mut i32]);
// StorageLive(_3);
// _3 = _2;
// StorageLive(_4);
// StorageLive(_5);
// Validate(Suspend(ReScope(Misc(NodeId(44)))), [(*_3): i32]);
// _5 = &ReErased mut (*_3);
// Validate(Acquire, [(*_5): i32/ReScope(Misc(NodeId(44)))]);
// _4 = _5 as *mut i32 (Misc);
// StorageDead(_5);
// EndRegion(ReScope(Misc(NodeId(44))));
// Validate(Release, [_0: bool, _4: *mut i32]);
// _0 = const write_42(_4) -> bb1;
// }
// }
// END rustc.node46.EraseRegions.after.mir