From 5f524ed5c4b0d5e0bf6a6ea52284704411712c82 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 12:59:40 +0100 Subject: [PATCH 01/15] Switch to using revisions in borrowck-lend-flow-loop.rs Most of the time we want to robustify tests, but in this case this test is deliberately encoding artifacts of AST-borrowck. So instead of adding artificial uses that would obscure the aspects of AST-borrowck that are being tests, we instead use revisions and then mark the cases that apply to NLL as well as AST-borrowck. --- ...err => borrowck-lend-flow-loop.ast.stderr} | 22 ++++---- .../borrowck-lend-flow-loop.nll.stderr | 10 ++-- .../ui/borrowck/borrowck-lend-flow-loop.rs | 50 +++++++++---------- 3 files changed, 41 insertions(+), 41 deletions(-) rename src/test/ui/borrowck/{borrowck-lend-flow-loop.stderr => borrowck-lend-flow-loop.ast.stderr} (81%) diff --git a/src/test/ui/borrowck/borrowck-lend-flow-loop.stderr b/src/test/ui/borrowck/borrowck-lend-flow-loop.ast.stderr similarity index 81% rename from src/test/ui/borrowck/borrowck-lend-flow-loop.stderr rename to src/test/ui/borrowck/borrowck-lend-flow-loop.ast.stderr index 534e30b564d7f..1844d8275999d 100644 --- a/src/test/ui/borrowck/borrowck-lend-flow-loop.stderr +++ b/src/test/ui/borrowck/borrowck-lend-flow-loop.ast.stderr @@ -4,7 +4,7 @@ error[E0502]: cannot borrow `*v` as immutable because `v` is also borrowed as mu LL | let mut x = &mut v; | - mutable borrow occurs here ... -LL | borrow(&*v); //~ ERROR cannot borrow +LL | borrow(&*v); //[ast]~ ERROR cannot borrow | ^^ immutable borrow occurs here LL | } LL | } @@ -16,7 +16,7 @@ error[E0502]: cannot borrow `*v` as immutable because `v` is also borrowed as mu LL | let mut x = &mut v; | - mutable borrow occurs here LL | for _ in 0..3 { -LL | borrow(&*v); //~ ERROR cannot borrow +LL | borrow(&*v); //[ast]~ ERROR cannot borrow | ^^ immutable borrow occurs here ... LL | } @@ -25,7 +25,7 @@ LL | } error[E0502]: cannot borrow `*v` as mutable because `v` is also borrowed as immutable --> $DIR/borrowck-lend-flow-loop.rs:57:25 | -LL | borrow_mut(&mut *v); //~ ERROR cannot borrow +LL | borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow | ^^ mutable borrow occurs here LL | _x = &v; | - immutable borrow occurs here @@ -36,7 +36,7 @@ LL | } error[E0502]: cannot borrow `*v` as mutable because `v` is also borrowed as immutable --> $DIR/borrowck-lend-flow-loop.rs:69:25 | -LL | borrow_mut(&mut *v); //~ ERROR cannot borrow +LL | borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow | ^^ mutable borrow occurs here LL | _x = &v; | - immutable borrow occurs here @@ -50,7 +50,7 @@ error[E0502]: cannot borrow `*v` as mutable because `v` is also borrowed as immu LL | _x = &v; | - immutable borrow occurs here ... -LL | borrow_mut(&mut *v); //~ ERROR cannot borrow +LL | borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow | ^^ mutable borrow occurs here LL | } | - immutable borrow ends here @@ -61,7 +61,7 @@ error[E0502]: cannot borrow `*v` as mutable because `v` is also borrowed as immu LL | _x = &v; | - immutable borrow occurs here ... -LL | borrow_mut(&mut *v); //~ ERROR cannot borrow +LL | borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow | ^^ mutable borrow occurs here LL | } | - immutable borrow ends here @@ -69,19 +69,19 @@ LL | } error[E0502]: cannot borrow `*v` as immutable because `v` is also borrowed as mutable --> $DIR/borrowck-lend-flow-loop.rs:109:17 | -LL | borrow(&*v); //~ ERROR cannot borrow +LL | borrow(&*v); //[ast]~ ERROR cannot borrow | ^^ immutable borrow occurs here -LL | if cond2 { -LL | x = &mut v; //~ ERROR cannot borrow +... +LL | x = &mut v; //[ast]~ ERROR cannot borrow | - mutable borrow occurs here ... LL | } | - mutable borrow ends here error[E0499]: cannot borrow `v` as mutable more than once at a time - --> $DIR/borrowck-lend-flow-loop.rs:111:22 + --> $DIR/borrowck-lend-flow-loop.rs:112:22 | -LL | x = &mut v; //~ ERROR cannot borrow +LL | x = &mut v; //[ast]~ ERROR cannot borrow | ^ mutable borrow starts here in previous iteration of loop ... LL | } diff --git a/src/test/ui/borrowck/borrowck-lend-flow-loop.nll.stderr b/src/test/ui/borrowck/borrowck-lend-flow-loop.nll.stderr index 388fc9c5fa8ac..19de3582c8819 100644 --- a/src/test/ui/borrowck/borrowck-lend-flow-loop.nll.stderr +++ b/src/test/ui/borrowck/borrowck-lend-flow-loop.nll.stderr @@ -4,9 +4,9 @@ error[E0502]: cannot borrow `*v` as immutable because it is also borrowed as mut LL | let mut x = &mut v; | ------ mutable borrow occurs here LL | for _ in 0..3 { -LL | borrow(&*v); //~ ERROR cannot borrow +LL | borrow(&*v); //[ast]~ ERROR cannot borrow | ^^^ immutable borrow occurs here -LL | } +... LL | *x = box 5; | -- mutable borrow used here, in later iteration of loop @@ -15,10 +15,10 @@ error[E0502]: cannot borrow `*v` as immutable because it is also borrowed as mut | LL | **x += 1; | -------- mutable borrow used here, in later iteration of loop -LL | borrow(&*v); //~ ERROR cannot borrow +LL | borrow(&*v); //[ast]~ ERROR cannot borrow | ^^^ immutable borrow occurs here -LL | if cond2 { -LL | x = &mut v; //~ ERROR cannot borrow +... +LL | x = &mut v; //[ast]~ ERROR cannot borrow | ------ mutable borrow occurs here error: aborting due to 2 previous errors diff --git a/src/test/ui/borrowck/borrowck-lend-flow-loop.rs b/src/test/ui/borrowck/borrowck-lend-flow-loop.rs index f09e7ffd7e4b7..7008e5cef4b75 100644 --- a/src/test/ui/borrowck/borrowck-lend-flow-loop.rs +++ b/src/test/ui/borrowck/borrowck-lend-flow-loop.rs @@ -1,18 +1,18 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// Note: the borrowck analysis is currently flow-insensitive. -// Therefore, some of these errors are marked as spurious and could be -// corrected by a simple change to the analysis. The others are -// either genuine or would require more advanced changes. The latter -// cases are noted. +// revisions: ast nll + +// Since we are testing nll migration explicitly as a separate +// revision, don't worry about the --compare-mode=nll on this test. + +// ignore-compare-mode-nll + +//[ast]compile-flags: -Z borrowck=ast +//[nll]compile-flags: -Z borrowck=migrate -Z two-phase-borrows + +// Note: the borrowck analysis was originally a flow-insensitive pass +// over the AST. Therefore, some of these (AST) errors are marked as +// spurious and are corrected by the flow-sensitive (NLL) analysis. +// The others are either genuine or would require more advanced +// changes. The latter cases are noted. #![feature(box_syntax)] @@ -32,7 +32,7 @@ fn loop_overarching_alias_mut() { let mut x = &mut v; **x += 1; loop { - borrow(&*v); //~ ERROR cannot borrow + borrow(&*v); //[ast]~ ERROR cannot borrow } } @@ -42,11 +42,11 @@ fn block_overarching_alias_mut() { let mut v: Box<_> = box 3; let mut x = &mut v; for _ in 0..3 { - borrow(&*v); //~ ERROR cannot borrow + borrow(&*v); //[ast]~ ERROR cannot borrow + //[nll]~^ ERROR cannot borrow } *x = box 5; } - fn loop_aliased_mut() { // In this instance, the borrow is carried through the loop. @@ -54,7 +54,7 @@ fn loop_aliased_mut() { let mut w: Box<_> = box 4; let mut _x = &w; loop { - borrow_mut(&mut *v); //~ ERROR cannot borrow + borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow _x = &v; } } @@ -66,7 +66,7 @@ fn while_aliased_mut() { let mut w: Box<_> = box 4; let mut _x = &w; while cond() { - borrow_mut(&mut *v); //~ ERROR cannot borrow + borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow _x = &v; } } @@ -83,7 +83,7 @@ fn loop_aliased_mut_break() { _x = &v; break; } - borrow_mut(&mut *v); //~ ERROR cannot borrow + borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow } fn while_aliased_mut_break() { @@ -97,7 +97,7 @@ fn while_aliased_mut_break() { _x = &v; break; } - borrow_mut(&mut *v); //~ ERROR cannot borrow + borrow_mut(&mut *v); //[ast]~ ERROR cannot borrow } fn while_aliased_mut_cond(cond: bool, cond2: bool) { @@ -106,13 +106,13 @@ fn while_aliased_mut_cond(cond: bool, cond2: bool) { let mut x = &mut w; while cond { **x += 1; - borrow(&*v); //~ ERROR cannot borrow + borrow(&*v); //[ast]~ ERROR cannot borrow + //[nll]~^ ERROR cannot borrow if cond2 { - x = &mut v; //~ ERROR cannot borrow + x = &mut v; //[ast]~ ERROR cannot borrow } } } - fn loop_break_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F) where F: FnMut(&'r mut usize) -> bool, { From cd52b3c2dccf64829112fe56995e25f2b75591df Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 13:07:51 +0100 Subject: [PATCH 02/15] Make `ui/span/borrowck-borrow-overloaded-auto-deref-mut.rs` robust w.r.t. NLL. --- ...orrow-overloaded-auto-deref-mut.nll.stderr | 25 +++++++++++++++++-- ...rrowck-borrow-overloaded-auto-deref-mut.rs | 6 +++-- ...ck-borrow-overloaded-auto-deref-mut.stderr | 2 ++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.nll.stderr b/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.nll.stderr index 4a693a3b05d4e..389a1116c163a 100644 --- a/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.nll.stderr +++ b/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.nll.stderr @@ -14,6 +14,16 @@ LL | fn deref_extend_mut_field1(x: &Own) -> &mut isize { LL | &mut x.y //~ ERROR cannot borrow | ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable +error[E0499]: cannot borrow `*x` as mutable more than once at a time + --> $DIR/borrowck-borrow-overloaded-auto-deref-mut.rs:88:19 + | +LL | let _x = &mut x.x; + | - first mutable borrow occurs here +LL | let _y = &mut x.y; //~ ERROR cannot borrow + | ^ second mutable borrow occurs here +LL | use_mut(_x); + | -- first borrow later used here + error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable --> $DIR/borrowck-borrow-overloaded-auto-deref-mut.rs:98:5 | @@ -30,6 +40,16 @@ LL | fn assign_field2<'a>(x: &'a Own) { LL | x.y = 3; //~ ERROR cannot borrow | ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable +error[E0499]: cannot borrow `*x` as mutable more than once at a time + --> $DIR/borrowck-borrow-overloaded-auto-deref-mut.rs:111:5 + | +LL | let _p: &mut Point = &mut **x; + | -- first mutable borrow occurs here +LL | x.y = 3; //~ ERROR cannot borrow + | ^ second mutable borrow occurs here +LL | use_mut(_p); + | -- first borrow later used here + error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable --> $DIR/borrowck-borrow-overloaded-auto-deref-mut.rs:119:5 | @@ -62,6 +82,7 @@ LL | fn assign_method2<'a>(x: &'a Own) { LL | *x.y_mut() = 3; //~ ERROR cannot borrow | ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable -error: aborting due to 8 previous errors +error: aborting due to 10 previous errors -For more information about this error, try `rustc --explain E0596`. +Some errors occurred: E0499, E0596. +For more information about an error, try `rustc --explain E0499`. diff --git a/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.rs b/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.rs index 764d05be879b8..48eb2e239f7cd 100644 --- a/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.rs +++ b/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.rs @@ -86,8 +86,8 @@ fn deref_extend_mut_field3(x: &mut Own) { let _x = &mut x.x; let _y = &mut x.y; //~ ERROR cannot borrow + use_mut(_x); } - fn deref_extend_mut_field4<'a>(x: &'a mut Own) { let p = &mut **x; let _x = &mut p.x; @@ -109,8 +109,8 @@ fn assign_field3<'a>(x: &'a mut Own) { fn assign_field4<'a>(x: &'a mut Own) { let _p: &mut Point = &mut **x; x.y = 3; //~ ERROR cannot borrow + use_mut(_p); } - fn deref_imm_method(x: Own) { let __isize = x.get(); } @@ -148,3 +148,5 @@ fn assign_method3<'a>(x: &'a mut Own) { } pub fn main() {} + +fn use_mut(_: &mut T) {} diff --git a/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.stderr b/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.stderr index d91ff6964237b..864357fee9f0c 100644 --- a/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.stderr +++ b/src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.stderr @@ -21,6 +21,7 @@ LL | let _x = &mut x.x; | - first mutable borrow occurs here LL | let _y = &mut x.y; //~ ERROR cannot borrow | ^ second mutable borrow occurs here +LL | use_mut(_x); LL | } | - first borrow ends here @@ -47,6 +48,7 @@ LL | let _p: &mut Point = &mut **x; | -- first mutable borrow occurs here LL | x.y = 3; //~ ERROR cannot borrow | ^ second mutable borrow occurs here +LL | use_mut(_p); LL | } | - first borrow ends here From affddf68c623390aa5566dad6357892d9e0ae278 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 13:49:58 +0100 Subject: [PATCH 03/15] Make `ui/issues/issue-17263.rs` robust w.r.t. NLL. --- ...ue-17263.stderr => issue-17263.ast.stderr} | 2 +- src/test/ui/issues/issue-17263.nll.stderr | 6 +-- src/test/ui/issues/issue-17263.rs | 38 ++++++++++++------- 3 files changed, 29 insertions(+), 17 deletions(-) rename src/test/ui/issues/{issue-17263.stderr => issue-17263.ast.stderr} (93%) diff --git a/src/test/ui/issues/issue-17263.stderr b/src/test/ui/issues/issue-17263.ast.stderr similarity index 93% rename from src/test/ui/issues/issue-17263.stderr rename to src/test/ui/issues/issue-17263.ast.stderr index 4767fbbcfbbd5..3d42dcb52f5db 100644 --- a/src/test/ui/issues/issue-17263.stderr +++ b/src/test/ui/issues/issue-17263.ast.stderr @@ -16,7 +16,7 @@ LL | let (c, d) = (&mut foo.a, &foo.b); | ----- ^^^^^ immutable borrow occurs here (via `foo.b`) | | | mutable borrow occurs here (via `foo.a`) -LL | //~^ ERROR cannot borrow `foo` (via `foo.b`) as immutable +... LL | } | - mutable borrow ends here diff --git a/src/test/ui/issues/issue-17263.nll.stderr b/src/test/ui/issues/issue-17263.nll.stderr index d6009e8078dce..cdb574b8b9f94 100644 --- a/src/test/ui/issues/issue-17263.nll.stderr +++ b/src/test/ui/issues/issue-17263.nll.stderr @@ -1,12 +1,12 @@ error: compilation successful --> $DIR/issue-17263.rs:15:1 | -LL | / fn main() { #![rustc_error] // rust-lang/rust#49855 +LL | / fn main() { //[nll]~ ERROR compilation successful LL | | let mut x: Box<_> = box Foo { a: 1, b: 2 }; LL | | let (a, b) = (&mut x.a, &mut x.b); -LL | | //~^ ERROR cannot borrow `x` (via `x.b`) as mutable more than once at a time +LL | | //[ast]~^ ERROR cannot borrow `x` (via `x.b`) as mutable more than once at a time ... | -LL | | //~^ ERROR cannot borrow `foo` (via `foo.b`) as immutable +LL | | use_mut(a); LL | | } | |_^ diff --git a/src/test/ui/issues/issue-17263.rs b/src/test/ui/issues/issue-17263.rs index b251f9a415253..754f3b90aacf1 100644 --- a/src/test/ui/issues/issue-17263.rs +++ b/src/test/ui/issues/issue-17263.rs @@ -1,23 +1,35 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. +// This checks diagnostic quality for cases where AST-borrowck treated +// `Box` as other types (see rust-lang/rfcs#130). NLL again treats +// `Box` specially. We capture the differences via revisions. +// revisions: ast nll +//[ast]compile-flags: -Z borrowck=ast +//[nll]compile-flags: -Z borrowck=migrate -Z two-phase-borrows + +// don't worry about the --compare-mode=nll on this test. +// ignore-compare-mode-nll #![feature(box_syntax, rustc_attrs)] struct Foo { a: isize, b: isize } - -fn main() { #![rustc_error] // rust-lang/rust#49855 +#[rustc_error] // rust-lang/rust#49855 +fn main() { //[nll]~ ERROR compilation successful let mut x: Box<_> = box Foo { a: 1, b: 2 }; let (a, b) = (&mut x.a, &mut x.b); - //~^ ERROR cannot borrow `x` (via `x.b`) as mutable more than once at a time + //[ast]~^ ERROR cannot borrow `x` (via `x.b`) as mutable more than once at a time let mut foo: Box<_> = box Foo { a: 1, b: 2 }; let (c, d) = (&mut foo.a, &foo.b); - //~^ ERROR cannot borrow `foo` (via `foo.b`) as immutable + //[ast]~^ ERROR cannot borrow `foo` (via `foo.b`) as immutable + + // We explicitly use the references created above to illustrate + // that NLL is accepting this code *not* because of artificially + // short lifetimes, but rather because it understands that all the + // references are of disjoint parts of memory. + use_imm(d); + use_mut(c); + use_mut(b); + use_mut(a); } + +fn use_mut(_: &mut T) { } +fn use_imm(_: &T) { } From 9f9bf94b8dcb9acc4c0419dec256d1ece3139b1c Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 13:55:00 +0100 Subject: [PATCH 04/15] Make `ui/borrowck/borrowck-overloaded-index-move-index.rs` robust w.r.t. NLL. --- ...wck-overloaded-index-move-index.nll.stderr | 29 +++++++++++++++++-- .../borrowck-overloaded-index-move-index.rs | 4 +++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/test/ui/borrowck/borrowck-overloaded-index-move-index.nll.stderr b/src/test/ui/borrowck/borrowck-overloaded-index-move-index.nll.stderr index 824d8298ecbc6..198d086aa3be6 100644 --- a/src/test/ui/borrowck/borrowck-overloaded-index-move-index.nll.stderr +++ b/src/test/ui/borrowck/borrowck-overloaded-index-move-index.nll.stderr @@ -1,3 +1,27 @@ +error[E0505]: cannot move out of `s` because it is borrowed + --> $DIR/borrowck-overloaded-index-move-index.rs:60:22 + | +LL | let rs = &mut s; + | ------ borrow of `s` occurs here +LL | +LL | println!("{}", f[s]); + | ^ move out of `s` occurs here +... +LL | use_mut(rs); + | -- borrow later used here + +error[E0505]: cannot move out of `s` because it is borrowed + --> $DIR/borrowck-overloaded-index-move-index.rs:63:7 + | +LL | let rs = &mut s; + | ------ borrow of `s` occurs here +... +LL | f[s] = 10; + | ^ move out of `s` occurs here +... +LL | use_mut(rs); + | -- borrow later used here + error[E0382]: use of moved value: `s` --> $DIR/borrowck-overloaded-index-move-index.rs:63:7 | @@ -9,6 +33,7 @@ LL | f[s] = 10; | = note: move occurs because `s` has type `std::string::String`, which does not implement the `Copy` trait -error: aborting due to previous error +error: aborting due to 3 previous errors -For more information about this error, try `rustc --explain E0382`. +Some errors occurred: E0382, E0505. +For more information about an error, try `rustc --explain E0382`. diff --git a/src/test/ui/borrowck/borrowck-overloaded-index-move-index.rs b/src/test/ui/borrowck/borrowck-overloaded-index-move-index.rs index d8615d1905338..e95423a8e834d 100644 --- a/src/test/ui/borrowck/borrowck-overloaded-index-move-index.rs +++ b/src/test/ui/borrowck/borrowck-overloaded-index-move-index.rs @@ -71,4 +71,8 @@ fn main() { let _j = &i; println!("{}", s[i]); // no error, i is copy println!("{}", s[i]); + + use_mut(rs); } + +fn use_mut(_: &mut T) { } From 6c7d82e1ca246e914e96beb1874a75f7306ac9dd Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 14:05:23 +0100 Subject: [PATCH 05/15] Make `ui/borrowck/borrowck-reborrow-from-mut.rs` robust w.r.t. NLL. --- .../borrowck-reborrow-from-mut.nll.stderr | 109 +++++++++++++++++- .../ui/borrowck/borrowck-reborrow-from-mut.rs | 35 +++--- .../borrowck-reborrow-from-mut.stderr | 10 +- 3 files changed, 135 insertions(+), 19 deletions(-) diff --git a/src/test/ui/borrowck/borrowck-reborrow-from-mut.nll.stderr b/src/test/ui/borrowck/borrowck-reborrow-from-mut.nll.stderr index 4c81bb8eb3086..1b4f9e77da80b 100644 --- a/src/test/ui/borrowck/borrowck-reborrow-from-mut.nll.stderr +++ b/src/test/ui/borrowck/borrowck-reborrow-from-mut.nll.stderr @@ -1,3 +1,107 @@ +error[E0499]: cannot borrow `foo.bar1` as mutable more than once at a time + --> $DIR/borrowck-reborrow-from-mut.rs:23:17 + | +LL | let _bar1 = &mut foo.bar1; + | ------------- first mutable borrow occurs here +LL | let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow + | ^^^^^^^^^^^^^ second mutable borrow occurs here +LL | use_mut(_bar1); + | ----- first borrow later used here + +error[E0502]: cannot borrow `foo.bar1` as immutable because it is also borrowed as mutable + --> $DIR/borrowck-reborrow-from-mut.rs:28:17 + | +LL | let _bar1 = &mut foo.bar1; + | ------------- mutable borrow occurs here +LL | let _bar2 = &foo.bar1; //~ ERROR cannot borrow + | ^^^^^^^^^ immutable borrow occurs here +LL | use_mut(_bar1); + | ----- mutable borrow later used here + +error[E0502]: cannot borrow `foo.bar1` as mutable because it is also borrowed as immutable + --> $DIR/borrowck-reborrow-from-mut.rs:33:17 + | +LL | let _bar1 = &foo.bar1; + | --------- immutable borrow occurs here +LL | let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow + | ^^^^^^^^^^^^^ mutable borrow occurs here +LL | use_imm(_bar1); + | ----- immutable borrow later used here + +error[E0499]: cannot borrow `foo.bar1` as mutable more than once at a time + --> $DIR/borrowck-reborrow-from-mut.rs:55:21 + | +LL | let _bar1 = &mut foo.bar1; + | ------------- first mutable borrow occurs here +LL | match *foo { +LL | Foo { bar1: ref mut _bar1, bar2: _ } => {} + | ^^^^^^^^^^^^^ second mutable borrow occurs here +... +LL | use_mut(_bar1); + | ----- first borrow later used here + +error[E0502]: cannot borrow `foo.bar1` as immutable because it is also borrowed as mutable + --> $DIR/borrowck-reborrow-from-mut.rs:62:17 + | +LL | let _bar1 = &mut foo.bar1.int1; + | ------------------ mutable borrow occurs here +LL | let _foo1 = &foo.bar1; //~ ERROR cannot borrow + | ^^^^^^^^^ immutable borrow occurs here +LL | let _foo2 = &*foo; //~ ERROR cannot borrow +LL | use_mut(_bar1); + | ----- mutable borrow later used here + +error[E0502]: cannot borrow `*foo` as immutable because it is also borrowed as mutable + --> $DIR/borrowck-reborrow-from-mut.rs:63:17 + | +LL | let _bar1 = &mut foo.bar1.int1; + | ------------------ mutable borrow occurs here +LL | let _foo1 = &foo.bar1; //~ ERROR cannot borrow +LL | let _foo2 = &*foo; //~ ERROR cannot borrow + | ^^^^^ immutable borrow occurs here +LL | use_mut(_bar1); + | ----- mutable borrow later used here + +error[E0499]: cannot borrow `foo.bar1` as mutable more than once at a time + --> $DIR/borrowck-reborrow-from-mut.rs:68:17 + | +LL | let _bar1 = &mut foo.bar1.int1; + | ------------------ first mutable borrow occurs here +LL | let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow + | ^^^^^^^^^^^^^ second mutable borrow occurs here +LL | use_mut(_bar1); + | ----- first borrow later used here + +error[E0499]: cannot borrow `*foo` as mutable more than once at a time + --> $DIR/borrowck-reborrow-from-mut.rs:73:17 + | +LL | let _bar1 = &mut foo.bar1.int1; + | ------------------ first mutable borrow occurs here +LL | let _foo2 = &mut *foo; //~ ERROR cannot borrow + | ^^^^^^^^^ second mutable borrow occurs here +LL | use_mut(_bar1); + | ----- first borrow later used here + +error[E0502]: cannot borrow `foo.bar1` as mutable because it is also borrowed as immutable + --> $DIR/borrowck-reborrow-from-mut.rs:78:17 + | +LL | let _bar1 = &foo.bar1.int1; + | -------------- immutable borrow occurs here +LL | let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow + | ^^^^^^^^^^^^^ mutable borrow occurs here +LL | use_imm(_bar1); + | ----- immutable borrow later used here + +error[E0502]: cannot borrow `*foo` as mutable because it is also borrowed as immutable + --> $DIR/borrowck-reborrow-from-mut.rs:83:17 + | +LL | let _bar1 = &foo.bar1.int1; + | -------------- immutable borrow occurs here +LL | let _foo2 = &mut *foo; //~ ERROR cannot borrow + | ^^^^^^^^^ mutable borrow occurs here +LL | use_imm(_bar1); + | ----- immutable borrow later used here + error[E0596]: cannot borrow `foo.bar1` as mutable, as it is behind a `&` reference --> $DIR/borrowck-reborrow-from-mut.rs:98:17 | @@ -6,6 +110,7 @@ LL | fn borrow_mut_from_imm(foo: &Foo) { LL | let _bar1 = &mut foo.bar1; //~ ERROR cannot borrow | ^^^^^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be borrowed as mutable -error: aborting due to previous error +error: aborting due to 11 previous errors -For more information about this error, try `rustc --explain E0596`. +Some errors occurred: E0499, E0502, E0596. +For more information about an error, try `rustc --explain E0499`. diff --git a/src/test/ui/borrowck/borrowck-reborrow-from-mut.rs b/src/test/ui/borrowck/borrowck-reborrow-from-mut.rs index 6f5dfa67be50d..9235d900a7e79 100644 --- a/src/test/ui/borrowck/borrowck-reborrow-from-mut.rs +++ b/src/test/ui/borrowck/borrowck-reborrow-from-mut.rs @@ -21,79 +21,79 @@ struct Bar { fn borrow_same_field_twice_mut_mut(foo: &mut Foo) { let _bar1 = &mut foo.bar1; let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow + use_mut(_bar1); } - fn borrow_same_field_twice_mut_imm(foo: &mut Foo) { let _bar1 = &mut foo.bar1; let _bar2 = &foo.bar1; //~ ERROR cannot borrow + use_mut(_bar1); } - fn borrow_same_field_twice_imm_mut(foo: &mut Foo) { let _bar1 = &foo.bar1; let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow + use_imm(_bar1); } - fn borrow_same_field_twice_imm_imm(foo: &mut Foo) { let _bar1 = &foo.bar1; let _bar2 = &foo.bar1; + use_imm(_bar1); } - fn borrow_both_mut(foo: &mut Foo) { let _bar1 = &mut foo.bar1; let _bar2 = &mut foo.bar2; + use_mut(_bar1); } - fn borrow_both_mut_pattern(foo: &mut Foo) { match *foo { - Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {} + Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => + { use_mut(_bar1); use_mut(_bar2); } } } - fn borrow_var_and_pattern(foo: &mut Foo) { let _bar1 = &mut foo.bar1; match *foo { Foo { bar1: ref mut _bar1, bar2: _ } => {} //~^ ERROR cannot borrow } + use_mut(_bar1); } - fn borrow_mut_and_base_imm(foo: &mut Foo) { let _bar1 = &mut foo.bar1.int1; let _foo1 = &foo.bar1; //~ ERROR cannot borrow let _foo2 = &*foo; //~ ERROR cannot borrow + use_mut(_bar1); } - fn borrow_mut_and_base_mut(foo: &mut Foo) { let _bar1 = &mut foo.bar1.int1; let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow + use_mut(_bar1); } - fn borrow_mut_and_base_mut2(foo: &mut Foo) { let _bar1 = &mut foo.bar1.int1; let _foo2 = &mut *foo; //~ ERROR cannot borrow + use_mut(_bar1); } - fn borrow_imm_and_base_mut(foo: &mut Foo) { let _bar1 = &foo.bar1.int1; let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow + use_imm(_bar1); } - fn borrow_imm_and_base_mut2(foo: &mut Foo) { let _bar1 = &foo.bar1.int1; let _foo2 = &mut *foo; //~ ERROR cannot borrow + use_imm(_bar1); } - fn borrow_imm_and_base_imm(foo: &mut Foo) { let _bar1 = &foo.bar1.int1; let _foo1 = &foo.bar1; let _foo2 = &*foo; + use_imm(_bar1); } - fn borrow_mut_and_imm(foo: &mut Foo) { let _bar1 = &mut foo.bar1; let _foo1 = &foo.bar2; + use_mut(_bar1); } - fn borrow_mut_from_imm(foo: &Foo) { let _bar1 = &mut foo.bar1; //~ ERROR cannot borrow } @@ -101,6 +101,9 @@ fn borrow_mut_from_imm(foo: &Foo) { fn borrow_long_path_both_mut(foo: &mut Foo) { let _bar1 = &mut foo.bar1.int1; let _foo1 = &mut foo.bar2.int2; + use_mut(_bar1); } - fn main() {} + +fn use_mut(_: &mut T) { } +fn use_imm(_: &T) { } diff --git a/src/test/ui/borrowck/borrowck-reborrow-from-mut.stderr b/src/test/ui/borrowck/borrowck-reborrow-from-mut.stderr index 00660ff84841b..1310e38cb3ee6 100644 --- a/src/test/ui/borrowck/borrowck-reborrow-from-mut.stderr +++ b/src/test/ui/borrowck/borrowck-reborrow-from-mut.stderr @@ -5,6 +5,7 @@ LL | let _bar1 = &mut foo.bar1; | -------- first mutable borrow occurs here LL | let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow | ^^^^^^^^ second mutable borrow occurs here +LL | use_mut(_bar1); LL | } | - first borrow ends here @@ -15,6 +16,7 @@ LL | let _bar1 = &mut foo.bar1; | -------- mutable borrow occurs here LL | let _bar2 = &foo.bar1; //~ ERROR cannot borrow | ^^^^^^^^ immutable borrow occurs here +LL | use_mut(_bar1); LL | } | - mutable borrow ends here @@ -25,6 +27,7 @@ LL | let _bar1 = &foo.bar1; | -------- immutable borrow occurs here LL | let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow | ^^^^^^^^ mutable borrow occurs here +LL | use_imm(_bar1); LL | } | - immutable borrow ends here @@ -47,7 +50,7 @@ LL | let _bar1 = &mut foo.bar1.int1; | ------------- mutable borrow occurs here LL | let _foo1 = &foo.bar1; //~ ERROR cannot borrow | ^^^^^^^^ immutable borrow occurs here -LL | let _foo2 = &*foo; //~ ERROR cannot borrow +... LL | } | - mutable borrow ends here @@ -59,6 +62,7 @@ LL | let _bar1 = &mut foo.bar1.int1; LL | let _foo1 = &foo.bar1; //~ ERROR cannot borrow LL | let _foo2 = &*foo; //~ ERROR cannot borrow | ^^^^ immutable borrow occurs here +LL | use_mut(_bar1); LL | } | - mutable borrow ends here @@ -69,6 +73,7 @@ LL | let _bar1 = &mut foo.bar1.int1; | ------------- first mutable borrow occurs here LL | let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow | ^^^^^^^^ second mutable borrow occurs here +LL | use_mut(_bar1); LL | } | - first borrow ends here @@ -79,6 +84,7 @@ LL | let _bar1 = &mut foo.bar1.int1; | ------------- first mutable borrow occurs here LL | let _foo2 = &mut *foo; //~ ERROR cannot borrow | ^^^^ second mutable borrow occurs here +LL | use_mut(_bar1); LL | } | - first borrow ends here @@ -89,6 +95,7 @@ LL | let _bar1 = &foo.bar1.int1; | ------------- immutable borrow occurs here LL | let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow | ^^^^^^^^ mutable borrow occurs here +LL | use_imm(_bar1); LL | } | - immutable borrow ends here @@ -99,6 +106,7 @@ LL | let _bar1 = &foo.bar1.int1; | ------------- immutable borrow occurs here LL | let _foo2 = &mut *foo; //~ ERROR cannot borrow | ^^^^ mutable borrow occurs here +LL | use_imm(_bar1); LL | } | - immutable borrow ends here From 9843a38632dcf4f33325bbef57ef4a1e55c48d6d Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 14:32:00 +0100 Subject: [PATCH 06/15] Make `ui/borrowck/borrowck-unboxed-closures.rs` robust w.r.t. NLL. --- .../borrowck/borrowck-unboxed-closures.nll.stderr | 14 ++++++++++++-- src/test/ui/borrowck/borrowck-unboxed-closures.rs | 4 +++- .../ui/borrowck/borrowck-unboxed-closures.stderr | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/test/ui/borrowck/borrowck-unboxed-closures.nll.stderr b/src/test/ui/borrowck/borrowck-unboxed-closures.nll.stderr index 3fbb747db24f1..ee5ad58290e9e 100644 --- a/src/test/ui/borrowck/borrowck-unboxed-closures.nll.stderr +++ b/src/test/ui/borrowck/borrowck-unboxed-closures.nll.stderr @@ -1,3 +1,13 @@ +error[E0502]: cannot borrow `f` as immutable because it is also borrowed as mutable + --> $DIR/borrowck-unboxed-closures.rs:13:5 + | +LL | let g = &mut f; + | ------ mutable borrow occurs here +LL | f(1, 2); //~ ERROR cannot borrow `f` as immutable + | ^ immutable borrow occurs here +LL | use_mut(g); + | - mutable borrow later used here + error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable --> $DIR/borrowck-unboxed-closures.rs:17:5 | @@ -16,7 +26,7 @@ LL | f(1, 2); //~ ERROR use of moved value | = note: move occurs because `f` has type `F`, which does not implement the `Copy` trait -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors -Some errors occurred: E0382, E0596. +Some errors occurred: E0382, E0502, E0596. For more information about an error, try `rustc --explain E0382`. diff --git a/src/test/ui/borrowck/borrowck-unboxed-closures.rs b/src/test/ui/borrowck/borrowck-unboxed-closures.rs index 4813b4b6a72cd..43f143a492fd6 100644 --- a/src/test/ui/borrowck/borrowck-unboxed-closures.rs +++ b/src/test/ui/borrowck/borrowck-unboxed-closures.rs @@ -11,8 +11,8 @@ fn a isize>(mut f: F) { let g = &mut f; f(1, 2); //~ ERROR cannot borrow `f` as immutable + use_mut(g); } - fn b isize>(f: F) { f(1, 2); //~ ERROR cannot borrow immutable argument } @@ -23,3 +23,5 @@ fn c isize>(f: F) { } fn main() {} + +fn use_mut(_: &mut T) { } diff --git a/src/test/ui/borrowck/borrowck-unboxed-closures.stderr b/src/test/ui/borrowck/borrowck-unboxed-closures.stderr index 0c067c47004cf..6ee1a6245a556 100644 --- a/src/test/ui/borrowck/borrowck-unboxed-closures.stderr +++ b/src/test/ui/borrowck/borrowck-unboxed-closures.stderr @@ -5,6 +5,7 @@ LL | let g = &mut f; | - mutable borrow occurs here LL | f(1, 2); //~ ERROR cannot borrow `f` as immutable | ^ immutable borrow occurs here +LL | use_mut(g); LL | } | - mutable borrow ends here From 41a1ee923ee9b3d1e9eb4aa76c048f4f1d3ede12 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 14:36:58 +0100 Subject: [PATCH 07/15] Make `ui/unop-move-semantics.rs` robust w.r.t. NLL. --- src/test/ui/unop-move-semantics.nll.stderr | 27 ++++++++++++++++++++-- src/test/ui/unop-move-semantics.rs | 7 ++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/test/ui/unop-move-semantics.nll.stderr b/src/test/ui/unop-move-semantics.nll.stderr index 111940aab2c32..bfc7736b2f32c 100644 --- a/src/test/ui/unop-move-semantics.nll.stderr +++ b/src/test/ui/unop-move-semantics.nll.stderr @@ -9,6 +9,29 @@ LL | x.clone(); //~ ERROR: use of moved value | = note: move occurs because `x` has type `T`, which does not implement the `Copy` trait +error[E0505]: cannot move out of `x` because it is borrowed + --> $DIR/unop-move-semantics.rs:25:6 + | +LL | let m = &x; + | -- borrow of `x` occurs here +... +LL | !x; //~ ERROR: cannot move out of `x` because it is borrowed + | ^ move out of `x` occurs here +... +LL | use_mut(n); use_imm(m); + | - borrow later used here + +error[E0505]: cannot move out of `y` because it is borrowed + --> $DIR/unop-move-semantics.rs:27:6 + | +LL | let n = &mut y; + | ------ borrow of `y` occurs here +... +LL | !y; //~ ERROR: cannot move out of `y` because it is borrowed + | ^ move out of `y` occurs here +LL | use_mut(n); use_imm(m); + | - borrow later used here + error[E0507]: cannot move out of borrowed content --> $DIR/unop-move-semantics.rs:34:6 | @@ -21,7 +44,7 @@ error[E0507]: cannot move out of borrowed content LL | !*n; //~ ERROR: cannot move out of borrowed content | ^^ cannot move out of borrowed content -error: aborting due to 3 previous errors +error: aborting due to 5 previous errors -Some errors occurred: E0382, E0507. +Some errors occurred: E0382, E0505, E0507. For more information about an error, try `rustc --explain E0382`. diff --git a/src/test/ui/unop-move-semantics.rs b/src/test/ui/unop-move-semantics.rs index 946566675981e..fcbbe546a3164 100644 --- a/src/test/ui/unop-move-semantics.rs +++ b/src/test/ui/unop-move-semantics.rs @@ -25,8 +25,8 @@ fn move_borrowed>(x: T, mut y: T) { !x; //~ ERROR: cannot move out of `x` because it is borrowed !y; //~ ERROR: cannot move out of `y` because it is borrowed + use_mut(n); use_imm(m); } - fn illegal_dereference>(mut x: T, y: T) { let m = &mut x; let n = &y; @@ -34,6 +34,9 @@ fn illegal_dereference>(mut x: T, y: T) { !*m; //~ ERROR: cannot move out of borrowed content !*n; //~ ERROR: cannot move out of borrowed content + use_imm(n); use_mut(m); } - fn main() {} + +fn use_mut(_: &mut T) { } +fn use_imm(_: &T) { } From 62a2bb129499d7f9cf84ec21874775611ac67125 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 14:44:14 +0100 Subject: [PATCH 08/15] Remove `println!`'s from `ui/issues/issue-52126-assign-op-invariance.rs` This is not strictly necessary to make this test "more robust with respect to NLL"; its just an attempt to narrow the scope of the test and focus on its core. --- .../ui/issues/issue-52126-assign-op-invariance.nll.stderr | 6 +++--- src/test/ui/issues/issue-52126-assign-op-invariance.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/ui/issues/issue-52126-assign-op-invariance.nll.stderr b/src/test/ui/issues/issue-52126-assign-op-invariance.nll.stderr index b25b063f3b6fb..2165d951102c2 100644 --- a/src/test/ui/issues/issue-52126-assign-op-invariance.nll.stderr +++ b/src/test/ui/issues/issue-52126-assign-op-invariance.nll.stderr @@ -3,9 +3,9 @@ error[E0597]: `line` does not live long enough | LL | let v: Vec<&str> = line.split_whitespace().collect(); | ^^^^ borrowed value does not live long enough -LL | //~^ ERROR `line` does not live long enough -LL | println!("accumulator before add_assign {:?}", acc.map); - | ------- borrow used here, in later iteration of loop +... +LL | acc += cnt2; + | --- borrow used here, in later iteration of loop ... LL | } | - `line` dropped here while still borrowed diff --git a/src/test/ui/issues/issue-52126-assign-op-invariance.rs b/src/test/ui/issues/issue-52126-assign-op-invariance.rs index b26ad9bc37dd1..1a353f9ea7cd3 100644 --- a/src/test/ui/issues/issue-52126-assign-op-invariance.rs +++ b/src/test/ui/issues/issue-52126-assign-op-invariance.rs @@ -43,7 +43,7 @@ pub fn panics() { for line in vec!["123456789".to_string(), "12345678".to_string()] { let v: Vec<&str> = line.split_whitespace().collect(); //~^ ERROR `line` does not live long enough - println!("accumulator before add_assign {:?}", acc.map); + // println!("accumulator before add_assign {:?}", acc.map); let mut map = HashMap::new(); for str_ref in v { let e = map.entry(str_ref); @@ -53,7 +53,7 @@ pub fn panics() { } let cnt2 = Counter{map}; acc += cnt2; - println!("accumulator after add_assign {:?}", acc.map); + // println!("accumulator after add_assign {:?}", acc.map); // line gets dropped here but references are kept in acc.map } } From e940801592b123e6ff03b355137ca568d11b4c4d Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 14:48:35 +0100 Subject: [PATCH 09/15] Make `ui/binop-move-semantics.rs` robust w.r.t. NLL. --- .../ui/binop/binop-move-semantics.nll.stderr | 27 +++++++++++++++++-- src/test/ui/binop/binop-move-semantics.rs | 7 +++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/test/ui/binop/binop-move-semantics.nll.stderr b/src/test/ui/binop/binop-move-semantics.nll.stderr index 545a60f6770d9..612375f904783 100644 --- a/src/test/ui/binop/binop-move-semantics.nll.stderr +++ b/src/test/ui/binop/binop-move-semantics.nll.stderr @@ -20,6 +20,29 @@ LL | x.clone(); //~ ERROR: use of moved value | = note: move occurs because `x` has type `T`, which does not implement the `Copy` trait +error[E0505]: cannot move out of `x` because it is borrowed + --> $DIR/binop-move-semantics.rs:31:5 + | +LL | let m = &x; + | -- borrow of `x` occurs here +... +LL | x //~ ERROR: cannot move out of `x` because it is borrowed + | ^ move out of `x` occurs here +... +LL | use_mut(n); use_imm(m); + | - borrow later used here + +error[E0505]: cannot move out of `y` because it is borrowed + --> $DIR/binop-move-semantics.rs:33:5 + | +LL | let n = &mut y; + | ------ borrow of `y` occurs here +... +LL | y; //~ ERROR: cannot move out of `y` because it is borrowed + | ^ move out of `y` occurs here +LL | use_mut(n); use_imm(m); + | - borrow later used here + error[E0507]: cannot move out of borrowed content --> $DIR/binop-move-semantics.rs:40:5 | @@ -62,7 +85,7 @@ LL | | &mut f; //~ ERROR: cannot borrow `f` as mutable because it is also b | | immutable borrow later used here | mutable borrow occurs here -error: aborting due to 6 previous errors +error: aborting due to 8 previous errors -Some errors occurred: E0382, E0502, E0507. +Some errors occurred: E0382, E0502, E0505, E0507. For more information about an error, try `rustc --explain E0382`. diff --git a/src/test/ui/binop/binop-move-semantics.rs b/src/test/ui/binop/binop-move-semantics.rs index cff0064497aff..f6fad8b46dd9d 100644 --- a/src/test/ui/binop/binop-move-semantics.rs +++ b/src/test/ui/binop/binop-move-semantics.rs @@ -31,8 +31,8 @@ fn move_borrowed>(x: T, mut y: T) { x //~ ERROR: cannot move out of `x` because it is borrowed + y; //~ ERROR: cannot move out of `y` because it is borrowed + use_mut(n); use_imm(m); } - fn illegal_dereference>(mut x: T, y: T) { let m = &mut x; let n = &y; @@ -40,8 +40,8 @@ fn illegal_dereference>(mut x: T, y: T) { *m //~ ERROR: cannot move out of borrowed content + *n; //~ ERROR: cannot move out of borrowed content + use_imm(n); use_mut(m); } - struct Foo; impl<'a, 'b> Add<&'b Foo> for &'a mut Foo { @@ -73,3 +73,6 @@ fn immut_plus_mut() { } fn main() {} + +fn use_mut(_: &mut T) { } +fn use_imm(_: &T) { } From f7ded5dcb6d917f4cd68ec7781c655dbe030f000 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 14:55:30 +0100 Subject: [PATCH 10/15] Removed overlapping_spans.{rs,stderr,nll.stderr}. This is based on the feedback from estebank: """ I believe that test can be removed outright. It'd be impossible for a new change to go through that breaks this kind of output without it being picked up by multiple other `stderr` tests. This is an artifact of the transition period to the "new" output style. """ see: https://github.com/rust-lang/rust/issues/52663#issuecomment-422155551 --- .../overlapping_spans.nll.stderr | 17 -------------- .../ui/codemap_tests/overlapping_spans.rs | 23 ------------------- .../ui/codemap_tests/overlapping_spans.stderr | 12 ---------- 3 files changed, 52 deletions(-) delete mode 100644 src/test/ui/codemap_tests/overlapping_spans.nll.stderr delete mode 100644 src/test/ui/codemap_tests/overlapping_spans.rs delete mode 100644 src/test/ui/codemap_tests/overlapping_spans.stderr diff --git a/src/test/ui/codemap_tests/overlapping_spans.nll.stderr b/src/test/ui/codemap_tests/overlapping_spans.nll.stderr deleted file mode 100644 index e334472f9d6e1..0000000000000 --- a/src/test/ui/codemap_tests/overlapping_spans.nll.stderr +++ /dev/null @@ -1,17 +0,0 @@ -error[E0509]: cannot move out of type `S`, which implements the `Drop` trait - --> $DIR/overlapping_spans.rs:20:11 - | -LL | match (S {f:"foo".to_string()}) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot move out of here -LL | S {f:_s} => {} //~ ERROR cannot move out - | -- data moved here - | -note: move occurs because `_s` has type `std::string::String`, which does not implement the `Copy` trait - --> $DIR/overlapping_spans.rs:21:14 - | -LL | S {f:_s} => {} //~ ERROR cannot move out - | ^^ - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0509`. diff --git a/src/test/ui/codemap_tests/overlapping_spans.rs b/src/test/ui/codemap_tests/overlapping_spans.rs deleted file mode 100644 index 467e90bd5a51b..0000000000000 --- a/src/test/ui/codemap_tests/overlapping_spans.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[derive(Debug)] -struct Foo { } - -struct S {f:String} -impl Drop for S { - fn drop(&mut self) { println!("{}", self.f); } -} - -fn main() { - match (S {f:"foo".to_string()}) { - S {f:_s} => {} //~ ERROR cannot move out - } -} diff --git a/src/test/ui/codemap_tests/overlapping_spans.stderr b/src/test/ui/codemap_tests/overlapping_spans.stderr deleted file mode 100644 index 62a4f08e15661..0000000000000 --- a/src/test/ui/codemap_tests/overlapping_spans.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0509]: cannot move out of type `S`, which implements the `Drop` trait - --> $DIR/overlapping_spans.rs:21:9 - | -LL | S {f:_s} => {} //~ ERROR cannot move out - | ^^^^^--^ - | | | - | | hint: to prevent move, use `ref _s` or `ref mut _s` - | cannot move out of here - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0509`. From fe29cd0a3d1ebff0d90f3e4ebc929f944863be4c Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 15:24:25 +0100 Subject: [PATCH 11/15] Add `ui/borrowck/borrowck-closures-mut-of-mut.rs`. This is a variant of `ui/borrowck/borrowck-closures-mut-of-imm.rs` that I used to help identify what changes I needed to make to the latter file in order to recover its instances of E0524 under NLL. (Basically this test includes the changes you'd need to make to `ui/borrowck/borrowck-closures-mut-of-imm.rs` in order to get rid of occurrences of E0596. And then I realized that one needs to add invocations of the closures in order to properly extend the mutable reborrows in a manner such that NLL will roughly match AST-borrowck.) --- .../borrowck-closures-mut-of-mut.nll.stderr | 18 +++++++++++++++++ .../borrowck/borrowck-closures-mut-of-mut.rs | 20 +++++++++++++++++++ .../borrowck-closures-mut-of-mut.stderr | 18 +++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/test/ui/borrowck/borrowck-closures-mut-of-mut.nll.stderr create mode 100644 src/test/ui/borrowck/borrowck-closures-mut-of-mut.rs create mode 100644 src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr diff --git a/src/test/ui/borrowck/borrowck-closures-mut-of-mut.nll.stderr b/src/test/ui/borrowck/borrowck-closures-mut-of-mut.nll.stderr new file mode 100644 index 0000000000000..18f95f232cdd3 --- /dev/null +++ b/src/test/ui/borrowck/borrowck-closures-mut-of-mut.nll.stderr @@ -0,0 +1,18 @@ +error[E0524]: two closures require unique access to `x` at the same time + --> $DIR/borrowck-closures-mut-of-mut.rs:14:18 + | +LL | let mut c1 = || set(&mut *x); + | -- - first borrow occurs due to use of `x` in closure + | | + | first closure is constructed here +LL | let mut c2 = || set(&mut *x); + | ^^ - second borrow occurs due to use of `x` in closure + | | + | second closure is constructed here +LL | //~^ ERROR two closures require unique access to `x` at the same time +LL | c2(); c1(); + | -- first borrow later used here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0524`. diff --git a/src/test/ui/borrowck/borrowck-closures-mut-of-mut.rs b/src/test/ui/borrowck/borrowck-closures-mut-of-mut.rs new file mode 100644 index 0000000000000..50c6f2c585ed9 --- /dev/null +++ b/src/test/ui/borrowck/borrowck-closures-mut-of-mut.rs @@ -0,0 +1,20 @@ +// Tests that two closures cannot simultaneously both have mutable +// access to the variable. Related to issue #6801. + +fn get(x: &isize) -> isize { + *x +} + +fn set(x: &mut isize) { + *x = 4; +} + +fn a(x: &mut isize) { + let mut c1 = || set(&mut *x); + let mut c2 = || set(&mut *x); + //~^ ERROR two closures require unique access to `x` at the same time + c2(); c1(); +} + +fn main() { +} diff --git a/src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr b/src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr new file mode 100644 index 0000000000000..2c5587710a154 --- /dev/null +++ b/src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr @@ -0,0 +1,18 @@ +error[E0524]: two closures require unique access to `x` at the same time + --> $DIR/borrowck-closures-mut-of-mut.rs:14:18 + | +LL | let mut c1 = || set(&mut *x); + | -- - previous borrow occurs due to use of `x` in closure + | | + | first closure is constructed here +LL | let mut c2 = || set(&mut *x); + | ^^ - borrow occurs due to use of `x` in closure + | | + | second closure is constructed here +... +LL | } + | - borrow from first closure ends here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0524`. From c25319fcfc5046b91b773c87edbfdb087e875343 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 15:25:11 +0100 Subject: [PATCH 12/15] Update `ui/borrowck/borrowck-closures-mut-of-imm.rs` robust w.r.t. NLL. --- .../borrowck-closures-mut-of-imm.nll.stderr | 33 ++++++++++++++----- .../borrowck/borrowck-closures-mut-of-imm.rs | 5 +-- .../borrowck-closures-mut-of-imm.stderr | 30 ++++++++--------- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.nll.stderr b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.nll.stderr index e8fae63a5d617..160a84c480cd3 100644 --- a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.nll.stderr +++ b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.nll.stderr @@ -1,15 +1,32 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference - --> $DIR/borrowck-closures-mut-of-imm.rs:23:21 + --> $DIR/borrowck-closures-mut-of-imm.rs:23:25 | -LL | let c1 = || set(&mut *x); - | ^^^^^^^ cannot borrow as mutable +LL | let mut c1 = || set(&mut *x); + | ^^^^^^^ cannot borrow as mutable error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference - --> $DIR/borrowck-closures-mut-of-imm.rs:25:21 + --> $DIR/borrowck-closures-mut-of-imm.rs:25:25 | -LL | let c2 = || set(&mut *x); - | ^^^^^^^ cannot borrow as mutable +LL | let mut c2 = || set(&mut *x); + | ^^^^^^^ cannot borrow as mutable -error: aborting due to 2 previous errors +error[E0524]: two closures require unique access to `x` at the same time + --> $DIR/borrowck-closures-mut-of-imm.rs:25:18 + | +LL | let mut c1 = || set(&mut *x); + | -- - first borrow occurs due to use of `x` in closure + | | + | first closure is constructed here +LL | //~^ ERROR cannot borrow +LL | let mut c2 = || set(&mut *x); + | ^^ - second borrow occurs due to use of `x` in closure + | | + | second closure is constructed here +... +LL | c2(); c1(); + | -- first borrow later used here + +error: aborting due to 3 previous errors -For more information about this error, try `rustc --explain E0596`. +Some errors occurred: E0524, E0596. +For more information about an error, try `rustc --explain E0524`. diff --git a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs index dc2f0e8395f08..3bf4f17fde1a8 100644 --- a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs +++ b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs @@ -20,11 +20,12 @@ fn set(x: &mut isize) { } fn a(x: &isize) { - let c1 = || set(&mut *x); + let mut c1 = || set(&mut *x); //~^ ERROR cannot borrow - let c2 = || set(&mut *x); + let mut c2 = || set(&mut *x); //~^ ERROR cannot borrow //~| ERROR two closures require unique access to `x` at the same time + c2(); c1(); } fn main() { diff --git a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr index 87eb52b6aa605..c248595d57119 100644 --- a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr +++ b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr @@ -1,30 +1,30 @@ error[E0524]: two closures require unique access to `x` at the same time - --> $DIR/borrowck-closures-mut-of-imm.rs:25:14 + --> $DIR/borrowck-closures-mut-of-imm.rs:25:18 | -LL | let c1 = || set(&mut *x); - | -- - previous borrow occurs due to use of `x` in closure - | | - | first closure is constructed here +LL | let mut c1 = || set(&mut *x); + | -- - previous borrow occurs due to use of `x` in closure + | | + | first closure is constructed here LL | //~^ ERROR cannot borrow -LL | let c2 = || set(&mut *x); - | ^^ - borrow occurs due to use of `x` in closure - | | - | second closure is constructed here +LL | let mut c2 = || set(&mut *x); + | ^^ - borrow occurs due to use of `x` in closure + | | + | second closure is constructed here ... LL | } | - borrow from first closure ends here error[E0596]: cannot borrow immutable borrowed content `***x` as mutable - --> $DIR/borrowck-closures-mut-of-imm.rs:23:26 + --> $DIR/borrowck-closures-mut-of-imm.rs:23:30 | -LL | let c1 = || set(&mut *x); - | ^^ cannot borrow as mutable +LL | let mut c1 = || set(&mut *x); + | ^^ cannot borrow as mutable error[E0596]: cannot borrow immutable borrowed content `***x` as mutable - --> $DIR/borrowck-closures-mut-of-imm.rs:25:26 + --> $DIR/borrowck-closures-mut-of-imm.rs:25:30 | -LL | let c2 = || set(&mut *x); - | ^^ cannot borrow as mutable +LL | let mut c2 = || set(&mut *x); + | ^^ cannot borrow as mutable error: aborting due to 3 previous errors From cf715827187791a82dd8401ca210d1e881b8e3e5 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 16:19:51 +0100 Subject: [PATCH 13/15] Use `// revisions` in the dropck-eyepatch tests instead of relying on compare-mode=nll. NLL has increased precision in its analysis of drop order, and we want the test annotations to deliberately reflect this by having fewer ERROR annotations for NLL than for AST-borrowck. The best way to get this effect is via `// revisions`. As a drive-by, also added uses of all the borrows just to make it clear that NLL isn't somehow sidestepping things by using shorter borrows than you might have otherwise expected. (Of course, the added uses do not make all that much difference since the relevant types all declare `impl Drop` and thus those drops have implicit uses anyway.) --- ...> dropck-eyepatch-extern-crate.ast.stderr} | 2 +- .../ui/dropck/dropck-eyepatch-extern-crate.rs | 35 ++++++++++--------- ...err => dropck-eyepatch-reorder.ast.stderr} | 2 +- src/test/ui/dropck/dropck-eyepatch-reorder.rs | 35 ++++++++++--------- ...atch.stderr => dropck-eyepatch.ast.stderr} | 2 +- src/test/ui/dropck/dropck-eyepatch.rs | 35 ++++++++++--------- 6 files changed, 60 insertions(+), 51 deletions(-) rename src/test/ui/dropck/{dropck-eyepatch-extern-crate.stderr => dropck-eyepatch-extern-crate.ast.stderr} (97%) rename src/test/ui/dropck/{dropck-eyepatch-reorder.stderr => dropck-eyepatch-reorder.ast.stderr} (98%) rename src/test/ui/dropck/{dropck-eyepatch.stderr => dropck-eyepatch.ast.stderr} (98%) diff --git a/src/test/ui/dropck/dropck-eyepatch-extern-crate.stderr b/src/test/ui/dropck/dropck-eyepatch-extern-crate.ast.stderr similarity index 97% rename from src/test/ui/dropck/dropck-eyepatch-extern-crate.stderr rename to src/test/ui/dropck/dropck-eyepatch-extern-crate.ast.stderr index 35db46f4faeb1..31adb2f3f1471 100644 --- a/src/test/ui/dropck/dropck-eyepatch-extern-crate.stderr +++ b/src/test/ui/dropck/dropck-eyepatch-extern-crate.ast.stderr @@ -32,7 +32,7 @@ LL | } = note: values in a scope are dropped in the opposite order they are created error[E0597]: `c_shortest` does not live long enough - --> $DIR/dropck-eyepatch-extern-crate.rs:49:20 + --> $DIR/dropck-eyepatch-extern-crate.rs:50:20 | LL | dr = Dr("dr", &c_shortest); | ^^^^^^^^^^ borrowed value does not live long enough diff --git a/src/test/ui/dropck/dropck-eyepatch-extern-crate.rs b/src/test/ui/dropck/dropck-eyepatch-extern-crate.rs index 3e531d9fd6011..68065639398a5 100644 --- a/src/test/ui/dropck/dropck-eyepatch-extern-crate.rs +++ b/src/test/ui/dropck/dropck-eyepatch-extern-crate.rs @@ -1,12 +1,12 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. +// The behavior of AST-borrowck and NLL explcitly differ here due to +// NLL's increased precision; so we use revisions and do not worry +// about the --compare-mode=nll on this test. + +// revisions: ast nll +//[ast]compile-flags: -Z borrowck=ast +//[nll]compile-flags: -Z borrowck=migrate -Z two-phase-borrows + +// ignore-compare-mode-nll // aux-build:dropck_eyepatch_extern_crate.rs @@ -39,29 +39,32 @@ fn main() { #![rustc_error] // rust-lang/rust#49855 // Error: destructor order imprecisely modelled dt = Dt("dt", &c); - //~^ ERROR `c` does not live long enough + //[ast]~^ ERROR `c` does not live long enough dr = Dr("dr", &c); - //~^ ERROR `c` does not live long enough + //[ast]~^ ERROR `c` does not live long enough // Error: `c_shortest` dies too soon for the references in dtors to be valid. dt = Dt("dt", &c_shortest); - //~^ ERROR `c_shortest` does not live long enough + //[ast]~^ ERROR `c_shortest` does not live long enough + //[nll]~^^ ERROR `c_shortest` does not live long enough dr = Dr("dr", &c_shortest); - //~^ ERROR `c_shortest` does not live long enough - + //[ast]~^ ERROR `c_shortest` does not live long enough // No error: Drop impl asserts .1 (A and &'a _) are not accessed pt = Pt("pt", &c_shortest, &c_long); pr = Pr("pr", &c_shortest, &c_long); // Error: Drop impl's assertion does not apply to `B` nor `&'b _` pt = Pt("pt", &c_long, &c_shortest); - //~^ ERROR `c_shortest` does not live long enough + //[ast]~^ ERROR `c_shortest` does not live long enough pr = Pr("pr", &c_long, &c_shortest); - //~^ ERROR `c_shortest` does not live long enough + //[ast]~^ ERROR `c_shortest` does not live long enough // No error: St and Sr have no destructor. st = St("st", &c_shortest); sr = Sr("sr", &c_shortest); println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0)); + use_imm(sr.1); use_imm(st.1); use_imm(pr.1); use_imm(pt.1); use_imm(dr.1); use_imm(dt.1); } + +fn use_imm(_: &T) { } diff --git a/src/test/ui/dropck/dropck-eyepatch-reorder.stderr b/src/test/ui/dropck/dropck-eyepatch-reorder.ast.stderr similarity index 98% rename from src/test/ui/dropck/dropck-eyepatch-reorder.stderr rename to src/test/ui/dropck/dropck-eyepatch-reorder.ast.stderr index 9984a7b9409c4..ddd47e9743497 100644 --- a/src/test/ui/dropck/dropck-eyepatch-reorder.stderr +++ b/src/test/ui/dropck/dropck-eyepatch-reorder.ast.stderr @@ -32,7 +32,7 @@ LL | } = note: values in a scope are dropped in the opposite order they are created error[E0597]: `c_shortest` does not live long enough - --> $DIR/dropck-eyepatch-reorder.rs:66:20 + --> $DIR/dropck-eyepatch-reorder.rs:67:20 | LL | dr = Dr("dr", &c_shortest); | ^^^^^^^^^^ borrowed value does not live long enough diff --git a/src/test/ui/dropck/dropck-eyepatch-reorder.rs b/src/test/ui/dropck/dropck-eyepatch-reorder.rs index 1806dc7142452..16aaa26125768 100644 --- a/src/test/ui/dropck/dropck-eyepatch-reorder.rs +++ b/src/test/ui/dropck/dropck-eyepatch-reorder.rs @@ -1,12 +1,12 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. +// The behavior of AST-borrowck and NLL explcitly differ here due to +// NLL's increased precision; so we use revisions and do not worry +// about the --compare-mode=nll on this test. + +// revisions: ast nll +//[ast]compile-flags: -Z borrowck=ast +//[nll]compile-flags: -Z borrowck=migrate -Z two-phase-borrows + +// ignore-compare-mode-nll #![feature(dropck_eyepatch, rustc_attrs)] @@ -56,29 +56,32 @@ fn main() { #![rustc_error] // rust-lang/rust#49855 // Error: destructor order imprecisely modelled dt = Dt("dt", &c); - //~^ ERROR `c` does not live long enough + //[ast]~^ ERROR `c` does not live long enough dr = Dr("dr", &c); - //~^ ERROR `c` does not live long enough + //[ast]~^ ERROR `c` does not live long enough // Error: `c_shortest` dies too soon for the references in dtors to be valid. dt = Dt("dt", &c_shortest); - //~^ ERROR `c_shortest` does not live long enough + //[ast]~^ ERROR `c_shortest` does not live long enough + //[nll]~^^ ERROR `c_shortest` does not live long enough dr = Dr("dr", &c_shortest); - //~^ ERROR `c_shortest` does not live long enough - + //[ast]~^ ERROR `c_shortest` does not live long enough // No error: Drop impl asserts .1 (A and &'a _) are not accessed pt = Pt("pt", &c_shortest, &c_long); pr = Pr("pr", &c_shortest, &c_long); // Error: Drop impl's assertion does not apply to `B` nor `&'b _` pt = Pt("pt", &c_long, &c_shortest); - //~^ ERROR `c_shortest` does not live long enough + //[ast]~^ ERROR `c_shortest` does not live long enough pr = Pr("pr", &c_long, &c_shortest); - //~^ ERROR `c_shortest` does not live long enough + //[ast]~^ ERROR `c_shortest` does not live long enough // No error: St and Sr have no destructor. st = St("st", &c_shortest); sr = Sr("sr", &c_shortest); println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0)); + use_imm(sr.1); use_imm(st.1); use_imm(pr.1); use_imm(pt.1); use_imm(dr.1); use_imm(dt.1); } + +fn use_imm(_: &T) { } diff --git a/src/test/ui/dropck/dropck-eyepatch.stderr b/src/test/ui/dropck/dropck-eyepatch.ast.stderr similarity index 98% rename from src/test/ui/dropck/dropck-eyepatch.stderr rename to src/test/ui/dropck/dropck-eyepatch.ast.stderr index 7cdf645941d09..0952ed0d6b793 100644 --- a/src/test/ui/dropck/dropck-eyepatch.stderr +++ b/src/test/ui/dropck/dropck-eyepatch.ast.stderr @@ -32,7 +32,7 @@ LL | } = note: values in a scope are dropped in the opposite order they are created error[E0597]: `c_shortest` does not live long enough - --> $DIR/dropck-eyepatch.rs:89:20 + --> $DIR/dropck-eyepatch.rs:90:20 | LL | dr = Dr("dr", &c_shortest); | ^^^^^^^^^^ borrowed value does not live long enough diff --git a/src/test/ui/dropck/dropck-eyepatch.rs b/src/test/ui/dropck/dropck-eyepatch.rs index 40d3ff050e2aa..d7a671fd33c2c 100644 --- a/src/test/ui/dropck/dropck-eyepatch.rs +++ b/src/test/ui/dropck/dropck-eyepatch.rs @@ -1,12 +1,12 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. +// The behavior of AST-borrowck and NLL explcitly differ here due to +// NLL's increased precision; so we use revisions and do not worry +// about the --compare-mode=nll on this test. + +// revisions: ast nll +//[ast]compile-flags: -Z borrowck=ast +//[nll]compile-flags: -Z borrowck=migrate -Z two-phase-borrows + +// ignore-compare-mode-nll #![feature(dropck_eyepatch, rustc_attrs)] @@ -79,16 +79,16 @@ fn main() { #![rustc_error] // rust-lang/rust#49855 // Error: destructor order imprecisely modelled dt = Dt("dt", &c); - //~^ ERROR `c` does not live long enough + //[ast]~^ ERROR `c` does not live long enough dr = Dr("dr", &c); - //~^ ERROR `c` does not live long enough + //[ast]~^ ERROR `c` does not live long enough // Error: `c_shortest` dies too soon for the references in dtors to be valid. dt = Dt("dt", &c_shortest); - //~^ ERROR `c_shortest` does not live long enough + //[ast]~^ ERROR `c_shortest` does not live long enough + //[nll]~^^ ERROR `c_shortest` does not live long enough dr = Dr("dr", &c_shortest); - //~^ ERROR `c_shortest` does not live long enough - + //[ast]~^ ERROR `c_shortest` does not live long enough // No error: Drop impl asserts .1 (A and &'a _) are not accessed pt = Pt("pt", &c_shortest, &c_long); @@ -96,13 +96,16 @@ fn main() { #![rustc_error] // rust-lang/rust#49855 // Error: Drop impl's assertion does not apply to `B` nor `&'b _` pt = Pt("pt", &c_long, &c_shortest); - //~^ ERROR `c_shortest` does not live long enough + //[ast]~^ ERROR `c_shortest` does not live long enough pr = Pr("pr", &c_long, &c_shortest); - //~^ ERROR `c_shortest` does not live long enough + //[ast]~^ ERROR `c_shortest` does not live long enough // No error: St and Sr have no destructor. st = St("st", &c_shortest); sr = Sr("sr", &c_shortest); println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0)); + use_imm(sr.1); use_imm(st.1); use_imm(pr.1); use_imm(pt.1); use_imm(dr.1); use_imm(dt.1); } + +fn use_imm(_: &T) { } From b75fbbf54095d5430ddf0f93c7210fe6f9007bc6 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 16:29:41 +0100 Subject: [PATCH 14/15] Make `ui/borrowck/borrowck-overloaded-call.rs` robust w.r.t. NLL. --- .../borrowck/borrowck-overloaded-call.nll.stderr | 14 ++++++++++++-- src/test/ui/borrowck/borrowck-overloaded-call.rs | 4 +++- .../ui/borrowck/borrowck-overloaded-call.stderr | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/test/ui/borrowck/borrowck-overloaded-call.nll.stderr b/src/test/ui/borrowck/borrowck-overloaded-call.nll.stderr index dc8d731dede74..0c4f2fa9d718b 100644 --- a/src/test/ui/borrowck/borrowck-overloaded-call.nll.stderr +++ b/src/test/ui/borrowck/borrowck-overloaded-call.nll.stderr @@ -1,3 +1,13 @@ +error[E0502]: cannot borrow `s` as immutable because it is also borrowed as mutable + --> $DIR/borrowck-overloaded-call.rs:69:5 + | +LL | let sp = &mut s; + | ------ mutable borrow occurs here +LL | s(3); //~ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable + | ^ immutable borrow occurs here +LL | use_mut(sp); + | -- mutable borrow later used here + error[E0596]: cannot borrow `s` as mutable, as it is not declared as mutable --> $DIR/borrowck-overloaded-call.rs:77:5 | @@ -17,7 +27,7 @@ LL | s(" world".to_string()); //~ ERROR use of moved value: `s` | = note: move occurs because `s` has type `SFnOnce`, which does not implement the `Copy` trait -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors -Some errors occurred: E0382, E0596. +Some errors occurred: E0382, E0502, E0596. For more information about an error, try `rustc --explain E0382`. diff --git a/src/test/ui/borrowck/borrowck-overloaded-call.rs b/src/test/ui/borrowck/borrowck-overloaded-call.rs index 41f3e472cd125..b2401fbbc042c 100644 --- a/src/test/ui/borrowck/borrowck-overloaded-call.rs +++ b/src/test/ui/borrowck/borrowck-overloaded-call.rs @@ -67,8 +67,8 @@ fn f() { }; let sp = &mut s; s(3); //~ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable + use_mut(sp); } - fn g() { let s = SFnMut { x: 1, @@ -86,3 +86,5 @@ fn h() { } fn main() {} + +fn use_mut(_: &mut T) { } diff --git a/src/test/ui/borrowck/borrowck-overloaded-call.stderr b/src/test/ui/borrowck/borrowck-overloaded-call.stderr index fa2473adc2ffd..bb5bafbbc7b85 100644 --- a/src/test/ui/borrowck/borrowck-overloaded-call.stderr +++ b/src/test/ui/borrowck/borrowck-overloaded-call.stderr @@ -5,6 +5,7 @@ LL | let sp = &mut s; | - mutable borrow occurs here LL | s(3); //~ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable | ^ immutable borrow occurs here +LL | use_mut(sp); LL | } | - mutable borrow ends here From fff9ddedcef8a869a4665414bf4f5a122eeab823 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 5 Nov 2018 16:37:18 +0100 Subject: [PATCH 15/15] For feature-gate-nll test, turn off testing modes that externally enable NLL. --- .../feature-gates/feature-gate-nll.nll.stderr | 13 ----------- src/test/ui/feature-gates/feature-gate-nll.rs | 22 +++++++++---------- 2 files changed, 11 insertions(+), 24 deletions(-) delete mode 100644 src/test/ui/feature-gates/feature-gate-nll.nll.stderr diff --git a/src/test/ui/feature-gates/feature-gate-nll.nll.stderr b/src/test/ui/feature-gates/feature-gate-nll.nll.stderr deleted file mode 100644 index 81de0d14aa7d3..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-nll.nll.stderr +++ /dev/null @@ -1,13 +0,0 @@ -error: compilation successful - --> $DIR/feature-gate-nll.rs:13:1 - | -LL | / fn main() { #![rustc_error] // rust-lang/rust#49855 -LL | | let mut x = 33; -LL | | -LL | | let p = &x; -LL | | x = 22; //~ ERROR cannot assign to `x` because it is borrowed [E0506] -LL | | } - | |_^ - -error: aborting due to previous error - diff --git a/src/test/ui/feature-gates/feature-gate-nll.rs b/src/test/ui/feature-gates/feature-gate-nll.rs index 752b1fa821f7f..14c48fb48a09b 100644 --- a/src/test/ui/feature-gates/feature-gate-nll.rs +++ b/src/test/ui/feature-gates/feature-gate-nll.rs @@ -1,16 +1,16 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. -#![feature(rustc_attrs)] +// This is a test checking that if you do not opt into NLL then you +// should not get the effects of NLL applied to the test. + +// Don't use 2018 edition, since that turns on NLL (migration mode). +// edition:2015 + +// Don't use compare-mode=nll, since that turns on NLL. +// ignore-compare-mode-nll + + #![allow(dead_code)] -fn main() { #![rustc_error] // rust-lang/rust#49855 +fn main() { let mut x = 33; let p = &x;