From c442dae5b89f175c2aa52877cb4f693b0f2b2ca6 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 11 Jun 2019 18:36:15 +0200 Subject: [PATCH 01/29] Ignore NLL migrate mode test in the Polonius compare-mode This is test specific to the NLL migrate mode which is irrelevant to -Z polonius. It can't currently be encoded depending on migrate-mode and NLL/Polonius mode, so the NLL compare-mode also ignores it. --- src/test/ui/borrowck/borrowck-migrate-to-nll.edition.stderr | 2 +- src/test/ui/borrowck/borrowck-migrate-to-nll.rs | 1 + src/test/ui/borrowck/borrowck-migrate-to-nll.zflag.stderr | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/ui/borrowck/borrowck-migrate-to-nll.edition.stderr b/src/test/ui/borrowck/borrowck-migrate-to-nll.edition.stderr index 663164cfc2c1c..d97883ad47a50 100644 --- a/src/test/ui/borrowck/borrowck-migrate-to-nll.edition.stderr +++ b/src/test/ui/borrowck/borrowck-migrate-to-nll.edition.stderr @@ -1,5 +1,5 @@ warning[E0507]: cannot move out of `foo` in pattern guard - --> $DIR/borrowck-migrate-to-nll.rs:25:18 + --> $DIR/borrowck-migrate-to-nll.rs:26:18 | LL | (|| { let bar = foo; bar.take() })(); | ^^ --- diff --git a/src/test/ui/borrowck/borrowck-migrate-to-nll.rs b/src/test/ui/borrowck/borrowck-migrate-to-nll.rs index 5b7018df91931..a64df9df25948 100644 --- a/src/test/ui/borrowck/borrowck-migrate-to-nll.rs +++ b/src/test/ui/borrowck/borrowck-migrate-to-nll.rs @@ -10,6 +10,7 @@ // just ignore it instead: // ignore-compare-mode-nll +// ignore-compare-mode-polonius // revisions: zflag edition //[zflag]compile-flags: -Z borrowck=migrate diff --git a/src/test/ui/borrowck/borrowck-migrate-to-nll.zflag.stderr b/src/test/ui/borrowck/borrowck-migrate-to-nll.zflag.stderr index 663164cfc2c1c..d97883ad47a50 100644 --- a/src/test/ui/borrowck/borrowck-migrate-to-nll.zflag.stderr +++ b/src/test/ui/borrowck/borrowck-migrate-to-nll.zflag.stderr @@ -1,5 +1,5 @@ warning[E0507]: cannot move out of `foo` in pattern guard - --> $DIR/borrowck-migrate-to-nll.rs:25:18 + --> $DIR/borrowck-migrate-to-nll.rs:26:18 | LL | (|| { let bar = foo; bar.take() })(); | ^^ --- From d4ca9a349ca67f3e23692a1ebfbce919da307738 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 11 Jun 2019 18:53:54 +0200 Subject: [PATCH 02/29] Ignore test issue-45983 in the polonius compare mode There is no difference between the NLL and Polonius outputs, and it manually tests NLLs. --- src/test/ui/borrowck/issue-45983.migrate.stderr | 2 +- src/test/ui/borrowck/issue-45983.nll.stderr | 4 ++-- src/test/ui/borrowck/issue-45983.rs | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/ui/borrowck/issue-45983.migrate.stderr b/src/test/ui/borrowck/issue-45983.migrate.stderr index 3a6b2f69a1ffc..c1564cf07e68a 100644 --- a/src/test/ui/borrowck/issue-45983.migrate.stderr +++ b/src/test/ui/borrowck/issue-45983.migrate.stderr @@ -1,5 +1,5 @@ error: borrowed data cannot be stored outside of its closure - --> $DIR/issue-45983.rs:19:27 + --> $DIR/issue-45983.rs:20:27 | LL | let x = None; | - borrowed data cannot be stored into here... diff --git a/src/test/ui/borrowck/issue-45983.nll.stderr b/src/test/ui/borrowck/issue-45983.nll.stderr index 94360b65ffe36..dff0b4cebace9 100644 --- a/src/test/ui/borrowck/issue-45983.nll.stderr +++ b/src/test/ui/borrowck/issue-45983.nll.stderr @@ -1,5 +1,5 @@ error[E0521]: borrowed data escapes outside of closure - --> $DIR/issue-45983.rs:19:18 + --> $DIR/issue-45983.rs:20:18 | LL | let x = None; | - `x` is declared here, outside of the closure body @@ -9,7 +9,7 @@ LL | give_any(|y| x = Some(y)); | `y` is a reference that is only valid in the closure body error[E0594]: cannot assign to `x`, as it is not declared as mutable - --> $DIR/issue-45983.rs:19:18 + --> $DIR/issue-45983.rs:20:18 | LL | let x = None; | - help: consider changing this to be mutable: `mut x` diff --git a/src/test/ui/borrowck/issue-45983.rs b/src/test/ui/borrowck/issue-45983.rs index a2656f5939aa1..3cd282077424b 100644 --- a/src/test/ui/borrowck/issue-45983.rs +++ b/src/test/ui/borrowck/issue-45983.rs @@ -7,6 +7,7 @@ // revisions, don't worry about the --compare-mode=nll on this test. // ignore-compare-mode-nll +// ignore-compare-mode-polonius //[nll]compile-flags: -Z borrowck=mir From 273bfd43c1c1b315ef7996ac5ef4ca2a1f1ebd2f Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 11 Jun 2019 19:02:13 +0200 Subject: [PATCH 03/29] Ignore two-phase-reservation-sharing-interference-2.rs in Polonius compare mode This is just a difference from the test construction, it's ignore-compare-mode-nll and manually checks migrate/nll over edition2015/2018. This failure is because the `migrate2015` and `migrate2018` revisions are ran with `-Zpolonius`. There is no actual difference in the errors output by NLLs and Polonius. --- ...se-reservation-sharing-interference-2.migrate2015.stderr | 6 +++--- ...se-reservation-sharing-interference-2.migrate2018.stderr | 6 +++--- ...-phase-reservation-sharing-interference-2.nll2015.stderr | 6 +++--- ...-phase-reservation-sharing-interference-2.nll2018.stderr | 6 +++--- .../two-phase-reservation-sharing-interference-2.rs | 1 + 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2015.stderr b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2015.stderr index 565e433f77361..88e9ced03ddde 100644 --- a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2015.stderr +++ b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2015.stderr @@ -1,5 +1,5 @@ error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:18:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:19:5 | LL | let shared = &v; | -- immutable borrow occurs here @@ -11,7 +11,7 @@ LL | v.extend(shared); | mutable borrow occurs here error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:28:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:29:5 | LL | v.extend(&v); | ^^------^--^ @@ -21,7 +21,7 @@ LL | v.extend(&v); | mutable borrow occurs here warning: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:39:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:40:5 | LL | let shared = &v; | -- immutable borrow occurs here diff --git a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2018.stderr b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2018.stderr index 565e433f77361..88e9ced03ddde 100644 --- a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2018.stderr +++ b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.migrate2018.stderr @@ -1,5 +1,5 @@ error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:18:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:19:5 | LL | let shared = &v; | -- immutable borrow occurs here @@ -11,7 +11,7 @@ LL | v.extend(shared); | mutable borrow occurs here error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:28:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:29:5 | LL | v.extend(&v); | ^^------^--^ @@ -21,7 +21,7 @@ LL | v.extend(&v); | mutable borrow occurs here warning: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:39:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:40:5 | LL | let shared = &v; | -- immutable borrow occurs here diff --git a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.nll2015.stderr b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.nll2015.stderr index 730741c7a9ae2..52017394e898e 100644 --- a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.nll2015.stderr +++ b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.nll2015.stderr @@ -1,5 +1,5 @@ error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:18:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:19:5 | LL | let shared = &v; | -- immutable borrow occurs here @@ -10,7 +10,7 @@ LL | v.extend(shared); | mutable borrow occurs here error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:28:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:29:5 | LL | v.extend(&v); | ^^------^--^ @@ -20,7 +20,7 @@ LL | v.extend(&v); | mutable borrow occurs here error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:39:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:40:5 | LL | let shared = &v; | -- immutable borrow occurs here diff --git a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.nll2018.stderr b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.nll2018.stderr index 730741c7a9ae2..52017394e898e 100644 --- a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.nll2018.stderr +++ b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.nll2018.stderr @@ -1,5 +1,5 @@ error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:18:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:19:5 | LL | let shared = &v; | -- immutable borrow occurs here @@ -10,7 +10,7 @@ LL | v.extend(shared); | mutable borrow occurs here error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:28:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:29:5 | LL | v.extend(&v); | ^^------^--^ @@ -20,7 +20,7 @@ LL | v.extend(&v); | mutable borrow occurs here error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable - --> $DIR/two-phase-reservation-sharing-interference-2.rs:39:5 + --> $DIR/two-phase-reservation-sharing-interference-2.rs:40:5 | LL | let shared = &v; | -- immutable borrow occurs here diff --git a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.rs b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.rs index de1af3aaa05e4..14f687c23780c 100644 --- a/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.rs +++ b/src/test/ui/borrowck/two-phase-reservation-sharing-interference-2.rs @@ -3,6 +3,7 @@ // everyone else. //ignore-compare-mode-nll +//ignore-compare-mode-polonius //revisions: migrate2015 migrate2018 nll2015 nll2018 From 63c837ef8db1e7452a6ef2faf2660d8038ed5ad5 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 11 Jun 2019 19:08:56 +0200 Subject: [PATCH 04/29] Ignore feature-gate-nll.rs in Polonius compare mode This is a test about turning the NLL feature gate on, ignored by the NLL compare-mode. --- src/test/ui/feature-gates/feature-gate-nll.rs | 1 + src/test/ui/feature-gates/feature-gate-nll.stderr | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/ui/feature-gates/feature-gate-nll.rs b/src/test/ui/feature-gates/feature-gate-nll.rs index ec5eacd162547..8ec752409ab00 100644 --- a/src/test/ui/feature-gates/feature-gate-nll.rs +++ b/src/test/ui/feature-gates/feature-gate-nll.rs @@ -3,6 +3,7 @@ // Don't use compare-mode=nll, since that turns on NLL. // ignore-compare-mode-nll +// ignore-compare-mode-polonius #![feature(rustc_attrs)] diff --git a/src/test/ui/feature-gates/feature-gate-nll.stderr b/src/test/ui/feature-gates/feature-gate-nll.stderr index 37542d52dc2ce..e5b28bbfa2477 100644 --- a/src/test/ui/feature-gates/feature-gate-nll.stderr +++ b/src/test/ui/feature-gates/feature-gate-nll.stderr @@ -1,5 +1,5 @@ warning[E0502]: cannot borrow `*x.1` as immutable because it is also borrowed as mutable - --> $DIR/feature-gate-nll.rs:14:13 + --> $DIR/feature-gate-nll.rs:15:13 | LL | let m = &mut x; | ------ mutable borrow occurs here @@ -14,7 +14,7 @@ LL | m; = note: for more information, try `rustc --explain E0729` error: compilation successful - --> $DIR/feature-gate-nll.rs:10:1 + --> $DIR/feature-gate-nll.rs:11:1 | LL | / fn main() { LL | | let mut x = (33, &0); From 51c15fae61cdf3af53a127b0c062fdacd596ebea Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 11 Jun 2019 19:12:45 +0200 Subject: [PATCH 05/29] Ignore test issue-45696-scribble-on-boxed-borrow.rs in Polonius compare mode Once again, the difference is in the test construction, it is ignored in compare-mode NLL and tested manually with revisions, and fails because the `migrate` revision is ran with `-Zpolonius`. There is no actual difference in the errors output by NLLs and Polonius. --- .../issue-45696-scribble-on-boxed-borrow.migrate.stderr | 8 ++++---- .../issue-45696-scribble-on-boxed-borrow.nll.stderr | 6 +++--- .../ui/issues/issue-45696-scribble-on-boxed-borrow.rs | 1 + 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.migrate.stderr b/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.migrate.stderr index 479b724ad18f1..2e99572d01828 100644 --- a/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.migrate.stderr +++ b/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.migrate.stderr @@ -1,5 +1,5 @@ warning[E0713]: borrow may still be in use when destructor runs - --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:51:5 + --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:52:5 | LL | fn scribbled<'a>(s: Scribble<'a>) -> &'a mut u32 { | -- lifetime `'a` defined here @@ -14,7 +14,7 @@ LL | } = note: for more information, try `rustc --explain E0729` warning[E0713]: borrow may still be in use when destructor runs - --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:62:5 + --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:63:5 | LL | fn boxed_scribbled<'a>(s: Box>) -> &'a mut u32 { | -- lifetime `'a` defined here @@ -29,7 +29,7 @@ LL | } = note: for more information, try `rustc --explain E0729` warning[E0713]: borrow may still be in use when destructor runs - --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:73:5 + --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:74:5 | LL | fn boxed_boxed_scribbled<'a>(s: Box>>) -> &'a mut u32 { | -- lifetime `'a` defined here @@ -44,7 +44,7 @@ LL | } = note: for more information, try `rustc --explain E0729` error: compilation successful - --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:80:1 + --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:81:1 | LL | / fn main() { LL | | let mut x = 1; diff --git a/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.nll.stderr b/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.nll.stderr index 1b9fb0499260c..45b22511d27d6 100644 --- a/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.nll.stderr +++ b/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.nll.stderr @@ -1,5 +1,5 @@ error[E0713]: borrow may still be in use when destructor runs - --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:51:5 + --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:52:5 | LL | fn scribbled<'a>(s: Scribble<'a>) -> &'a mut u32 { | -- lifetime `'a` defined here @@ -10,7 +10,7 @@ LL | } | - here, drop of `s` needs exclusive access to `*s.0`, because the type `Scribble<'_>` implements the `Drop` trait error[E0713]: borrow may still be in use when destructor runs - --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:62:5 + --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:63:5 | LL | fn boxed_scribbled<'a>(s: Box>) -> &'a mut u32 { | -- lifetime `'a` defined here @@ -21,7 +21,7 @@ LL | } | - here, drop of `s` needs exclusive access to `*s.0`, because the type `Scribble<'_>` implements the `Drop` trait error[E0713]: borrow may still be in use when destructor runs - --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:73:5 + --> $DIR/issue-45696-scribble-on-boxed-borrow.rs:74:5 | LL | fn boxed_boxed_scribbled<'a>(s: Box>>) -> &'a mut u32 { | -- lifetime `'a` defined here diff --git a/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.rs b/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.rs index f568efa487cd7..9f261884f3d2d 100644 --- a/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.rs +++ b/src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.rs @@ -7,6 +7,7 @@ // revisions: nll migrate // ignore-compare-mode-nll +// ignore-compare-mode-polonius // This test is going to pass in the migrate revision, because the AST-borrowck // accepted this code in the past (see notes below). So we use `#[rustc_error]` From ff350f8efa1882c34a2d4e2d48b1fed62b2fae31 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 11 Jun 2019 19:21:24 +0200 Subject: [PATCH 06/29] Bless output of test ui/emit-artifact-notifications.rs for Polonius --- src/test/ui/emit-artifact-notifications.polonius.stderr | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/test/ui/emit-artifact-notifications.polonius.stderr diff --git a/src/test/ui/emit-artifact-notifications.polonius.stderr b/src/test/ui/emit-artifact-notifications.polonius.stderr new file mode 100644 index 0000000000000..47b48b399c25c --- /dev/null +++ b/src/test/ui/emit-artifact-notifications.polonius.stderr @@ -0,0 +1 @@ +{"artifact":"$TEST_BUILD_DIR/emit-artifact-notifications.polonius/libemit_artifact_notifications.rmeta","emit":"metadata"} From 2824db1388dbd300c80466b10dc736d2780b8000 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 11 Jun 2019 19:24:50 +0200 Subject: [PATCH 07/29] Bless output of test save-analysis/emit-notifications.rs for Polonius --- src/test/ui/save-analysis/emit-notifications.polonius.stderr | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/test/ui/save-analysis/emit-notifications.polonius.stderr diff --git a/src/test/ui/save-analysis/emit-notifications.polonius.stderr b/src/test/ui/save-analysis/emit-notifications.polonius.stderr new file mode 100644 index 0000000000000..a1a1b8c63dac3 --- /dev/null +++ b/src/test/ui/save-analysis/emit-notifications.polonius.stderr @@ -0,0 +1,2 @@ +{"artifact":"$TEST_BUILD_DIR/save-analysis/emit-notifications.polonius/save-analysis/libemit_notifications.json","emit":"save-analysis"} +{"artifact":"$TEST_BUILD_DIR/save-analysis/emit-notifications.polonius/libemit_notifications.rlib","emit":"link"} From 94101046ffe4e6a01819631d00d3802a838af98a Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 11 Jun 2019 19:33:36 +0200 Subject: [PATCH 08/29] Bless output of test nll/loan_ends_mid_block_pair.rs for Polonius --- .../loan_ends_mid_block_pair.polonius.stderr | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/ui/nll/loan_ends_mid_block_pair.polonius.stderr diff --git a/src/test/ui/nll/loan_ends_mid_block_pair.polonius.stderr b/src/test/ui/nll/loan_ends_mid_block_pair.polonius.stderr new file mode 100644 index 0000000000000..c9537d42c41af --- /dev/null +++ b/src/test/ui/nll/loan_ends_mid_block_pair.polonius.stderr @@ -0,0 +1,38 @@ +error[E0506]: cannot assign to `data.0` because it is borrowed + --> $DIR/loan_ends_mid_block_pair.rs:12:5 + | +LL | let c = &mut data.0; + | ----------- borrow of `data.0` occurs here +LL | capitalize(c); +LL | data.0 = 'e'; + | ^^^^^^^^^^^^ assignment to borrowed `data.0` occurs here +... +LL | capitalize(c); + | - borrow later used here + +error[E0506]: cannot assign to `data.0` because it is borrowed + --> $DIR/loan_ends_mid_block_pair.rs:14:5 + | +LL | let c = &mut data.0; + | ----------- borrow of `data.0` occurs here +... +LL | data.0 = 'f'; + | ^^^^^^^^^^^^ assignment to borrowed `data.0` occurs here +LL | data.0 = 'g'; +LL | capitalize(c); + | - borrow later used here + +error[E0506]: cannot assign to `data.0` because it is borrowed + --> $DIR/loan_ends_mid_block_pair.rs:15:5 + | +LL | let c = &mut data.0; + | ----------- borrow of `data.0` occurs here +... +LL | data.0 = 'g'; + | ^^^^^^^^^^^^ assignment to borrowed `data.0` occurs here +LL | capitalize(c); + | - borrow later used here + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0506`. From 6a7c15ed25c17dfae87cc8561e7171ab749377e3 Mon Sep 17 00:00:00 2001 From: lqd Date: Wed, 12 Jun 2019 12:55:18 +0200 Subject: [PATCH 09/29] Bless output of test consts/promote_const_let.rs for Polonius --- .../consts/promote_const_let.polonius.stderr | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/test/ui/consts/promote_const_let.polonius.stderr diff --git a/src/test/ui/consts/promote_const_let.polonius.stderr b/src/test/ui/consts/promote_const_let.polonius.stderr new file mode 100644 index 0000000000000..cf41bd7bdb1eb --- /dev/null +++ b/src/test/ui/consts/promote_const_let.polonius.stderr @@ -0,0 +1,29 @@ +error[E0597]: `y` does not live long enough + --> $DIR/promote_const_let.rs:4:9 + | +LL | let x: &'static u32 = { + | - borrow later stored here +LL | let y = 42; +LL | &y + | ^^ borrowed value does not live long enough +LL | }; + | - `y` dropped here while still borrowed + +error[E0716]: temporary value dropped while borrowed + --> $DIR/promote_const_let.rs:6:28 + | +LL | let x: &'static u32 = &{ + | ____________------------____^ + | | | + | | type annotation requires that borrow lasts for `'static` +LL | | let y = 42; +LL | | y +LL | | }; + | |_____^ creates a temporary which is freed while still in use +LL | } + | - temporary value is freed at the end of this statement + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0597, E0716. +For more information about an error, try `rustc --explain E0597`. From c5a1bc1e5c45c507b247e92d33a35190257c6726 Mon Sep 17 00:00:00 2001 From: lqd Date: Wed, 12 Jun 2019 13:01:26 +0200 Subject: [PATCH 10/29] Bless output of test nll/get_default.rs for Polonius 2 of the 3 errors are "fixed by Polonius" :tada: --- src/test/ui/nll/get_default.polonius.stderr | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/test/ui/nll/get_default.polonius.stderr diff --git a/src/test/ui/nll/get_default.polonius.stderr b/src/test/ui/nll/get_default.polonius.stderr new file mode 100644 index 0000000000000..2df6d5d61fc46 --- /dev/null +++ b/src/test/ui/nll/get_default.polonius.stderr @@ -0,0 +1,15 @@ +error[E0502]: cannot borrow `*map` as mutable because it is also borrowed as immutable + --> $DIR/get_default.rs:32:17 + | +LL | match map.get() { + | --- immutable borrow occurs here +LL | Some(v) => { +LL | map.set(String::new()); // Both AST and MIR error here + | ^^^ mutable borrow occurs here +LL | +LL | return v; + | - immutable borrow later used here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0502`. From 292d5c18c93df1a502a03f228e36c4c1da3d3f6b Mon Sep 17 00:00:00 2001 From: lqd Date: Wed, 12 Jun 2019 19:27:59 +0200 Subject: [PATCH 11/29] Bless output of test generator/ref-escapes-but-not-over-yield.rs for Polonius --- ...escapes-but-not-over-yield.polonius.stderr | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/test/ui/generator/ref-escapes-but-not-over-yield.polonius.stderr diff --git a/src/test/ui/generator/ref-escapes-but-not-over-yield.polonius.stderr b/src/test/ui/generator/ref-escapes-but-not-over-yield.polonius.stderr new file mode 100644 index 0000000000000..530bf368f676e --- /dev/null +++ b/src/test/ui/generator/ref-escapes-but-not-over-yield.polonius.stderr @@ -0,0 +1,20 @@ +error[E0597]: `b` does not live long enough + --> $DIR/ref-escapes-but-not-over-yield.rs:11:13 + | +LL | let mut b = move || { + | _________________- +LL | | yield(); +LL | | let b = 5; +LL | | a = &b; + | | ^^ borrowed value does not live long enough +LL | | +LL | | }; + | | - + | | | + | | `b` dropped here while still borrowed + | |_____... and the borrow might be used here, when that temporary is dropped and runs the destructor for generator + | a temporary with access to the borrow is created here ... + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0597`. From 6fe52928a3dd9a69ebd6422a23aba879871026c4 Mon Sep 17 00:00:00 2001 From: lqd Date: Fri, 28 Jun 2019 13:37:08 +0200 Subject: [PATCH 12/29] Bless output of test borrowck/borrowck-escaping-closure-error-2.rs for Polonius --- ...owck-escaping-closure-error-2.polonius.stderr | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/ui/borrowck/borrowck-escaping-closure-error-2.polonius.stderr diff --git a/src/test/ui/borrowck/borrowck-escaping-closure-error-2.polonius.stderr b/src/test/ui/borrowck/borrowck-escaping-closure-error-2.polonius.stderr new file mode 100644 index 0000000000000..89af8764557ff --- /dev/null +++ b/src/test/ui/borrowck/borrowck-escaping-closure-error-2.polonius.stderr @@ -0,0 +1,16 @@ +error[E0597]: `books` does not live long enough + --> $DIR/borrowck-escaping-closure-error-2.rs:11:17 + | +LL | Box::new(|| books.push(4)) + | ------------^^^^^--------- + | | | | + | | | borrowed value does not live long enough + | | value captured here + | borrow later used here +LL | +LL | } + | - `books` dropped here while still borrowed + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0597`. From 7db61e78c01b93cceb28cf87e69d5d6c0ca0b482 Mon Sep 17 00:00:00 2001 From: lqd Date: Fri, 28 Jun 2019 14:21:48 +0200 Subject: [PATCH 13/29] Bless output of test borrowck/two-phase-surprise-no-conflict.rs for Polonius --- ...phase-surprise-no-conflict.polonius.stderr | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 src/test/ui/borrowck/two-phase-surprise-no-conflict.polonius.stderr diff --git a/src/test/ui/borrowck/two-phase-surprise-no-conflict.polonius.stderr b/src/test/ui/borrowck/two-phase-surprise-no-conflict.polonius.stderr new file mode 100644 index 0000000000000..7b246426a2333 --- /dev/null +++ b/src/test/ui/borrowck/two-phase-surprise-no-conflict.polonius.stderr @@ -0,0 +1,148 @@ +error[E0503]: cannot use `self.cx` because it was mutably borrowed + --> $DIR/two-phase-surprise-no-conflict.rs:21:23 + | +LL | let _mut_borrow = &mut *self; + | ---------- borrow of `*self` occurs here +LL | let _access = self.cx; + | ^^^^^^^ use of borrowed `*self` +LL | +LL | _mut_borrow; + | ----------- borrow later used here + +error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable + --> $DIR/two-phase-surprise-no-conflict.rs:57:17 + | +LL | self.hash_expr(&self.cx_mut.body(eid).value); + | ^^^^^---------^^-----------^^^^^^^^^^^^^^^^^ + | | | | + | | | immutable borrow occurs here + | | immutable borrow later used by call + | mutable borrow occurs here + +error[E0499]: cannot borrow `reg.sess_mut` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:119:51 + | +LL | reg.register_static(Box::new(TrivialPass::new(&mut reg.sess_mut))); + | --- --------------- ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here + | | | + | | first borrow later used by call + | first mutable borrow occurs here + +error[E0499]: cannot borrow `reg.sess_mut` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:122:54 + | +LL | reg.register_bound(Box::new(TrivialPass::new_mut(&mut reg.sess_mut))); + | --- -------------- ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here + | | | + | | first borrow later used by call + | first mutable borrow occurs here + +error[E0499]: cannot borrow `reg.sess_mut` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:125:53 + | +LL | reg.register_univ(Box::new(TrivialPass::new_mut(&mut reg.sess_mut))); + | --- ------------- ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here + | | | + | | first borrow later used by call + | first mutable borrow occurs here + +error[E0499]: cannot borrow `reg.sess_mut` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:128:44 + | +LL | reg.register_ref(&TrivialPass::new_mut(&mut reg.sess_mut)); + | --- ------------ ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here + | | | + | | first borrow later used by call + | first mutable borrow occurs here + +error[E0502]: cannot borrow `*reg` as mutable because it is also borrowed as immutable + --> $DIR/two-phase-surprise-no-conflict.rs:138:5 + | +LL | reg.register_bound(Box::new(CapturePass::new(®.sess_mut))); + | ^^^^--------------^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------^^^ + | | | | + | | | immutable borrow occurs here + | | immutable borrow later used by call + | mutable borrow occurs here + +error[E0502]: cannot borrow `*reg` as mutable because it is also borrowed as immutable + --> $DIR/two-phase-surprise-no-conflict.rs:141:5 + | +LL | reg.register_univ(Box::new(CapturePass::new(®.sess_mut))); + | ^^^^-------------^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------^^^ + | | | | + | | | immutable borrow occurs here + | | immutable borrow later used by call + | mutable borrow occurs here + +error[E0502]: cannot borrow `*reg` as mutable because it is also borrowed as immutable + --> $DIR/two-phase-surprise-no-conflict.rs:144:5 + | +LL | reg.register_ref(&CapturePass::new(®.sess_mut)); + | ^^^^------------^^^^^^^^^^^^^^^^^^^-------------^^ + | | | | + | | | immutable borrow occurs here + | | immutable borrow later used by call + | mutable borrow occurs here + +error[E0499]: cannot borrow `*reg` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:154:5 + | +LL | reg.register_bound(Box::new(CapturePass::new_mut(&mut reg.sess_mut))); + | ^^^^--------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------^^^ + | | | | + | | | first mutable borrow occurs here + | | first borrow later used by call + | second mutable borrow occurs here + +error[E0499]: cannot borrow `reg.sess_mut` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:154:54 + | +LL | reg.register_bound(Box::new(CapturePass::new_mut(&mut reg.sess_mut))); + | --- -------------- ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here + | | | + | | first borrow later used by call + | first mutable borrow occurs here + +error[E0499]: cannot borrow `*reg` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:158:5 + | +LL | reg.register_univ(Box::new(CapturePass::new_mut(&mut reg.sess_mut))); + | ^^^^-------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------^^^ + | | | | + | | | first mutable borrow occurs here + | | first borrow later used by call + | second mutable borrow occurs here + +error[E0499]: cannot borrow `reg.sess_mut` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:158:53 + | +LL | reg.register_univ(Box::new(CapturePass::new_mut(&mut reg.sess_mut))); + | --- ------------- ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here + | | | + | | first borrow later used by call + | first mutable borrow occurs here + +error[E0499]: cannot borrow `*reg` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:162:5 + | +LL | reg.register_ref(&CapturePass::new_mut(&mut reg.sess_mut)); + | ^^^^------------^^^^^^^^^^^^^^^^^^^^^^^-----------------^^ + | | | | + | | | first mutable borrow occurs here + | | first borrow later used by call + | second mutable borrow occurs here + +error[E0499]: cannot borrow `reg.sess_mut` as mutable more than once at a time + --> $DIR/two-phase-surprise-no-conflict.rs:162:44 + | +LL | reg.register_ref(&CapturePass::new_mut(&mut reg.sess_mut)); + | --- ------------ ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here + | | | + | | first borrow later used by call + | first mutable borrow occurs here + +error: aborting due to 15 previous errors + +Some errors have detailed explanations: E0499, E0502, E0503. +For more information about an error, try `rustc --explain E0499`. From 08c25b5122574e1dca21c673cb6d2e7ceee489b1 Mon Sep 17 00:00:00 2001 From: lqd Date: Fri, 28 Jun 2019 14:26:05 +0200 Subject: [PATCH 14/29] Bless output of test dropck/dropck_trait_cycle_checked.rs for Polonius --- ...dropck_trait_cycle_checked.polonius.stderr | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/ui/dropck/dropck_trait_cycle_checked.polonius.stderr diff --git a/src/test/ui/dropck/dropck_trait_cycle_checked.polonius.stderr b/src/test/ui/dropck/dropck_trait_cycle_checked.polonius.stderr new file mode 100644 index 0000000000000..dbcb0fcebb73d --- /dev/null +++ b/src/test/ui/dropck/dropck_trait_cycle_checked.polonius.stderr @@ -0,0 +1,74 @@ +error[E0597]: `o2` does not live long enough + --> $DIR/dropck_trait_cycle_checked.rs:111:13 + | +LL | o1.set0(&o2); + | ^^^ borrowed value does not live long enough +... +LL | } + | - + | | + | `o2` dropped here while still borrowed + | borrow might be used here, when `o2` is dropped and runs the destructor for type `std::boxed::Box>` + +error[E0597]: `o3` does not live long enough + --> $DIR/dropck_trait_cycle_checked.rs:112:13 + | +LL | o1.set1(&o3); + | ^^^ borrowed value does not live long enough +... +LL | } + | - + | | + | `o3` dropped here while still borrowed + | borrow might be used here, when `o3` is dropped and runs the destructor for type `std::boxed::Box>` + +error[E0597]: `o2` does not live long enough + --> $DIR/dropck_trait_cycle_checked.rs:113:13 + | +LL | let (o1, o2, o3): (Box, Box, Box) = (O::new(), O::new(), O::new()); + | -------- cast requires that `o2` is borrowed for `'static` +... +LL | o2.set0(&o2); + | ^^^ borrowed value does not live long enough +... +LL | } + | - `o2` dropped here while still borrowed + +error[E0597]: `o3` does not live long enough + --> $DIR/dropck_trait_cycle_checked.rs:114:13 + | +LL | let (o1, o2, o3): (Box, Box, Box) = (O::new(), O::new(), O::new()); + | -------- cast requires that `o3` is borrowed for `'static` +... +LL | o2.set1(&o3); + | ^^^ borrowed value does not live long enough +... +LL | } + | - `o3` dropped here while still borrowed + +error[E0597]: `o1` does not live long enough + --> $DIR/dropck_trait_cycle_checked.rs:115:13 + | +LL | o3.set0(&o1); + | ^^^ borrowed value does not live long enough +LL | o3.set1(&o2); +LL | } + | - + | | + | `o1` dropped here while still borrowed + | borrow might be used here, when `o1` is dropped and runs the destructor for type `std::boxed::Box>` + +error[E0597]: `o2` does not live long enough + --> $DIR/dropck_trait_cycle_checked.rs:116:13 + | +LL | let (o1, o2, o3): (Box, Box, Box) = (O::new(), O::new(), O::new()); + | -------- cast requires that `o2` is borrowed for `'static` +... +LL | o3.set1(&o2); + | ^^^ borrowed value does not live long enough +LL | } + | - `o2` dropped here while still borrowed + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0597`. From 9a82f52e59c39e3a101a51cc6902ab0064c61c6a Mon Sep 17 00:00:00 2001 From: lqd Date: Fri, 5 Jul 2019 19:18:36 +0200 Subject: [PATCH 15/29] Create a dedicated polonius test folder --- src/test/ui/nll/{ => polonius}/polonius-smoke-test.rs | 0 src/test/ui/nll/{ => polonius}/polonius-smoke-test.stderr | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/test/ui/nll/{ => polonius}/polonius-smoke-test.rs (100%) rename src/test/ui/nll/{ => polonius}/polonius-smoke-test.stderr (100%) diff --git a/src/test/ui/nll/polonius-smoke-test.rs b/src/test/ui/nll/polonius/polonius-smoke-test.rs similarity index 100% rename from src/test/ui/nll/polonius-smoke-test.rs rename to src/test/ui/nll/polonius/polonius-smoke-test.rs diff --git a/src/test/ui/nll/polonius-smoke-test.stderr b/src/test/ui/nll/polonius/polonius-smoke-test.stderr similarity index 100% rename from src/test/ui/nll/polonius-smoke-test.stderr rename to src/test/ui/nll/polonius/polonius-smoke-test.stderr From 6d9a4f978313798a275a150ffd0f103609484ebd Mon Sep 17 00:00:00 2001 From: lqd Date: Mon, 15 Jul 2019 15:54:43 +0200 Subject: [PATCH 16/29] Polonius facts: kill loans on Call terminators and StorageDead --- .../borrow_check/nll/constraint_generation.rs | 74 +++++++++++++++---- src/test/ui/nll/polonius/calls-kill-loans.rs | 24 ++++++ 2 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 src/test/ui/nll/polonius/calls-kill-loans.rs diff --git a/src/librustc_mir/borrow_check/nll/constraint_generation.rs b/src/librustc_mir/borrow_check/nll/constraint_generation.rs index 055568f0a27a2..25d0eaafc0049 100644 --- a/src/librustc_mir/borrow_check/nll/constraint_generation.rs +++ b/src/librustc_mir/borrow_check/nll/constraint_generation.rs @@ -6,8 +6,8 @@ use crate::borrow_check::nll::region_infer::values::LivenessValues; use rustc::infer::InferCtxt; use rustc::mir::visit::TyContext; use rustc::mir::visit::Visitor; -use rustc::mir::{BasicBlock, BasicBlockData, Location, Body, Place, PlaceBase, Rvalue}; -use rustc::mir::{SourceInfo, Statement, Terminator}; +use rustc::mir::{BasicBlock, BasicBlockData, Location, Body, Place, PlaceBase, Rvalue, TerminatorKind}; +use rustc::mir::{Local, SourceInfo, Statement, StatementKind, Terminator}; use rustc::mir::UserTypeProjection; use rustc::ty::fold::TypeFoldable; use rustc::ty::{self, ClosureSubsts, GeneratorSubsts, RegionVid, Ty}; @@ -114,6 +114,17 @@ impl<'cg, 'cx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'tcx> { self.location_table .start_index(location.successor_within_block()), )); + + // If there are borrows on this now dead local, we need to record them as `killed`. + if let StatementKind::StorageDead(ref local) = statement.kind { + record_killed_borrows_for_local( + all_facts, + self.borrow_set, + self.location_table, + local, + location, + ); + } } self.super_statement(statement, location); @@ -127,20 +138,7 @@ impl<'cg, 'cx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'tcx> { ) { // When we see `X = ...`, then kill borrows of // `(*X).foo` and so forth. - if let Some(all_facts) = self.all_facts { - if let Place { - base: PlaceBase::Local(temp), - projection: None, - } = place { - if let Some(borrow_indices) = self.borrow_set.local_map.get(temp) { - all_facts.killed.reserve(borrow_indices.len()); - for &borrow_index in borrow_indices { - let location_index = self.location_table.mid_index(location); - all_facts.killed.push((borrow_index, location_index)); - } - } - } - } + self.record_killed_borrows_for_place(place, location); self.super_assign(place, rvalue, location); } @@ -167,6 +165,14 @@ impl<'cg, 'cx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'tcx> { } } + // A `Call` terminator's return value can be a local which has borrows, + // so we need to record those as `killed` as well. + if let TerminatorKind::Call { ref destination, .. } = terminator.kind { + if let Some((place, _)) = destination { + self.record_killed_borrows_for_place(place, location); + } + } + self.super_terminator(terminator, location); } @@ -201,4 +207,40 @@ impl<'cx, 'cg, 'tcx> ConstraintGeneration<'cx, 'cg, 'tcx> { self.liveness_constraints.add_element(vid, location); }); } + + /// When recording facts for Polonius, records the borrows on the specified place + /// as `killed`. For example, when assigning to a local, or on a call's return destination. + fn record_killed_borrows_for_place(&mut self, place: &Place<'tcx>, location: Location) { + if let Some(all_facts) = self.all_facts { + if let Place { + base: PlaceBase::Local(local), + projection: None, + } = place { + record_killed_borrows_for_local( + all_facts, + self.borrow_set, + self.location_table, + local, + location, + ); + } + } + } +} + +/// When recording facts for Polonius, records the borrows on the specified local as `killed`. +fn record_killed_borrows_for_local( + all_facts: &mut AllFacts, + borrow_set: &BorrowSet<'_>, + location_table: &LocationTable, + local: &Local, + location: Location, +) { + if let Some(borrow_indices) = borrow_set.local_map.get(local) { + all_facts.killed.reserve(borrow_indices.len()); + for &borrow_index in borrow_indices { + let location_index = location_table.mid_index(location); + all_facts.killed.push((borrow_index, location_index)); + } + } } diff --git a/src/test/ui/nll/polonius/calls-kill-loans.rs b/src/test/ui/nll/polonius/calls-kill-loans.rs new file mode 100644 index 0000000000000..3121571478893 --- /dev/null +++ b/src/test/ui/nll/polonius/calls-kill-loans.rs @@ -0,0 +1,24 @@ +// `Call` terminators can write to a local which has existing loans +// and those need to be killed like a regular assignment to a local. +// This is a simplified version of issue 47680, is correctly accepted +// by NLL but was incorrectly rejected by Polonius because of these +// missing `killed` facts. + +// build-pass +// compile-flags: -Z borrowck=mir -Z polonius +// ignore-compare-mode-nll + +struct Thing; + +impl Thing { + fn next(&mut self) -> &mut Self { unimplemented!() } +} + +fn main() { + let mut temp = &mut Thing; + + loop { + let v = temp.next(); + temp = v; // accepted by NLL, was incorrectly rejected by Polonius + } +} From 0bd2b3235808e50f1568d89bb85e2ec6f8397e49 Mon Sep 17 00:00:00 2001 From: lqd Date: Mon, 15 Jul 2019 18:04:41 +0200 Subject: [PATCH 17/29] Bless output of test borrowck/promote-ref-mut-in-let-issue-46557.rs for Polonius --- ...ref-mut-in-let-issue-46557.polonius.stderr | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/test/ui/borrowck/promote-ref-mut-in-let-issue-46557.polonius.stderr diff --git a/src/test/ui/borrowck/promote-ref-mut-in-let-issue-46557.polonius.stderr b/src/test/ui/borrowck/promote-ref-mut-in-let-issue-46557.polonius.stderr new file mode 100644 index 0000000000000..a5b2e8762746c --- /dev/null +++ b/src/test/ui/borrowck/promote-ref-mut-in-let-issue-46557.polonius.stderr @@ -0,0 +1,59 @@ +error[E0716]: temporary value dropped while borrowed + --> $DIR/promote-ref-mut-in-let-issue-46557.rs:5:21 + | +LL | let ref mut x = 1234543; + | ^^^^^^^ creates a temporary which is freed while still in use +LL | x + | - borrow later used here +LL | } + | - temporary value is freed at the end of this statement + | + = note: consider using a `let` binding to create a longer lived value + +error[E0716]: temporary value dropped while borrowed + --> $DIR/promote-ref-mut-in-let-issue-46557.rs:10:25 + | +LL | let (ref mut x, ) = (1234543, ); + | ^^^^^^^^^^^ creates a temporary which is freed while still in use +LL | x + | - borrow later used here +LL | } + | - temporary value is freed at the end of this statement + | + = note: consider using a `let` binding to create a longer lived value + +error[E0515]: cannot return value referencing temporary value + --> $DIR/promote-ref-mut-in-let-issue-46557.rs:15:5 + | +LL | match 1234543 { + | ^ ------- temporary value created here + | _____| + | | +LL | | ref mut x => x +LL | | } + | |_____^ returns a value referencing data owned by the current function + +error[E0515]: cannot return value referencing temporary value + --> $DIR/promote-ref-mut-in-let-issue-46557.rs:21:5 + | +LL | match (123443,) { + | ^ --------- temporary value created here + | _____| + | | +LL | | (ref mut x,) => x, +LL | | } + | |_____^ returns a value referencing data owned by the current function + +error[E0515]: cannot return reference to temporary value + --> $DIR/promote-ref-mut-in-let-issue-46557.rs:27:5 + | +LL | &mut 1234543 + | ^^^^^------- + | | | + | | temporary value created here + | returns a reference to data owned by the current function + +error: aborting due to 5 previous errors + +Some errors have detailed explanations: E0515, E0716. +For more information about an error, try `rustc --explain E0515`. From ed1625f29eabb0659cd9799623922c86a2e4415b Mon Sep 17 00:00:00 2001 From: lqd Date: Mon, 15 Jul 2019 18:22:02 +0200 Subject: [PATCH 18/29] Ignore test hrtb/issue-30786.rs in Polonius compare mode --- src/test/ui/hrtb/issue-30786.migrate.stderr | 6 +++--- src/test/ui/hrtb/issue-30786.nll.stderr | 4 ++-- src/test/ui/hrtb/issue-30786.rs | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/ui/hrtb/issue-30786.migrate.stderr b/src/test/ui/hrtb/issue-30786.migrate.stderr index 9a4f877282245..e7deca7644b0e 100644 --- a/src/test/ui/hrtb/issue-30786.migrate.stderr +++ b/src/test/ui/hrtb/issue-30786.migrate.stderr @@ -1,11 +1,11 @@ error: implementation of `Stream` is not general enough - --> $DIR/issue-30786.rs:107:22 + --> $DIR/issue-30786.rs:108:22 | LL | let map = source.map(|x: &_| x); | ^^^ | - = note: `Stream` would have to be implemented for the type `&'0 mut Map`, for any lifetime `'0` - = note: but `Stream` is actually implemented for the type `&'1 mut Map`, for some specific lifetime `'1` + = note: `Stream` would have to be implemented for the type `&'0 mut Map`, for any lifetime `'0` + = note: but `Stream` is actually implemented for the type `&'1 mut Map`, for some specific lifetime `'1` error: aborting due to previous error diff --git a/src/test/ui/hrtb/issue-30786.nll.stderr b/src/test/ui/hrtb/issue-30786.nll.stderr index 5c865d76851d3..8614d86d93ac3 100644 --- a/src/test/ui/hrtb/issue-30786.nll.stderr +++ b/src/test/ui/hrtb/issue-30786.nll.stderr @@ -1,11 +1,11 @@ error: higher-ranked subtype error - --> $DIR/issue-30786.rs:111:18 + --> $DIR/issue-30786.rs:112:18 | LL | let filter = map.filter(|x: &_| true); | ^^^^^^^^^^^^^^^^^^^^^^^^ error: higher-ranked subtype error - --> $DIR/issue-30786.rs:113:17 + --> $DIR/issue-30786.rs:114:17 | LL | let count = filter.count(); // Assert that we still have a valid stream. | ^^^^^^^^^^^^^^ diff --git a/src/test/ui/hrtb/issue-30786.rs b/src/test/ui/hrtb/issue-30786.rs index 321b83c3459d0..b9920a1950498 100644 --- a/src/test/ui/hrtb/issue-30786.rs +++ b/src/test/ui/hrtb/issue-30786.rs @@ -12,6 +12,7 @@ // revisions, don't worry about the --compare-mode=nll on this test. // ignore-compare-mode-nll +// ignore-compare-mode-polonius //[nll]compile-flags: -Z borrowck=mir From 40e6b025b27892877935c96315920b382083fcce Mon Sep 17 00:00:00 2001 From: lqd Date: Mon, 15 Jul 2019 18:26:51 +0200 Subject: [PATCH 19/29] Bless output of test nll/return-ref-mut-issue-46557.rs for Polonius --- .../return-ref-mut-issue-46557.polonius.stderr | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/test/ui/nll/return-ref-mut-issue-46557.polonius.stderr diff --git a/src/test/ui/nll/return-ref-mut-issue-46557.polonius.stderr b/src/test/ui/nll/return-ref-mut-issue-46557.polonius.stderr new file mode 100644 index 0000000000000..8e3cf59cffb44 --- /dev/null +++ b/src/test/ui/nll/return-ref-mut-issue-46557.polonius.stderr @@ -0,0 +1,15 @@ +error[E0716]: temporary value dropped while borrowed + --> $DIR/return-ref-mut-issue-46557.rs:4:21 + | +LL | let ref mut x = 1234543; + | ^^^^^^^ creates a temporary which is freed while still in use +LL | x + | - borrow later used here +LL | } + | - temporary value is freed at the end of this statement + | + = note: consider using a `let` binding to create a longer lived value + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0716`. From 9bd9b0d9bf616e8a8e7f6b4c3580041b007dd868 Mon Sep 17 00:00:00 2001 From: lqd Date: Mon, 15 Jul 2019 18:50:50 +0200 Subject: [PATCH 20/29] Add test extracted from rand, checking that StorageDead kills loans Like "call-kills-loans", Polonius didn't know about some `killed` facts. --- .../nll/polonius/storagedead-kills-loans.rs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/test/ui/nll/polonius/storagedead-kills-loans.rs diff --git a/src/test/ui/nll/polonius/storagedead-kills-loans.rs b/src/test/ui/nll/polonius/storagedead-kills-loans.rs new file mode 100644 index 0000000000000..3c121d3cd1882 --- /dev/null +++ b/src/test/ui/nll/polonius/storagedead-kills-loans.rs @@ -0,0 +1,28 @@ +// Whenever a `StorageDead` MIR statement destroys a value `x`, +// we should kill all loans of `x`. This is extracted from `rand 0.4.6`, +// is correctly accepted by NLL but was incorrectly rejected by +// Polonius because of these missing `killed` facts. + +// build-pass +// compile-flags: -Z borrowck=mir -Z polonius +// ignore-compare-mode-nll + +use std::{io, mem}; +use std::io::Read; + +fn fill(r: &mut Read, mut buf: &mut [u8]) -> io::Result<()> { + while buf.len() > 0 { + match r.read(buf).unwrap() { + 0 => return Err(io::Error::new(io::ErrorKind::Other, + "end of file reached")), + n => buf = &mut mem::replace(&mut buf, &mut [])[n..], + // ^- Polonius had multiple errors on the previous line (where NLL has none) + // as it didn't know `buf` was killed here, and would + // incorrectly reject both the borrow expression, and the assignment. + } + } + Ok(()) +} + +fn main() { +} From 606f7984e7fd0c33b6c7bd040dfd057569f1c19b Mon Sep 17 00:00:00 2001 From: lqd Date: Mon, 15 Jul 2019 18:51:55 +0200 Subject: [PATCH 21/29] Rename test so that both "kills-loans" tests match names --- .../nll/polonius/{calls-kill-loans.rs => call-kills-loans.rs} | 0 src/test/ui/nll/polonius/storagedead-kills-loans.rs | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) rename src/test/ui/nll/polonius/{calls-kill-loans.rs => call-kills-loans.rs} (100%) diff --git a/src/test/ui/nll/polonius/calls-kill-loans.rs b/src/test/ui/nll/polonius/call-kills-loans.rs similarity index 100% rename from src/test/ui/nll/polonius/calls-kill-loans.rs rename to src/test/ui/nll/polonius/call-kills-loans.rs diff --git a/src/test/ui/nll/polonius/storagedead-kills-loans.rs b/src/test/ui/nll/polonius/storagedead-kills-loans.rs index 3c121d3cd1882..f69bfdf459320 100644 --- a/src/test/ui/nll/polonius/storagedead-kills-loans.rs +++ b/src/test/ui/nll/polonius/storagedead-kills-loans.rs @@ -10,7 +10,8 @@ use std::{io, mem}; use std::io::Read; -fn fill(r: &mut Read, mut buf: &mut [u8]) -> io::Result<()> { +#[allow(dead_code)] +fn fill(r: &mut dyn Read, mut buf: &mut [u8]) -> io::Result<()> { while buf.len() > 0 { match r.read(buf).unwrap() { 0 => return Err(io::Error::new(io::ErrorKind::Other, From 9e0fb6fad256124354778783a135f3b0ac0553ee Mon Sep 17 00:00:00 2001 From: lqd Date: Mon, 15 Jul 2019 18:57:16 +0200 Subject: [PATCH 22/29] Make both polonius loans tests check-pass --- src/test/ui/nll/polonius/call-kills-loans.rs | 2 +- src/test/ui/nll/polonius/storagedead-kills-loans.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/nll/polonius/call-kills-loans.rs b/src/test/ui/nll/polonius/call-kills-loans.rs index 3121571478893..57dc140110246 100644 --- a/src/test/ui/nll/polonius/call-kills-loans.rs +++ b/src/test/ui/nll/polonius/call-kills-loans.rs @@ -4,7 +4,7 @@ // by NLL but was incorrectly rejected by Polonius because of these // missing `killed` facts. -// build-pass +// check-pass // compile-flags: -Z borrowck=mir -Z polonius // ignore-compare-mode-nll diff --git a/src/test/ui/nll/polonius/storagedead-kills-loans.rs b/src/test/ui/nll/polonius/storagedead-kills-loans.rs index f69bfdf459320..ff801cbf9f35d 100644 --- a/src/test/ui/nll/polonius/storagedead-kills-loans.rs +++ b/src/test/ui/nll/polonius/storagedead-kills-loans.rs @@ -3,7 +3,7 @@ // is correctly accepted by NLL but was incorrectly rejected by // Polonius because of these missing `killed` facts. -// build-pass +// check-pass // compile-flags: -Z borrowck=mir -Z polonius // ignore-compare-mode-nll From 823ab42e66cc966098030616e36e4aa7417dcf7f Mon Sep 17 00:00:00 2001 From: lqd Date: Mon, 15 Jul 2019 19:04:50 +0200 Subject: [PATCH 23/29] Bless output of test unboxed-closures/unboxed-closures-failed-recursive-fn-1.rs for Polonius --- ...ures-failed-recursive-fn-1.polonius.stderr | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/test/ui/unboxed-closures/unboxed-closures-failed-recursive-fn-1.polonius.stderr diff --git a/src/test/ui/unboxed-closures/unboxed-closures-failed-recursive-fn-1.polonius.stderr b/src/test/ui/unboxed-closures/unboxed-closures-failed-recursive-fn-1.polonius.stderr new file mode 100644 index 0000000000000..4b906f75149af --- /dev/null +++ b/src/test/ui/unboxed-closures/unboxed-closures-failed-recursive-fn-1.polonius.stderr @@ -0,0 +1,60 @@ +error[E0597]: `factorial` does not live long enough + --> $DIR/unboxed-closures-failed-recursive-fn-1.rs:15:17 + | +LL | let f = |x: u32| -> u32 { + | --------------- value captured here +LL | let g = factorial.as_ref().unwrap(); + | ^^^^^^^^^ borrowed value does not live long enough +... +LL | } + | - + | | + | `factorial` dropped here while still borrowed + | borrow might be used here, when `factorial` is dropped and runs the destructor for type `std::option::Option u32>>` + +error[E0506]: cannot assign to `factorial` because it is borrowed + --> $DIR/unboxed-closures-failed-recursive-fn-1.rs:20:5 + | +LL | let f = |x: u32| -> u32 { + | --------------- borrow of `factorial` occurs here +LL | let g = factorial.as_ref().unwrap(); + | --------- borrow occurs due to use in closure +... +LL | factorial = Some(Box::new(f)); + | ^^^^^^^^^ + | | + | assignment to borrowed `factorial` occurs here + | borrow later used here + +error[E0597]: `factorial` does not live long enough + --> $DIR/unboxed-closures-failed-recursive-fn-1.rs:28:17 + | +LL | let f = |x: u32| -> u32 { + | --------------- value captured here +LL | let g = factorial.as_ref().unwrap(); + | ^^^^^^^^^ borrowed value does not live long enough +... +LL | } + | - + | | + | `factorial` dropped here while still borrowed + | borrow might be used here, when `factorial` is dropped and runs the destructor for type `std::option::Option u32>>` + +error[E0506]: cannot assign to `factorial` because it is borrowed + --> $DIR/unboxed-closures-failed-recursive-fn-1.rs:33:5 + | +LL | let f = |x: u32| -> u32 { + | --------------- borrow of `factorial` occurs here +LL | let g = factorial.as_ref().unwrap(); + | --------- borrow occurs due to use in closure +... +LL | factorial = Some(Box::new(f)); + | ^^^^^^^^^ + | | + | assignment to borrowed `factorial` occurs here + | borrow later used here + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0506, E0597. +For more information about an error, try `rustc --explain E0506`. From 2f3e36f51a7055da3b55253edad7df594b0711d7 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 16 Jul 2019 17:30:41 +0200 Subject: [PATCH 24/29] Polonius: generate `killed` facts for assignments to projections --- .../borrow_check/nll/constraint_generation.rs | 89 ++++++++++++++++--- 1 file changed, 75 insertions(+), 14 deletions(-) diff --git a/src/librustc_mir/borrow_check/nll/constraint_generation.rs b/src/librustc_mir/borrow_check/nll/constraint_generation.rs index 25d0eaafc0049..95c3299693b68 100644 --- a/src/librustc_mir/borrow_check/nll/constraint_generation.rs +++ b/src/librustc_mir/borrow_check/nll/constraint_generation.rs @@ -3,12 +3,15 @@ use crate::borrow_check::location::LocationTable; use crate::borrow_check::nll::ToRegionVid; use crate::borrow_check::nll::facts::AllFacts; use crate::borrow_check::nll::region_infer::values::LivenessValues; +use crate::borrow_check::places_conflict; use rustc::infer::InferCtxt; use rustc::mir::visit::TyContext; use rustc::mir::visit::Visitor; -use rustc::mir::{BasicBlock, BasicBlockData, Location, Body, Place, PlaceBase, Rvalue, TerminatorKind}; -use rustc::mir::{Local, SourceInfo, Statement, StatementKind, Terminator}; -use rustc::mir::UserTypeProjection; +use rustc::mir::{ + BasicBlock, BasicBlockData, Body, Local, Location, Place, PlaceBase, Projection, + ProjectionElem, Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, + UserTypeProjection, +}; use rustc::ty::fold::TypeFoldable; use rustc::ty::{self, ClosureSubsts, GeneratorSubsts, RegionVid, Ty}; use rustc::ty::subst::SubstsRef; @@ -27,6 +30,7 @@ pub(super) fn generate_constraints<'cx, 'tcx>( liveness_constraints, location_table, all_facts, + body, }; for (bb, data) in body.basic_blocks().iter_enumerated() { @@ -41,6 +45,7 @@ struct ConstraintGeneration<'cg, 'cx, 'tcx> { location_table: &'cg LocationTable, liveness_constraints: &'cg mut LivenessValues, borrow_set: &'cg BorrowSet<'tcx>, + body: &'cg Body<'tcx>, } impl<'cg, 'cx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'tcx> { @@ -212,17 +217,73 @@ impl<'cx, 'cg, 'tcx> ConstraintGeneration<'cx, 'cg, 'tcx> { /// as `killed`. For example, when assigning to a local, or on a call's return destination. fn record_killed_borrows_for_place(&mut self, place: &Place<'tcx>, location: Location) { if let Some(all_facts) = self.all_facts { - if let Place { - base: PlaceBase::Local(local), - projection: None, - } = place { - record_killed_borrows_for_local( - all_facts, - self.borrow_set, - self.location_table, - local, - location, - ); + // Depending on the `Place` we're killing: + // - if it's a local, or a single deref of a local, + // we kill all the borrows on the local. + // - if it's a deeper projection, we have to filter which + // of the borrows are killed: the ones whose `borrowed_place` + // conflicts with the `place`. + match place { + Place { + base: PlaceBase::Local(local), + projection: None, + } | + Place { + base: PlaceBase::Local(local), + projection: Some(box Projection { + base: None, + elem: ProjectionElem::Deref, + }), + } => { + debug!( + "Recording `killed` facts for borrows of local={:?} at location={:?}", + local, location + ); + + record_killed_borrows_for_local( + all_facts, + self.borrow_set, + self.location_table, + local, + location, + ); + } + + Place { + base: PlaceBase::Static(_), + .. + } => { + // Ignore kills of static or static mut variables. + } + + Place { + base: PlaceBase::Local(local), + projection: Some(_), + } => { + // Kill conflicting borrows of the innermost local. + debug!( + "Recording `killed` facts for borrows of \ + innermost projected local={:?} at location={:?}", + local, location + ); + + if let Some(borrow_indices) = self.borrow_set.local_map.get(local) { + for &borrow_index in borrow_indices { + let places_conflict = places_conflict::places_conflict( + self.infcx.tcx, + self.body, + &self.borrow_set.borrows[borrow_index].borrowed_place, + place, + places_conflict::PlaceConflictBias::NoOverlap, + ); + + if places_conflict { + let location_index = self.location_table.mid_index(location); + all_facts.killed.push((borrow_index, location_index)); + } + } + } + } } } } From d41e002111dd61efa08d996cfc1085fc00472288 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 16 Jul 2019 17:32:38 +0200 Subject: [PATCH 25/29] Add test checking various assignments are accepted in Polonius --- .../ui/nll/polonius/assignment-kills-loans.rs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/test/ui/nll/polonius/assignment-kills-loans.rs diff --git a/src/test/ui/nll/polonius/assignment-kills-loans.rs b/src/test/ui/nll/polonius/assignment-kills-loans.rs new file mode 100644 index 0000000000000..8301f603cb4d3 --- /dev/null +++ b/src/test/ui/nll/polonius/assignment-kills-loans.rs @@ -0,0 +1,88 @@ +#![allow(dead_code)] + +// This tests the various kinds of assignments there are. Polonius used to generate `killed` +// facts only on simple assigments, but not projections, incorrectly causing errors to be emitted +// for code accepted by NLL. They are all variations from example code in the NLL RFC. + +// check-pass +// compile-flags: -Z borrowck=mir -Z polonius +// ignore-compare-mode-nll + +struct List { + value: T, + next: Option>>, +} + +// Assignment to a local: the `list` assignment should clear the existing +// borrows of `list.value` and `list.next` +fn assignment_to_local(mut list: &mut List) -> Vec<&mut T> { + let mut result = vec![]; + loop { + result.push(&mut list.value); + if let Some(n) = list.next.as_mut() { + list = n; + } else { + return result; + } + } +} + +// Assignment to a deref projection: the `*list` assignment should clear the existing +// borrows of `list.value` and `list.next` +fn assignment_to_deref_projection(mut list: Box<&mut List>) -> Vec<&mut T> { + let mut result = vec![]; + loop { + result.push(&mut list.value); + if let Some(n) = list.next.as_mut() { + *list = n; + } else { + return result; + } + } +} + +// Assignment to a field projection: the `list.0` assignment should clear the existing +// borrows of `list.0.value` and `list.0.next` +fn assignment_to_field_projection(mut list: (&mut List,)) -> Vec<&mut T> { + let mut result = vec![]; + loop { + result.push(&mut list.0.value); + if let Some(n) = list.0.next.as_mut() { + list.0 = n; + } else { + return result; + } + } +} + +// Assignment to a deref field projection: the `*list.0` assignment should clear the existing +// borrows of `list.0.value` and `list.0.next` +fn assignment_to_deref_field_projection(mut list: (Box<&mut List>,)) -> Vec<&mut T> { + let mut result = vec![]; + loop { + result.push(&mut list.0.value); + if let Some(n) = list.0.next.as_mut() { + *list.0 = n; + } else { + return result; + } + } +} + +// Similar to `assignment_to_deref_field_projection` but through a longer projection chain +fn assignment_through_projection_chain( + mut list: (((((Box<&mut List>,),),),),), +) -> Vec<&mut T> { + let mut result = vec![]; + loop { + result.push(&mut ((((list.0).0).0).0).0.value); + if let Some(n) = ((((list.0).0).0).0).0.next.as_mut() { + *((((list.0).0).0).0).0 = n; + } else { + return result; + } + } +} + +fn main() { +} \ No newline at end of file From 770129c28039ab7501cdc0f2f7ca1a38e22208f0 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 16 Jul 2019 17:33:16 +0200 Subject: [PATCH 26/29] Add test to check that assignments to projections do not kill too many loans --- .../polonius/assignment-to-differing-field.rs | 50 ++++++++++++++++++ .../assignment-to-differing-field.stderr | 51 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/test/ui/nll/polonius/assignment-to-differing-field.rs create mode 100644 src/test/ui/nll/polonius/assignment-to-differing-field.stderr diff --git a/src/test/ui/nll/polonius/assignment-to-differing-field.rs b/src/test/ui/nll/polonius/assignment-to-differing-field.rs new file mode 100644 index 0000000000000..c0ba1b983fc35 --- /dev/null +++ b/src/test/ui/nll/polonius/assignment-to-differing-field.rs @@ -0,0 +1,50 @@ +#![allow(dead_code)] + +// Compared to `assignment-kills-loans.rs`, we check here +// that we do not kill too many borrows. Assignments to the `.1` +// field projections should leave the borrows on `.0` intact. + +// compile-flags: -Z borrowck=mir -Z polonius +// ignore-compare-mode-nll + +struct List { + value: T, + next: Option>>, +} + + +fn assignment_to_field_projection<'a, T>( + mut list: (&'a mut List, &'a mut List), +) -> Vec<&'a mut T> { + let mut result = vec![]; + loop { + result.push(&mut (list.0).value); + //~^ ERROR cannot borrow `list.0.value` as mutable + + if let Some(n) = (list.0).next.as_mut() { + //~^ ERROR cannot borrow `list.0.next` as mutable + list.1 = n; + } else { + return result; + } + } +} + +fn assignment_through_projection_chain<'a, T>( + mut list: (((((Box<&'a mut List>, Box<&'a mut List>),),),),), +) -> Vec<&'a mut T> { + let mut result = vec![]; + loop { + result.push(&mut ((((list.0).0).0).0).0.value); + //~^ ERROR cannot borrow `list.0.0.0.0.0.value` as mutable + + if let Some(n) = ((((list.0).0).0).0).0.next.as_mut() { + //~^ ERROR cannot borrow `list.0.0.0.0.0.next` as mutable + *((((list.0).0).0).0).1 = n; + } else { + return result; + } + } +} + +fn main() {} diff --git a/src/test/ui/nll/polonius/assignment-to-differing-field.stderr b/src/test/ui/nll/polonius/assignment-to-differing-field.stderr new file mode 100644 index 0000000000000..07ca021b53bce --- /dev/null +++ b/src/test/ui/nll/polonius/assignment-to-differing-field.stderr @@ -0,0 +1,51 @@ +error[E0499]: cannot borrow `list.0.value` as mutable more than once at a time + --> $DIR/assignment-to-differing-field.rs:21:21 + | +LL | fn assignment_to_field_projection<'a, T>( + | -- lifetime `'a` defined here +... +LL | result.push(&mut (list.0).value); + | ^^^^^^^^^^^^^^^^^^^ mutable borrow starts here in previous iteration of loop +... +LL | return result; + | ------ returning this value requires that `list.0.value` is borrowed for `'a` + +error[E0499]: cannot borrow `list.0.next` as mutable more than once at a time + --> $DIR/assignment-to-differing-field.rs:24:26 + | +LL | fn assignment_to_field_projection<'a, T>( + | -- lifetime `'a` defined here +... +LL | if let Some(n) = (list.0).next.as_mut() { + | ^^^^^^^^^^^^^--------- + | | + | mutable borrow starts here in previous iteration of loop + | argument requires that `list.0.next` is borrowed for `'a` + +error[E0499]: cannot borrow `list.0.0.0.0.0.value` as mutable more than once at a time + --> $DIR/assignment-to-differing-field.rs:38:21 + | +LL | fn assignment_through_projection_chain<'a, T>( + | -- lifetime `'a` defined here +... +LL | result.push(&mut ((((list.0).0).0).0).0.value); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow starts here in previous iteration of loop +... +LL | return result; + | ------ returning this value requires that `list.0.0.0.0.0.value` is borrowed for `'a` + +error[E0499]: cannot borrow `list.0.0.0.0.0.next` as mutable more than once at a time + --> $DIR/assignment-to-differing-field.rs:41:26 + | +LL | fn assignment_through_projection_chain<'a, T>( + | -- lifetime `'a` defined here +... +LL | if let Some(n) = ((((list.0).0).0).0).0.next.as_mut() { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^--------- + | | + | mutable borrow starts here in previous iteration of loop + | argument requires that `list.0.0.0.0.0.next` is borrowed for `'a` + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0499`. From c7f9a71d7875e5acced7f3a1b9f05a46be2104f4 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 16 Jul 2019 17:34:06 +0200 Subject: [PATCH 27/29] issue-46589 passes in Polonius and fails in NLL, duplicate it and manually check each outcome --- src/test/ui/nll/issue-46589.rs | 6 +++++ src/test/ui/nll/issue-46589.stderr | 2 +- src/test/ui/nll/polonius/issue-46589.rs | 32 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/nll/polonius/issue-46589.rs diff --git a/src/test/ui/nll/issue-46589.rs b/src/test/ui/nll/issue-46589.rs index 8c0c356e96721..0a4c20d15159f 100644 --- a/src/test/ui/nll/issue-46589.rs +++ b/src/test/ui/nll/issue-46589.rs @@ -1,3 +1,9 @@ +// This tests passes in Polonius mode, so is skipped in the automated compare-mode. +// We will manually check it passes in Polonius tests, as we can't have a test here +// which conditionally passes depending on a test revision/compile-flags. + +// ignore-compare-mode-polonius + struct Foo; impl Foo { diff --git a/src/test/ui/nll/issue-46589.stderr b/src/test/ui/nll/issue-46589.stderr index 397909a436610..82cd364eeffd0 100644 --- a/src/test/ui/nll/issue-46589.stderr +++ b/src/test/ui/nll/issue-46589.stderr @@ -1,5 +1,5 @@ error[E0499]: cannot borrow `**other` as mutable more than once at a time - --> $DIR/issue-46589.rs:17:21 + --> $DIR/issue-46589.rs:23:21 | LL | *other = match (*other).get_self() { | -------- first mutable borrow occurs here diff --git a/src/test/ui/nll/polonius/issue-46589.rs b/src/test/ui/nll/polonius/issue-46589.rs new file mode 100644 index 0000000000000..b5792587ff0ec --- /dev/null +++ b/src/test/ui/nll/polonius/issue-46589.rs @@ -0,0 +1,32 @@ +// This test is a copy of `ui/nll/issue-46589.rs` which fails in NLL but succeeds in Polonius. +// As we can't have a test here which conditionally passes depending on a test +// revision/compile-flags. We ensure here that it passes in Polonius mode. + +// check-pass +// compile-flags: -Z borrowck=mir -Z polonius +// ignore-compare-mode-nll + +struct Foo; + +impl Foo { + fn get_self(&mut self) -> Option<&mut Self> { + Some(self) + } + + fn new_self(&mut self) -> &mut Self { + self + } + + fn trigger_bug(&mut self) { + let other = &mut (&mut *self); + + *other = match (*other).get_self() { + Some(s) => s, + None => (*other).new_self() + }; + + let c = other; + } +} + +fn main() {} From c0eab36bce7625bc45dec180dd08c7aa679cf1d7 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 16 Jul 2019 17:35:21 +0200 Subject: [PATCH 28/29] Bless output of test nll/loan_ends_mid_block_pair.rs for Polonius, again Fixing assignments to projections made the Polonius output exactly the same as the NLL one. --- .../loan_ends_mid_block_pair.polonius.stderr | 25 +------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/src/test/ui/nll/loan_ends_mid_block_pair.polonius.stderr b/src/test/ui/nll/loan_ends_mid_block_pair.polonius.stderr index c9537d42c41af..eb8442b31d7c7 100644 --- a/src/test/ui/nll/loan_ends_mid_block_pair.polonius.stderr +++ b/src/test/ui/nll/loan_ends_mid_block_pair.polonius.stderr @@ -10,29 +10,6 @@ LL | data.0 = 'e'; LL | capitalize(c); | - borrow later used here -error[E0506]: cannot assign to `data.0` because it is borrowed - --> $DIR/loan_ends_mid_block_pair.rs:14:5 - | -LL | let c = &mut data.0; - | ----------- borrow of `data.0` occurs here -... -LL | data.0 = 'f'; - | ^^^^^^^^^^^^ assignment to borrowed `data.0` occurs here -LL | data.0 = 'g'; -LL | capitalize(c); - | - borrow later used here - -error[E0506]: cannot assign to `data.0` because it is borrowed - --> $DIR/loan_ends_mid_block_pair.rs:15:5 - | -LL | let c = &mut data.0; - | ----------- borrow of `data.0` occurs here -... -LL | data.0 = 'g'; - | ^^^^^^^^^^^^ assignment to borrowed `data.0` occurs here -LL | capitalize(c); - | - borrow later used here - -error: aborting due to 3 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0506`. From e16bede3f503b85685e80b39942fbc0b0e11a721 Mon Sep 17 00:00:00 2001 From: lqd Date: Tue, 16 Jul 2019 18:51:13 +0200 Subject: [PATCH 29/29] fix tidy --- src/test/ui/nll/polonius/assignment-kills-loans.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/nll/polonius/assignment-kills-loans.rs b/src/test/ui/nll/polonius/assignment-kills-loans.rs index 8301f603cb4d3..a80c62d19d5a6 100644 --- a/src/test/ui/nll/polonius/assignment-kills-loans.rs +++ b/src/test/ui/nll/polonius/assignment-kills-loans.rs @@ -85,4 +85,4 @@ fn assignment_through_projection_chain( } fn main() { -} \ No newline at end of file +}