From c54dcf59aed7f44260df41a800e7855436a205eb Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 13 Nov 2018 13:03:01 +0100 Subject: [PATCH] add some tests for retagging inside tuples and options --- .../stacked_borrows/return_invalid_mut_option.rs | 11 +++++++++++ .../stacked_borrows/return_invalid_mut_tuple.rs | 11 +++++++++++ .../stacked_borrows/return_invalid_shr_option.rs | 11 +++++++++++ .../stacked_borrows/return_invalid_shr_tuple.rs | 11 +++++++++++ 4 files changed, 44 insertions(+) create mode 100644 tests/compile-fail-fullmir/stacked_borrows/return_invalid_mut_option.rs create mode 100644 tests/compile-fail-fullmir/stacked_borrows/return_invalid_mut_tuple.rs create mode 100644 tests/compile-fail-fullmir/stacked_borrows/return_invalid_shr_option.rs create mode 100644 tests/compile-fail-fullmir/stacked_borrows/return_invalid_shr_tuple.rs diff --git a/tests/compile-fail-fullmir/stacked_borrows/return_invalid_mut_option.rs b/tests/compile-fail-fullmir/stacked_borrows/return_invalid_mut_option.rs new file mode 100644 index 000000000000..28a1f74c6ac2 --- /dev/null +++ b/tests/compile-fail-fullmir/stacked_borrows/return_invalid_mut_option.rs @@ -0,0 +1,11 @@ +// Make sure that we cannot return a `&mut` that got already invalidated, not even in an `Option`. +fn foo(x: &mut (i32, i32)) -> Option<&mut i32> { + let xraw = x as *mut (i32, i32); + let ret = Some(unsafe { &mut (*xraw).1 }); + let _val = unsafe { *xraw }; // invalidate xref + ret //~ ERROR does not exist on the stack +} + +fn main() { + foo(&mut (1, 2)); +} diff --git a/tests/compile-fail-fullmir/stacked_borrows/return_invalid_mut_tuple.rs b/tests/compile-fail-fullmir/stacked_borrows/return_invalid_mut_tuple.rs new file mode 100644 index 000000000000..3357af68a841 --- /dev/null +++ b/tests/compile-fail-fullmir/stacked_borrows/return_invalid_mut_tuple.rs @@ -0,0 +1,11 @@ +// Make sure that we cannot return a `&mut` that got already invalidated, not even in a tuple. +fn foo(x: &mut (i32, i32)) -> (&mut i32,) { + let xraw = x as *mut (i32, i32); + let ret = (unsafe { &mut (*xraw).1 },); + let _val = unsafe { *xraw }; // invalidate xref + ret //~ ERROR does not exist on the stack +} + +fn main() { + foo(&mut (1, 2)); +} diff --git a/tests/compile-fail-fullmir/stacked_borrows/return_invalid_shr_option.rs b/tests/compile-fail-fullmir/stacked_borrows/return_invalid_shr_option.rs new file mode 100644 index 000000000000..9d220991c330 --- /dev/null +++ b/tests/compile-fail-fullmir/stacked_borrows/return_invalid_shr_option.rs @@ -0,0 +1,11 @@ +// Make sure that we cannot return a `&` that got already invalidated, not even in an `Option`. +fn foo(x: &mut (i32, i32)) -> Option<&i32> { + let xraw = x as *mut (i32, i32); + let ret = Some(unsafe { &(*xraw).1 }); + unsafe { *xraw = (42, 23) }; // unfreeze + ret //~ ERROR is not frozen +} + +fn main() { + foo(&mut (1, 2)); +} diff --git a/tests/compile-fail-fullmir/stacked_borrows/return_invalid_shr_tuple.rs b/tests/compile-fail-fullmir/stacked_borrows/return_invalid_shr_tuple.rs new file mode 100644 index 000000000000..060fa25c2307 --- /dev/null +++ b/tests/compile-fail-fullmir/stacked_borrows/return_invalid_shr_tuple.rs @@ -0,0 +1,11 @@ +// Make sure that we cannot return a `&` that got already invalidated, not even in a tuple. +fn foo(x: &mut (i32, i32)) -> (&i32,) { + let xraw = x as *mut (i32, i32); + let ret = (unsafe { &(*xraw).1 },); + unsafe { *xraw = (42, 23) }; // unfreeze + ret //~ ERROR is not frozen +} + +fn main() { + foo(&mut (1, 2)); +}