From 356369dd08f968b74b8bdee3a177f9919194914b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 23 Oct 2018 16:01:22 +0200 Subject: [PATCH] test against passing invalid shared refs around --- .../compile-fail/stacked_borrows/load_invalid_shr.rs | 9 +++++++++ .../compile-fail/stacked_borrows/pass_invalid_shr.rs | 10 ++++++++++ .../stacked_borrows/return_invalid_shr.rs | 11 +++++++++++ 3 files changed, 30 insertions(+) create mode 100644 tests/compile-fail/stacked_borrows/load_invalid_shr.rs create mode 100644 tests/compile-fail/stacked_borrows/pass_invalid_shr.rs create mode 100644 tests/compile-fail/stacked_borrows/return_invalid_shr.rs diff --git a/tests/compile-fail/stacked_borrows/load_invalid_shr.rs b/tests/compile-fail/stacked_borrows/load_invalid_shr.rs new file mode 100644 index 00000000000..785a15c4704 --- /dev/null +++ b/tests/compile-fail/stacked_borrows/load_invalid_shr.rs @@ -0,0 +1,9 @@ +// Make sure that we cannot load from memory a `&` that got already invalidated. +fn main() { + let x = &mut 42; + let xraw = x as *mut _; + let xref = unsafe { &*xraw }; + let xref_in_mem = Box::new(xref); + let _val = *x; // invalidate xraw + let _val = *xref_in_mem; //~ ERROR Shr reference with non-reactivatable tag: Location should be frozen +} diff --git a/tests/compile-fail/stacked_borrows/pass_invalid_shr.rs b/tests/compile-fail/stacked_borrows/pass_invalid_shr.rs new file mode 100644 index 00000000000..8b7a846d849 --- /dev/null +++ b/tests/compile-fail/stacked_borrows/pass_invalid_shr.rs @@ -0,0 +1,10 @@ +// Make sure that we cannot pass by argument a `&` that got already invalidated. +fn foo(_: &i32) {} + +fn main() { + let x = &mut 42; + let xraw = &*x as *const _; + let xref = unsafe { &*xraw }; + let _val = *x; // invalidate xraw + foo(xref); //~ ERROR Shr reference with non-reactivatable tag: Location should be frozen +} diff --git a/tests/compile-fail/stacked_borrows/return_invalid_shr.rs b/tests/compile-fail/stacked_borrows/return_invalid_shr.rs new file mode 100644 index 00000000000..89c94127b0b --- /dev/null +++ b/tests/compile-fail/stacked_borrows/return_invalid_shr.rs @@ -0,0 +1,11 @@ +// Make sure that we cannot return a `&` that got already invalidated. +fn foo(x: &mut (i32, i32)) -> &i32 { + let xraw = x as *mut (i32, i32); + let ret = unsafe { &(*xraw).1 }; + let _val = *x; // invalidate xraw and its children + ret //~ ERROR Shr reference with non-reactivatable tag: Location should be frozen +} + +fn main() { + foo(&mut (1, 2)); +}