diff --git a/tests/ui/cleanup-rvalue-scopes-cf.rs b/tests/ui/borrowck/rvalue-borrow-scope-error.rs similarity index 81% rename from tests/ui/cleanup-rvalue-scopes-cf.rs rename to tests/ui/borrowck/rvalue-borrow-scope-error.rs index e3cecb1bffed6..5bf96e800d38e 100644 --- a/tests/ui/cleanup-rvalue-scopes-cf.rs +++ b/tests/ui/borrowck/rvalue-borrow-scope-error.rs @@ -1,15 +1,19 @@ -// Test that the borrow checker prevents pointers to temporaries -// with statement lifetimes from escaping. +//! Test that the borrow checker prevents pointers to temporaries +//! with statement lifetimes from escaping. use std::ops::Drop; static mut FLAGS: u64 = 0; -struct StackBox { f: T } -struct AddFlags { bits: u64 } +struct StackBox { + f: T, +} +struct AddFlags { + bits: u64, +} fn AddFlags(bits: u64) -> AddFlags { - AddFlags { bits: bits } + AddFlags { bits } } fn arg(x: &AddFlags) -> &AddFlags { diff --git a/tests/ui/cleanup-rvalue-scopes-cf.stderr b/tests/ui/borrowck/rvalue-borrow-scope-error.stderr similarity index 92% rename from tests/ui/cleanup-rvalue-scopes-cf.stderr rename to tests/ui/borrowck/rvalue-borrow-scope-error.stderr index 425cd75141cec..bedcfce454111 100644 --- a/tests/ui/cleanup-rvalue-scopes-cf.stderr +++ b/tests/ui/borrowck/rvalue-borrow-scope-error.stderr @@ -1,5 +1,5 @@ error[E0716]: temporary value dropped while borrowed - --> $DIR/cleanup-rvalue-scopes-cf.rs:26:19 + --> $DIR/rvalue-borrow-scope-error.rs:30:19 | LL | let x1 = arg(&AddFlags(1)); | ^^^^^^^^^^^ - temporary value is freed at the end of this statement @@ -16,7 +16,7 @@ LL ~ let x1 = arg(&binding); | error[E0716]: temporary value dropped while borrowed - --> $DIR/cleanup-rvalue-scopes-cf.rs:27:14 + --> $DIR/rvalue-borrow-scope-error.rs:31:14 | LL | let x2 = AddFlags(1).get(); | ^^^^^^^^^^^ - temporary value is freed at the end of this statement @@ -33,7 +33,7 @@ LL ~ let x2 = binding.get(); | error[E0716]: temporary value dropped while borrowed - --> $DIR/cleanup-rvalue-scopes-cf.rs:28:21 + --> $DIR/rvalue-borrow-scope-error.rs:32:21 | LL | let x3 = &*arg(&AddFlags(1)); | ^^^^^^^^^^^ - temporary value is freed at the end of this statement @@ -50,7 +50,7 @@ LL ~ let x3 = &*arg(&binding); | error[E0716]: temporary value dropped while borrowed - --> $DIR/cleanup-rvalue-scopes-cf.rs:29:24 + --> $DIR/rvalue-borrow-scope-error.rs:33:24 | LL | let ref x4 = *arg(&AddFlags(1)); | ^^^^^^^^^^^ - temporary value is freed at the end of this statement @@ -67,7 +67,7 @@ LL ~ let ref x4 = *arg(&binding); | error[E0716]: temporary value dropped while borrowed - --> $DIR/cleanup-rvalue-scopes-cf.rs:30:24 + --> $DIR/rvalue-borrow-scope-error.rs:34:24 | LL | let &ref x5 = arg(&AddFlags(1)); | ^^^^^^^^^^^ - temporary value is freed at the end of this statement @@ -84,7 +84,7 @@ LL ~ let &ref x5 = arg(&binding); | error[E0716]: temporary value dropped while borrowed - --> $DIR/cleanup-rvalue-scopes-cf.rs:31:14 + --> $DIR/rvalue-borrow-scope-error.rs:35:14 | LL | let x6 = AddFlags(1).get(); | ^^^^^^^^^^^ - temporary value is freed at the end of this statement @@ -101,7 +101,7 @@ LL ~ let x6 = binding.get(); | error[E0716]: temporary value dropped while borrowed - --> $DIR/cleanup-rvalue-scopes-cf.rs:32:44 + --> $DIR/rvalue-borrow-scope-error.rs:36:44 | LL | let StackBox { f: x7 } = StackBox { f: AddFlags(1).get() }; | ^^^^^^^^^^^ - temporary value is freed at the end of this statement diff --git a/tests/ui/cleanup-rvalue-scopes.rs b/tests/ui/cleanup-rvalue-scopes.rs deleted file mode 100644 index 09ceda065b9d3..0000000000000 --- a/tests/ui/cleanup-rvalue-scopes.rs +++ /dev/null @@ -1,128 +0,0 @@ -//@ run-pass -#![allow(unused_braces)] -#![allow(non_snake_case)] -#![allow(unused_variables)] -// Test that destructors for rvalue temporaries run either at end of -// statement or end of block, as appropriate given the temporary -// lifetime rules. - -#![feature(box_patterns)] - -static mut FLAGS: u64 = 0; - -struct Box { f: T } -struct AddFlags { bits: u64 } - -fn AddFlags(bits: u64) -> AddFlags { - AddFlags { bits: bits } -} - -fn arg(exp: u64, _x: &AddFlags) { - check_flags(exp); -} - -fn pass(v: T) -> T { - v -} - -fn check_flags(exp: u64) { - unsafe { - let x = FLAGS; - FLAGS = 0; - println!("flags {}, expected {}", x, exp); - assert_eq!(x, exp); - } -} - -impl AddFlags { - fn check_flags<'a>(&'a self, exp: u64) -> &'a AddFlags { - check_flags(exp); - self - } - - fn bits(&self) -> u64 { - self.bits - } -} - -impl Drop for AddFlags { - fn drop(&mut self) { - unsafe { - FLAGS = FLAGS + self.bits; - } - } -} - -macro_rules! end_of_block { - ($pat:pat, $expr:expr) => ( - { - println!("end_of_block({})", stringify!({let $pat = $expr;})); - - { - // Destructor here does not run until exit from the block. - let $pat = $expr; - check_flags(0); - } - check_flags(1); - } - ) -} - -macro_rules! end_of_stmt { - ($pat:pat, $expr:expr) => ( - { - println!("end_of_stmt({})", stringify!($expr)); - - { - // Destructor here run after `let` statement - // terminates. - let $pat = $expr; - check_flags(1); - } - - check_flags(0); - } - ) -} - -pub fn main() { - - // In all these cases, we trip over the rules designed to cover - // the case where we are taking addr of rvalue and storing that - // addr into a stack slot, either via `let ref` or via a `&` in - // the initializer. - - end_of_block!(_x, AddFlags(1)); - end_of_block!(_x, &AddFlags(1)); - end_of_block!(_x, & &AddFlags(1)); - end_of_block!(_x, Box { f: AddFlags(1) }); - end_of_block!(_x, Box { f: &AddFlags(1) }); - end_of_block!(_x, Box { f: &AddFlags(1) }); - end_of_block!(_x, pass(AddFlags(1))); - end_of_block!(ref _x, AddFlags(1)); - end_of_block!(AddFlags { bits: ref _x }, AddFlags(1)); - end_of_block!(&AddFlags { bits }, &AddFlags(1)); - end_of_block!((_, ref _y), (AddFlags(1), 22)); - end_of_block!(box ref _x, std::boxed::Box::new(AddFlags(1))); - end_of_block!(box _x, std::boxed::Box::new(AddFlags(1))); - end_of_block!(_, { { check_flags(0); &AddFlags(1) } }); - end_of_block!(_, &((Box { f: AddFlags(1) }).f)); - end_of_block!(_, &(([AddFlags(1)])[0])); - - // LHS does not create a ref binding, so temporary lives as long - // as statement, and we do not move the AddFlags out: - end_of_stmt!(_, AddFlags(1)); - end_of_stmt!((_, _), (AddFlags(1), 22)); - - // `&` operator appears inside an arg to a function, - // so it is not prolonged: - end_of_stmt!(ref _x, arg(0, &AddFlags(1))); - - // autoref occurs inside receiver, so temp lifetime is not - // prolonged: - end_of_stmt!(ref _x, AddFlags(1).check_flags(0).bits()); - - // No reference is created on LHS, thus RHS is moved into - // a temporary that lives just as long as the statement. - end_of_stmt!(AddFlags { bits }, AddFlags(1)); -} diff --git a/tests/ui/close-over-big-then-small-data.rs b/tests/ui/close-over-big-then-small-data.rs deleted file mode 100644 index d3cb1db8886b4..0000000000000 --- a/tests/ui/close-over-big-then-small-data.rs +++ /dev/null @@ -1,39 +0,0 @@ -//@ run-pass - -#![allow(dead_code)] -// If we use GEPi rather than GEP_tup_like when -// storing closure data (as we used to do), the u64 would -// overwrite the u16. - -struct Pair { - a: A, b: B -} - -struct Invoker { - a: A, - b: u16, -} - -trait Invokable { - fn f(&self) -> (A, u16); -} - -impl Invokable for Invoker { - fn f(&self) -> (A, u16) { - (self.a.clone(), self.b) - } -} - -fn f(a: A, b: u16) -> Box+'static> { - Box::new(Invoker { - a: a, - b: b, - }) as Box+'static> -} - -pub fn main() { - let (a, b) = f(22_u64, 44u16).f(); - println!("a={} b={}", a, b); - assert_eq!(a, 22u64); - assert_eq!(b, 44u16); -} diff --git a/tests/ui/command-line-diagnostics.rs b/tests/ui/diagnostic-width/command-line-error-format-human.rs similarity index 50% rename from tests/ui/command-line-diagnostics.rs rename to tests/ui/diagnostic-width/command-line-error-format-human.rs index 8a6cf5b8e32b0..a2cfbbcbeb106 100644 --- a/tests/ui/command-line-diagnostics.rs +++ b/tests/ui/diagnostic-width/command-line-error-format-human.rs @@ -1,4 +1,5 @@ -// This test checks the output format without the intermediate json representation +//! This test checks the output format without the intermediate json representation + //@ compile-flags: --error-format=human pub fn main() { diff --git a/tests/ui/command-line-diagnostics.stderr b/tests/ui/diagnostic-width/command-line-error-format-human.stderr similarity index 89% rename from tests/ui/command-line-diagnostics.stderr rename to tests/ui/diagnostic-width/command-line-error-format-human.stderr index 6d33fb4172f8d..b4b78239f880a 100644 --- a/tests/ui/command-line-diagnostics.stderr +++ b/tests/ui/diagnostic-width/command-line-error-format-human.stderr @@ -1,5 +1,5 @@ error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/command-line-diagnostics.rs:6:5 + --> $DIR/command-line-error-format-human.rs:7:5 | LL | let x = 42; | - first assignment to `x` diff --git a/tests/ui/cleanup-shortcircuit.rs b/tests/ui/lifetimes/rvalue-cleanup-shortcircuit.rs similarity index 78% rename from tests/ui/cleanup-shortcircuit.rs rename to tests/ui/lifetimes/rvalue-cleanup-shortcircuit.rs index 40a5dfa94e373..dba899585c429 100644 --- a/tests/ui/cleanup-shortcircuit.rs +++ b/tests/ui/lifetimes/rvalue-cleanup-shortcircuit.rs @@ -1,10 +1,9 @@ -//@ run-pass -// Test that cleanups for the RHS of shortcircuiting operators work. +//! Test that cleanups for the RHS of shortcircuiting operators work. +//@ run-pass #![allow(deref_nullptr)] - use std::env; pub fn main() { @@ -18,6 +17,8 @@ pub fn main() { if args.len() >= 2 && args[1] == "signal" { // Raise a segfault. - unsafe { *std::ptr::null_mut::() = 0; } + unsafe { + *std::ptr::null_mut::() = 0; + } } } diff --git a/tests/ui/lifetimes/rvalue-lifetime-drop-timing.rs b/tests/ui/lifetimes/rvalue-lifetime-drop-timing.rs new file mode 100644 index 0000000000000..9e7b84bfccfd2 --- /dev/null +++ b/tests/ui/lifetimes/rvalue-lifetime-drop-timing.rs @@ -0,0 +1,104 @@ +//! Test that destructors for temporaries run either at end of +//! statement or end of block as appropriate. + +//@ run-pass + +#![feature(box_patterns)] + +static mut FLAGS: u64 = 0; + +struct Box { + f: T, +} + +struct AddFlags { + bits: u64, +} + +fn add_flags(bits: u64) -> AddFlags { + AddFlags { bits } +} + +fn arg(expected: u64, _x: &AddFlags) { + check_flags(expected); +} + +fn pass(v: T) -> T { + v +} + +fn check_flags(expected: u64) { + unsafe { + let actual = FLAGS; + FLAGS = 0; + assert_eq!(actual, expected, "flags {}, expected {}", actual, expected); + } +} + +impl AddFlags { + fn check_flags(&self, expected: u64) -> &AddFlags { + check_flags(expected); + self + } + + fn bits(&self) -> u64 { + self.bits + } +} + +impl Drop for AddFlags { + fn drop(&mut self) { + unsafe { + FLAGS += self.bits; + } + } +} + +macro_rules! end_of_block { + ($pat:pat, $expr:expr) => {{ + { + let $pat = $expr; + check_flags(0); + } + check_flags(1); + }}; +} + +macro_rules! end_of_stmt { + ($pat:pat, $expr:expr) => {{ + { + let $pat = $expr; + check_flags(1); + } + check_flags(0); + }}; +} + +fn main() { + end_of_block!(_x, add_flags(1)); + end_of_block!(_x, &add_flags(1)); + end_of_block!(_x, &&add_flags(1)); + end_of_block!(_x, Box { f: add_flags(1) }); + end_of_block!(_x, Box { f: &add_flags(1) }); + end_of_block!(_x, pass(add_flags(1))); + end_of_block!(ref _x, add_flags(1)); + end_of_block!(AddFlags { bits: ref _x }, add_flags(1)); + end_of_block!(&AddFlags { bits: _ }, &add_flags(1)); + end_of_block!((_, ref _y), (add_flags(1), 22)); + end_of_block!(box ref _x, std::boxed::Box::new(add_flags(1))); + end_of_block!(box _x, std::boxed::Box::new(add_flags(1))); + end_of_block!(_, { + { + check_flags(0); + &add_flags(1) + } + }); + end_of_block!(_, &((Box { f: add_flags(1) }).f)); + end_of_block!(_, &(([add_flags(1)])[0])); + + end_of_stmt!(_, add_flags(1)); + end_of_stmt!((_, _), (add_flags(1), 22)); + end_of_stmt!(ref _x, arg(0, &add_flags(1))); + end_of_stmt!(ref _x, add_flags(1).check_flags(0).bits()); + end_of_stmt!(AddFlags { bits: _ }, add_flags(1)); +} diff --git a/tests/ui/cleanup-rvalue-temp-during-incomplete-alloc.rs b/tests/ui/panics/rvalue-cleanup-during-box-panic.rs similarity index 90% rename from tests/ui/cleanup-rvalue-temp-during-incomplete-alloc.rs rename to tests/ui/panics/rvalue-cleanup-during-box-panic.rs index 4c59df24e4b3f..84c5d85d7e096 100644 --- a/tests/ui/cleanup-rvalue-temp-during-incomplete-alloc.rs +++ b/tests/ui/panics/rvalue-cleanup-during-box-panic.rs @@ -25,16 +25,20 @@ use std::thread; enum Conzabble { - Bickwick(Foo) + Bickwick(Foo), } -struct Foo { field: Box } +struct Foo { + field: Box, +} fn do_it(x: &[usize]) -> Foo { panic!() } -fn get_bar(x: usize) -> Vec { vec![x * 2] } +fn get_bar(x: usize) -> Vec { + vec![x * 2] +} pub fn fails() { let x = 2;