diff --git a/src/test/ui/borrowck/borrowck-closures-two-mut-fail.nll.stderr b/src/test/ui/borrowck/borrowck-closures-two-mut-fail.nll.stderr new file mode 100644 index 0000000000000..c96799c85f206 --- /dev/null +++ b/src/test/ui/borrowck/borrowck-closures-two-mut-fail.nll.stderr @@ -0,0 +1,75 @@ +error[E0499]: cannot borrow `x` as mutable more than once at a time + --> $DIR/borrowck-closures-two-mut-fail.rs:26:24 + | +LL | let c1 = to_fn_mut(|| x = 4); + | -- - first borrow occurs due to use of `x` in closure + | | + | first mutable borrow occurs here +LL | let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once + | ^^ - second borrow occurs due to use of `x` in closure + | | + | second mutable borrow occurs here +LL | c1; + | -- borrow later used here + +error[E0499]: cannot borrow `x` as mutable more than once at a time + --> $DIR/borrowck-closures-two-mut-fail.rs:37:24 + | +LL | let c1 = to_fn_mut(|| set(&mut x)); + | -- - first borrow occurs due to use of `x` in closure + | | + | first mutable borrow occurs here +LL | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once + | ^^ - second borrow occurs due to use of `x` in closure + | | + | second mutable borrow occurs here +LL | c1; + | -- borrow later used here + +error[E0499]: cannot borrow `x` as mutable more than once at a time + --> $DIR/borrowck-closures-two-mut-fail.rs:44:24 + | +LL | let c1 = to_fn_mut(|| x = 5); + | -- - first borrow occurs due to use of `x` in closure + | | + | first mutable borrow occurs here +LL | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once + | ^^ - second borrow occurs due to use of `x` in closure + | | + | second mutable borrow occurs here +LL | c1; + | -- borrow later used here + +error[E0499]: cannot borrow `x` as mutable more than once at a time + --> $DIR/borrowck-closures-two-mut-fail.rs:51:24 + | +LL | let c1 = to_fn_mut(|| x = 5); + | -- - first borrow occurs due to use of `x` in closure + | | + | first mutable borrow occurs here +LL | let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure) + | ^^ - second borrow occurs due to use of `x` in closure + | | + | second mutable borrow occurs here +LL | //~^ ERROR cannot borrow `x` as mutable more than once +LL | c1; + | -- borrow later used here + +error[E0499]: cannot borrow `x` as mutable more than once at a time + --> $DIR/borrowck-closures-two-mut-fail.rs:63:24 + | +LL | let c1 = to_fn_mut(|| set(&mut *x.f)); + | -- - first borrow occurs due to use of `x` in closure + | | + | first mutable borrow occurs here +LL | let c2 = to_fn_mut(|| set(&mut *x.f)); + | ^^ - second borrow occurs due to use of `x` in closure + | | + | second mutable borrow occurs here +LL | //~^ ERROR cannot borrow `x` as mutable more than once +LL | c1; + | -- borrow later used here + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0499`. diff --git a/src/test/ui/borrowck/borrowck-closures-two-mut-fail.rs b/src/test/ui/borrowck/borrowck-closures-two-mut-fail.rs index 6a1b786f52843..66ebd346dab62 100644 --- a/src/test/ui/borrowck/borrowck-closures-two-mut-fail.rs +++ b/src/test/ui/borrowck/borrowck-closures-two-mut-fail.rs @@ -8,22 +8,23 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// ignore-compare-mode-nll - // Tests that two closures cannot simultaneously have mutable // access to the variable, whether that mutable access be used // for direct assignment or for taking mutable ref. Issue #6801. -// ignore-compare-mode-nll - #![feature(box_syntax)] + + + + fn to_fn_mut(f: F) -> F { f } fn a() { let mut x = 3; let c1 = to_fn_mut(|| x = 4); let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once + c1; } fn set(x: &mut isize) { @@ -34,12 +35,14 @@ fn b() { let mut x = 3; let c1 = to_fn_mut(|| set(&mut x)); let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once + c1; } fn c() { let mut x = 3; let c1 = to_fn_mut(|| x = 5); let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once + c1; } fn d() { @@ -47,6 +50,7 @@ fn d() { let c1 = to_fn_mut(|| x = 5); let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure) //~^ ERROR cannot borrow `x` as mutable more than once + c1; } fn g() { @@ -58,6 +62,7 @@ fn g() { let c1 = to_fn_mut(|| set(&mut *x.f)); let c2 = to_fn_mut(|| set(&mut *x.f)); //~^ ERROR cannot borrow `x` as mutable more than once + c1; } fn main() { diff --git a/src/test/ui/borrowck/borrowck-closures-two-mut-fail.stderr b/src/test/ui/borrowck/borrowck-closures-two-mut-fail.stderr index bea32f6be9964..59104cc4be9b9 100644 --- a/src/test/ui/borrowck/borrowck-closures-two-mut-fail.stderr +++ b/src/test/ui/borrowck/borrowck-closures-two-mut-fail.stderr @@ -9,11 +9,12 @@ LL | let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable mo | ^^ - borrow occurs due to use of `x` in closure | | | second mutable borrow occurs here +LL | c1; LL | } | - first borrow ends here error[E0499]: cannot borrow `x` as mutable more than once at a time - --> $DIR/borrowck-closures-two-mut-fail.rs:36:24 + --> $DIR/borrowck-closures-two-mut-fail.rs:37:24 | LL | let c1 = to_fn_mut(|| set(&mut x)); | -- - previous borrow occurs due to use of `x` in closure @@ -23,11 +24,12 @@ LL | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as muta | ^^ - borrow occurs due to use of `x` in closure | | | second mutable borrow occurs here +LL | c1; LL | } | - first borrow ends here error[E0499]: cannot borrow `x` as mutable more than once at a time - --> $DIR/borrowck-closures-two-mut-fail.rs:42:24 + --> $DIR/borrowck-closures-two-mut-fail.rs:44:24 | LL | let c1 = to_fn_mut(|| x = 5); | -- - previous borrow occurs due to use of `x` in closure @@ -37,11 +39,12 @@ LL | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as muta | ^^ - borrow occurs due to use of `x` in closure | | | second mutable borrow occurs here +LL | c1; LL | } | - first borrow ends here error[E0499]: cannot borrow `x` as mutable more than once at a time - --> $DIR/borrowck-closures-two-mut-fail.rs:48:24 + --> $DIR/borrowck-closures-two-mut-fail.rs:51:24 | LL | let c1 = to_fn_mut(|| x = 5); | -- - previous borrow occurs due to use of `x` in closure @@ -51,12 +54,12 @@ LL | let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nes | ^^ - borrow occurs due to use of `x` in closure | | | second mutable borrow occurs here -LL | //~^ ERROR cannot borrow `x` as mutable more than once +... LL | } | - first borrow ends here error[E0499]: cannot borrow `x` as mutable more than once at a time - --> $DIR/borrowck-closures-two-mut-fail.rs:59:24 + --> $DIR/borrowck-closures-two-mut-fail.rs:63:24 | LL | let c1 = to_fn_mut(|| set(&mut *x.f)); | -- - previous borrow occurs due to use of `x` in closure @@ -66,7 +69,7 @@ LL | let c2 = to_fn_mut(|| set(&mut *x.f)); | ^^ - borrow occurs due to use of `x` in closure | | | second mutable borrow occurs here -LL | //~^ ERROR cannot borrow `x` as mutable more than once +... LL | } | - first borrow ends here diff --git a/src/test/ui/borrowck/borrowck-closures-unique.nll.stderr b/src/test/ui/borrowck/borrowck-closures-unique.nll.stderr new file mode 100644 index 0000000000000..17519ccb1438d --- /dev/null +++ b/src/test/ui/borrowck/borrowck-closures-unique.nll.stderr @@ -0,0 +1,54 @@ +error[E0500]: closure requires unique access to `x` but it is already borrowed + --> $DIR/borrowck-closures-unique.rs:36:14 + | +LL | let c1 = || get(x); + | -- - first borrow occurs due to use of `x` in closure + | | + | borrow occurs here +LL | let c2 = || set(x); //~ ERROR closure requires unique access to `x` + | ^^ - second borrow occurs due to use of `x` in closure + | | + | closure construction occurs here +LL | c1; + | -- borrow later used here + +error[E0500]: closure requires unique access to `x` but it is already borrowed + --> $DIR/borrowck-closures-unique.rs:42:14 + | +LL | let c1 = || get(x); + | -- - first borrow occurs due to use of `x` in closure + | | + | borrow occurs here +LL | let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique access to `x` + | ^^ - second borrow occurs due to use of `x` in closure + | | + | closure construction occurs here +LL | c1; + | -- borrow later used here + +error[E0524]: two closures require unique access to `x` at the same time + --> $DIR/borrowck-closures-unique.rs:48:14 + | +LL | let c1 = || set(x); + | -- - first borrow occurs due to use of `x` in closure + | | + | first closure is constructed here +LL | let c2 = || set(x); //~ ERROR two closures require unique access to `x` at the same time + | ^^ - second borrow occurs due to use of `x` in closure + | | + | second closure is constructed here +LL | c1; + | -- borrow later used here + +error[E0594]: cannot assign to `x`, as it is not declared as mutable + --> $DIR/borrowck-closures-unique.rs:57:38 + | +LL | fn e(x: &'static mut isize) { + | - help: consider changing this to be mutable: `mut x` +LL | let c1 = |y: &'static mut isize| x = y; //~ ERROR closure cannot assign to immutable argument + | ^^^^^ cannot assign + +error: aborting due to 4 previous errors + +Some errors occurred: E0500, E0524, E0594. +For more information about an error, try `rustc --explain E0500`. diff --git a/src/test/ui/borrowck/borrowck-closures-unique.rs b/src/test/ui/borrowck/borrowck-closures-unique.rs index 9a74f188a545f..caf5785f481c2 100644 --- a/src/test/ui/borrowck/borrowck-closures-unique.rs +++ b/src/test/ui/borrowck/borrowck-closures-unique.rs @@ -8,14 +8,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// ignore-compare-mode-nll - // Tests that a closure which requires mutable access to the referent // of an `&mut` requires a "unique" borrow -- that is, the variable to // be borrowed (here, `x`) will not be borrowed *mutably*, but // may be *immutable*, but we cannot allow // multiple borrows. + + fn get(x: &isize) -> isize { *x } @@ -27,25 +27,40 @@ fn set(x: &mut isize) -> isize { fn a(x: &mut isize) { let c1 = || get(x); let c2 = || get(x); + c1(); + c2(); } fn b(x: &mut isize) { let c1 = || get(x); let c2 = || set(x); //~ ERROR closure requires unique access to `x` + c1; } fn c(x: &mut isize) { let c1 = || get(x); let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique access to `x` + c1; } fn d(x: &mut isize) { let c1 = || set(x); let c2 = || set(x); //~ ERROR two closures require unique access to `x` at the same time + c1; +} + +// This test was originally encoded in the form shown as `fn f` below. +// However, since MIR-borrowck and thus NLL takes more control-flow information +// into account, it was necessary to change the test in order to witness the +// same (expected) error under both AST-borrowck and NLL. +fn e(x: &'static mut isize) { + let c1 = |y: &'static mut isize| x = y; //~ ERROR closure cannot assign to immutable argument + c1; } -fn e(x: &mut isize) { +fn f(x: &'static mut isize) { let c1 = || x = panic!(); //~ ERROR closure cannot assign to immutable argument + c1; } fn main() { diff --git a/src/test/ui/borrowck/borrowck-closures-unique.stderr b/src/test/ui/borrowck/borrowck-closures-unique.stderr index 27163bde34f6a..cfcb81b31e700 100644 --- a/src/test/ui/borrowck/borrowck-closures-unique.stderr +++ b/src/test/ui/borrowck/borrowck-closures-unique.stderr @@ -1,5 +1,5 @@ error[E0500]: closure requires unique access to `x` but it is already borrowed - --> $DIR/borrowck-closures-unique.rs:34:14 + --> $DIR/borrowck-closures-unique.rs:36:14 | LL | let c1 = || get(x); | -- - previous borrow occurs due to use of `x` in closure @@ -9,11 +9,12 @@ LL | let c2 = || set(x); //~ ERROR closure requires unique access to `x` | ^^ - borrow occurs due to use of `x` in closure | | | closure construction occurs here +LL | c1; LL | } | - borrow ends here error[E0500]: closure requires unique access to `x` but it is already borrowed - --> $DIR/borrowck-closures-unique.rs:39:14 + --> $DIR/borrowck-closures-unique.rs:42:14 | LL | let c1 = || get(x); | -- - previous borrow occurs due to use of `x` in closure @@ -23,11 +24,12 @@ LL | let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique acce | ^^ - borrow occurs due to use of `x` in closure | | | closure construction occurs here +LL | c1; LL | } | - borrow ends here error[E0524]: two closures require unique access to `x` at the same time - --> $DIR/borrowck-closures-unique.rs:44:14 + --> $DIR/borrowck-closures-unique.rs:48:14 | LL | let c1 = || set(x); | -- - previous borrow occurs due to use of `x` in closure @@ -37,11 +39,22 @@ LL | let c2 = || set(x); //~ ERROR two closures require unique access to `x` | ^^ - borrow occurs due to use of `x` in closure | | | second closure is constructed here +LL | c1; LL | } | - borrow from first closure ends here error[E0595]: closure cannot assign to immutable argument `x` - --> $DIR/borrowck-closures-unique.rs:48:14 + --> $DIR/borrowck-closures-unique.rs:57:14 + | +LL | let c1 = |y: &'static mut isize| x = y; //~ ERROR closure cannot assign to immutable argument + | ^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow mutably +help: consider removing the `&mut`, as it is an immutable binding to a mutable reference + | +LL | x //~ ERROR closure cannot assign to immutable argument + | ^ + +error[E0595]: closure cannot assign to immutable argument `x` + --> $DIR/borrowck-closures-unique.rs:62:14 | LL | let c1 = || x = panic!(); //~ ERROR closure cannot assign to immutable argument | ^^ cannot borrow mutably @@ -50,7 +63,7 @@ help: consider removing the `&mut`, as it is an immutable binding to a mutable r LL | x //~ ERROR closure cannot assign to immutable argument | ^ -error: aborting due to 4 previous errors +error: aborting due to 5 previous errors Some errors occurred: E0500, E0524, E0595. For more information about an error, try `rustc --explain E0500`. diff --git a/src/test/ui/hashmap-lifetimes.nll.stderr b/src/test/ui/hashmap-lifetimes.nll.stderr new file mode 100644 index 0000000000000..943b684fe8d77 --- /dev/null +++ b/src/test/ui/hashmap-lifetimes.nll.stderr @@ -0,0 +1,13 @@ +error[E0502]: cannot borrow `my_stuff` as mutable because it is also borrowed as immutable + --> $DIR/hashmap-lifetimes.rs:18:5 + | +LL | let mut it = my_stuff.iter(); + | -------- immutable borrow occurs here +LL | my_stuff.insert(1, 43); //~ ERROR cannot borrow + | ^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here +LL | it; + | -- borrow later used here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0502`. diff --git a/src/test/ui/hashmap-lifetimes.rs b/src/test/ui/hashmap-lifetimes.rs index 9418c34cbfdaa..eb7404e7b780b 100644 --- a/src/test/ui/hashmap-lifetimes.rs +++ b/src/test/ui/hashmap-lifetimes.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// ignore-compare-mode-nll + fn main() { let mut my_stuff = std::collections::HashMap::new(); @@ -16,4 +16,5 @@ fn main() { let mut it = my_stuff.iter(); my_stuff.insert(1, 43); //~ ERROR cannot borrow + it; } diff --git a/src/test/ui/hashmap-lifetimes.stderr b/src/test/ui/hashmap-lifetimes.stderr index eb129848ed2d6..187c98ab298b6 100644 --- a/src/test/ui/hashmap-lifetimes.stderr +++ b/src/test/ui/hashmap-lifetimes.stderr @@ -5,6 +5,7 @@ LL | let mut it = my_stuff.iter(); | -------- immutable borrow occurs here LL | my_stuff.insert(1, 43); //~ ERROR cannot borrow | ^^^^^^^^ mutable borrow occurs here +LL | it; LL | } | - immutable borrow ends here diff --git a/src/test/ui/unboxed-closures/unboxed-closure-region.nll.stderr b/src/test/ui/unboxed-closures/unboxed-closure-region.nll.stderr new file mode 100644 index 0000000000000..6ad57a15465d3 --- /dev/null +++ b/src/test/ui/unboxed-closures/unboxed-closure-region.nll.stderr @@ -0,0 +1,15 @@ +error[E0597]: `x` does not live long enough + --> $DIR/unboxed-closure-region.rs:18:12 + | +LL | || x //~ ERROR `x` does not live long enough + | -- ^ borrowed value does not live long enough + | | + | value captured here +LL | }; + | - `x` dropped here while still borrowed +LL | _f; + | -- borrow later used here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0597`. diff --git a/src/test/ui/unboxed-closures/unboxed-closure-region.rs b/src/test/ui/unboxed-closures/unboxed-closure-region.rs index cc635296210a0..da6dbc6e74f86 100644 --- a/src/test/ui/unboxed-closures/unboxed-closure-region.rs +++ b/src/test/ui/unboxed-closures/unboxed-closure-region.rs @@ -8,13 +8,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// ignore-compare-mode-nll - // Test that an unboxed closure that captures a free variable by // reference cannot escape the region of that variable. + + fn main() { let _f = { let x = 0; || x //~ ERROR `x` does not live long enough }; + _f; } diff --git a/src/test/ui/unboxed-closures/unboxed-closure-region.stderr b/src/test/ui/unboxed-closures/unboxed-closure-region.stderr index a838c3608b9a9..f85f3afff70e8 100644 --- a/src/test/ui/unboxed-closures/unboxed-closure-region.stderr +++ b/src/test/ui/unboxed-closures/unboxed-closure-region.stderr @@ -7,6 +7,7 @@ LL | || x //~ ERROR `x` does not live long enough | capture occurs here LL | }; | - borrowed value only lives until here +LL | _f; LL | } | - borrowed value needs to live until here diff --git a/src/test/ui/unboxed-closures/unboxed-closures-borrow-conflict.nll.stderr b/src/test/ui/unboxed-closures/unboxed-closures-borrow-conflict.nll.stderr new file mode 100644 index 0000000000000..cadda398c6f33 --- /dev/null +++ b/src/test/ui/unboxed-closures/unboxed-closures-borrow-conflict.nll.stderr @@ -0,0 +1,15 @@ +error[E0503]: cannot use `x` because it was mutably borrowed + --> $DIR/unboxed-closures-borrow-conflict.rs:19:14 + | +LL | let f = || x += 1; + | -- - borrow occurs due to use of `x` in closure + | | + | borrow of `x` occurs here +LL | let _y = x; //~ ERROR cannot use `x` because it was mutably borrowed + | ^ use of borrowed `x` +LL | f; + | - borrow later used here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0503`. diff --git a/src/test/ui/unboxed-closures/unboxed-closures-borrow-conflict.rs b/src/test/ui/unboxed-closures/unboxed-closures-borrow-conflict.rs index 734f89e5e0b8b..aa50fb837733c 100644 --- a/src/test/ui/unboxed-closures/unboxed-closures-borrow-conflict.rs +++ b/src/test/ui/unboxed-closures/unboxed-closures-borrow-conflict.rs @@ -8,13 +8,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// ignore-compare-mode-nll - // Test that an unboxed closure that mutates a free variable will // cause borrow conflicts. + + fn main() { let mut x = 0; let f = || x += 1; let _y = x; //~ ERROR cannot use `x` because it was mutably borrowed + f; }