Auto merge of #54782 - pnkfelix:issue-54556-semi-on-tail-diagnostic, r=nikomatsakis

NLL: temps in block tail expression diagnostic

This change adds a diagnostic that explains when temporaries in a block tail expression live longer than block local variables that they borrow, and attempts to suggest turning the tail expresion into a statement (either by adding a semicolon at the end, when its result value is clearly unused, or by introducing a `let`-binding for the result value and then returning that).

Fix #54556
This commit is contained in:
bors 2018-10-07 00:28:26 +00:00
commit dbecb7a644
66 changed files with 958 additions and 141 deletions

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `c_shortest` dropped here while still borrowed
| borrow later used here, when `dt` is dropped
| borrow might be used here, when `dt` is dropped and runs the `Drop` code for type `other::Dt`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `c_shortest` dropped here while still borrowed
| borrow later used here, when `dt` is dropped
| borrow might be used here, when `dt` is dropped and runs the `Drop` code for type `Dt`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `c_shortest` dropped here while still borrowed
| borrow later used here, when `dt` is dropped
| borrow might be used here, when `dt` is dropped and runs the `Drop` code for type `Dt`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -7,7 +7,7 @@ LL | }
| -
| |
| `v` dropped here while still borrowed
| borrow later used here, when `v` is dropped
| borrow might be used here, when `v` is dropped and runs the `Drop` code for type `Wrap`
error: aborting due to previous error

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `y` dropped here while still borrowed
| borrow later used here, when `x` is dropped
| borrow might be used here, when `x` is dropped and runs the `Drop` code for type `Foo`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -2,10 +2,17 @@ error[E0597]: `a` does not live long enough
--> $DIR/borrowing.rs:18:18
|
LL | unsafe { (|| yield &a).resume() }
| ^^^^^^^^^^^^^ borrowed value does not live long enough
| ^^^^^^^^^^^^^
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
LL | //~^ ERROR: `a` does not live long enough
LL | };
| - `a` dropped here while still borrowed
| -- ... and the borrow might be used here, when that temporary is dropped and runs the destructor for generator
| |
| `a` dropped here while still borrowed
|
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.
error[E0597]: `a` does not live long enough
--> $DIR/borrowing.rs:24:9

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `*cell` dropped here while still borrowed
| borrow later used here, when `gen` is dropped
| borrow might be used here, when `gen` is dropped and runs the destructor for generator
|
= note: values in a scope are dropped in the opposite order they are defined
@ -27,7 +27,7 @@ LL | }
| -
| |
| `ref_` dropped here while still borrowed
| borrow later used here, when `gen` is dropped
| borrow might be used here, when `gen` is dropped and runs the destructor for generator
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -11,7 +11,7 @@ LL | c.push(Box::new(|| y = 0));
| second mutable borrow occurs here
LL | //~^ ERROR cannot borrow `y` as mutable more than once at a time
LL | }
| - first borrow later used here, when `c` is dropped
| - first borrow might be used here, when `c` is dropped and runs the destructor for type `std::cell::RefCell<std::vec::Vec<std::boxed::Box<dyn std::ops::FnMut()>>>`
error[E0499]: cannot borrow `y` as mutable more than once at a time
--> $DIR/issue-18783.rs:26:29
@ -26,7 +26,7 @@ LL | Push::push(&c, Box::new(|| y = 0));
| second mutable borrow occurs here
LL | //~^ ERROR cannot borrow `y` as mutable more than once at a time
LL | }
| - first borrow later used here, when `c` is dropped
| - first borrow might be used here, when `c` is dropped and runs the destructor for type `std::cell::RefCell<std::vec::Vec<std::boxed::Box<dyn std::ops::FnMut()>>>`
error: aborting due to 2 previous errors

View file

@ -3,9 +3,15 @@ error[E0502]: cannot borrow `heap` as immutable because it is also borrowed as m
|
LL | let borrow = heap.peek_mut();
| ---- mutable borrow occurs here
...
LL |
LL | match (borrow, ()) {
| ------------ a temporary with access to the mutable borrow is created here ...
LL | (Some(_), ()) => {
LL | println!("{:?}", heap); //~ ERROR cannot borrow `heap` as immutable
| ^^^^ immutable borrow occurs here
...
LL | };
| - ... and the mutable borrow might be used here, when that temporary is dropped and runs the destructor for type `(std::option::Option<std::collections::binary_heap::PeekMut<'_, i32>>, ())`
error: aborting due to previous error

View file

@ -8,7 +8,7 @@ LL | v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowe
| ^^^^^^^^^ assignment to borrowed `v[..]` occurs here
...
LL | }
| - borrow later used here, when `p` is dropped
| - borrow might be used here, when `p` is dropped and runs the `Drop` code for type `WrapMayNotDangle`
error[E0506]: cannot assign to `v[..]` because it is borrowed
--> $DIR/drop-no-may-dangle.rs:33:5
@ -19,7 +19,7 @@ LL | let p: WrapMayNotDangle<&usize> = WrapMayNotDangle { value: &v[0] };
LL | v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
| ^^^^^^^^^ assignment to borrowed `v[..]` occurs here
LL | }
| - borrow later used here, when `p` is dropped
| - borrow might be used here, when `p` is dropped and runs the `Drop` code for type `WrapMayNotDangle`
error: aborting due to 2 previous errors

View file

@ -0,0 +1,20 @@
// (this works, but only in NLL)
// compile-pass
#![feature(nll)]
use std::collections::HashMap;
use std::sync::Mutex;
fn i_used_to_be_able_to(foo: &Mutex<HashMap<usize, usize>>) -> Vec<(usize, usize)> {
let mut foo = foo.lock().unwrap();
foo.drain().collect()
}
fn but_after_nightly_update_now_i_gotta(foo: &Mutex<HashMap<usize, usize>>) -> Vec<(usize, usize)> {
let mut foo = foo.lock().unwrap();
return foo.drain().collect();
}
fn main() {}

View file

@ -0,0 +1,19 @@
// (this works, but only in NLL)
// compile-pass
#![feature(nll)]
fn from_stdin(min: u64) -> Vec<u64> {
use std::io::BufRead;
let stdin = std::io::stdin();
let stdin = stdin.lock();
stdin.lines()
.map(Result::unwrap)
.map(|val| val.parse())
.map(Result::unwrap)
.filter(|val| *val >= min)
.collect()
}
fn main() {}

View file

@ -0,0 +1,20 @@
error[E0597]: `counter` does not live long enough
--> $DIR/issue-54556-niconii.rs:22:20
|
LL | if let Ok(_) = counter.lock() { }
| ^^^^^^^-------
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
...
LL | }
| -
| |
| `counter` dropped here while still borrowed
| ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::result::Result<MutexGuard<'_>, ()>`
|
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,31 @@
// This is a reduction of a concrete test illustrating a case that was
// annoying to Rust developer niconii (see comment thread on #21114).
//
// With resolving issue #54556, pnkfelix hopes that the new diagnostic
// output produced by NLL helps to *explain* the semantic significance
// of temp drop order, and thus why inserting a semi-colon after the
// `if let` expression in `main` works.
struct Mutex;
struct MutexGuard<'a>(&'a Mutex);
impl Drop for Mutex { fn drop(&mut self) { println!("Mutex::drop"); } }
impl<'a> Drop for MutexGuard<'a> { fn drop(&mut self) { println!("MutexGuard::drop"); } }
impl Mutex {
fn lock(&self) -> Result<MutexGuard, ()> { Ok(MutexGuard(self)) }
}
fn main() {
let counter = Mutex;
if let Ok(_) = counter.lock() { }
// With this code as written, the dynamic semantics here implies
// that `Mutex::drop` for `counter` runs *before*
// `MutexGuard::drop`, which would be unsound since `MutexGuard`
// still has a reference to `counter`.
//
// The goal of #54556 is to explain that within a compiler
// diagnostic.
}

View file

@ -0,0 +1,14 @@
error[E0597]: `counter` does not live long enough
--> $DIR/issue-54556-niconii.rs:22:20
|
LL | if let Ok(_) = counter.lock() { }
| ^^^^^^^ borrowed value does not live long enough
...
LL | }
| - `counter` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,19 @@
error[E0597]: `stmt` does not live long enough
--> $DIR/issue-54556-stephaneyfx.rs:27:21
|
LL | let rows = Rows(&stmt);
| ^^^^^ borrowed value does not live long enough
LL | rows.map(|row| row).next()
| ------------------- a temporary with access to the borrow is created here ...
...
LL | }
| -
| |
| `stmt` dropped here while still borrowed
| ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::iter::Map<Rows<'_>, [closure@$DIR/issue-54556-stephaneyfx.rs:28:14: 28:23]>`
|
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,35 @@
// This is a reduction of a concrete test illustrating a case that was
// annoying to Rust developer stephaneyfx (see issue #46413).
//
// With resolving issue #54556, pnkfelix hopes that the new diagnostic
// output produced by NLL helps to *explain* the semantic significance
// of temp drop order, and thus why storing the result in `x` and then
// returning `x` works.
pub struct Statement;
pub struct Rows<'stmt>(&'stmt Statement);
impl<'stmt> Drop for Rows<'stmt> {
fn drop(&mut self) {}
}
impl<'stmt> Iterator for Rows<'stmt> {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
None
}
}
fn get_names() -> Option<String> {
let stmt = Statement;
let rows = Rows(&stmt);
rows.map(|row| row).next()
// let x = rows.map(|row| row).next();
// x
//
// Removing the map works too as does removing the Drop impl.
}
fn main() {}

View file

@ -0,0 +1,14 @@
error[E0597]: `stmt` does not live long enough
--> $DIR/issue-54556-stephaneyfx.rs:27:22
|
LL | let rows = Rows(&stmt);
| ^^^^ borrowed value does not live long enough
...
LL | }
| - `stmt` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,19 @@
error[E0597]: `_thing1` does not live long enough
--> $DIR/issue-54556-temps-in-tail-diagnostic.rs:5:11
|
LL | D(&_thing1).end()
| --^^^^^^^^-
| | |
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
LL | }
| - `_thing1` dropped here while still borrowed
LL |
LL | ;
| - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
|
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,23 @@
fn main() {
{
let mut _thing1 = D(Box::new("thing1"));
// D("other").next(&_thing1).end()
D(&_thing1).end()
}
;
}
#[derive(Debug)]
struct D<T: std::fmt::Debug>(T);
impl<T: std::fmt::Debug> Drop for D<T> {
fn drop(&mut self) {
println!("dropping {:?})", self);
}
}
impl<T: std::fmt::Debug> D<T> {
fn next<U: std::fmt::Debug>(&self, _other: U) -> D<U> { D(_other) }
fn end(&self) { }
}

View file

@ -0,0 +1,14 @@
error[E0597]: `_thing1` does not live long enough
--> $DIR/issue-54556-temps-in-tail-diagnostic.rs:5:12
|
LL | D(&_thing1).end()
| ^^^^^^^ borrowed value does not live long enough
LL | }
| - `_thing1` dropped here while still borrowed
LL |
LL | ;
| - borrowed value needs to live until here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,113 @@
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:10:55
|
LL | { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // suggest `;`
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
| | | |
| | | `_t1` dropped here while still borrowed
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
|
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:12:55
|
LL | { { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } } ; // suggest `;`
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
| | | |
| | | `_t1` dropped here while still borrowed
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
|
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:14:55
|
LL | { { let mut _t1 = D(Box::new("t1")); D(&_t1).end() }; } // suggest `;`
| --^^^^- -- ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
| | | |
| | | `_t1` dropped here while still borrowed
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
|
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:16:55
|
LL | let _ = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // suggest `;`
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
| | | |
| | | `_t1` dropped here while still borrowed
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
|
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:18:55
|
LL | let _u = { let mut _t1 = D(Box::new("t1")); D(&_t1).unit() } ; // suggest `;`
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
| | | |
| | | `_t1` dropped here while still borrowed
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
|
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:20:55
|
LL | let _x = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // `let x = ...; x`
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
| | | |
| | | `_t1` dropped here while still borrowed
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
|
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:24:55
|
LL | _y = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // `let x = ...; x`
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
| | | |
| | | `_t1` dropped here while still borrowed
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
|
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:30:55
|
LL | fn f_local_ref() { let mut _t1 = D(Box::new("t1")); D(&_t1).unit() } // suggest `;`
| --^^^^- -
| | | |
| | | `_t1` dropped here while still borrowed
| | | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
|
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:32:55
|
LL | fn f() -> String { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } // `let x = ...; x`
| --^^^^- -
| | | |
| | | `_t1` dropped here while still borrowed
| | | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
| | borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
|
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.
error: aborting due to 9 previous errors
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,48 @@
// Ths test case is exploring the space of how blocs with tail
// expressions and statements can be composed, trying to keep each
// case on one line so that we can compare them via a vertical scan
// with the human eye.
// Each comment on the right side of the line is summarizing the
// expected suggestion from the diagnostic for issue #54556.
fn main() {
{ let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // suggest `;`
{ { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } } ; // suggest `;`
{ { let mut _t1 = D(Box::new("t1")); D(&_t1).end() }; } // suggest `;`
let _ = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // suggest `;`
let _u = { let mut _t1 = D(Box::new("t1")); D(&_t1).unit() } ; // suggest `;`
let _x = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // `let x = ...; x`
let _x = { let mut _t1 = D(Box::new("t1")); let x = D(&_t1).end(); x } ; // no error
let mut _y;
_y = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // `let x = ...; x`
_y = { let mut _t1 = D(Box::new("t1")); let x = D(&_t1).end(); x } ; // no error
}
fn f_param_ref(_t1: D<Box<&'static str>>) { D(&_t1).unit() } // no error
fn f_local_ref() { let mut _t1 = D(Box::new("t1")); D(&_t1).unit() } // suggest `;`
fn f() -> String { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } // `let x = ...; x`
#[derive(Debug)]
struct D<T: std::fmt::Debug>(T);
impl<T: std::fmt::Debug> Drop for D<T> {
fn drop(&mut self) {
println!("dropping {:?})", self);
}
}
impl<T: std::fmt::Debug> D<T> {
fn next<U: std::fmt::Debug>(&self, _other: U) -> D<U> { D(_other) }
fn end(&self) -> String { format!("End({:?})", self.0) }
fn unit(&self) { }
}

View file

@ -0,0 +1,86 @@
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:10:56
|
LL | { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // suggest `;`
| ^^^ - - borrowed value needs to live until here
| | |
| | `_t1` dropped here while still borrowed
| borrowed value does not live long enough
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:12:56
|
LL | { { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } } ; // suggest `;`
| ^^^ - - borrowed value needs to live until here
| | |
| | `_t1` dropped here while still borrowed
| borrowed value does not live long enough
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:14:56
|
LL | { { let mut _t1 = D(Box::new("t1")); D(&_t1).end() }; } // suggest `;`
| ^^^ -- borrowed value needs to live until here
| | |
| | `_t1` dropped here while still borrowed
| borrowed value does not live long enough
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:16:56
|
LL | let _ = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // suggest `;`
| ^^^ - - borrowed value needs to live until here
| | |
| | `_t1` dropped here while still borrowed
| borrowed value does not live long enough
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:18:56
|
LL | let _u = { let mut _t1 = D(Box::new("t1")); D(&_t1).unit() } ; // suggest `;`
| ^^^ - - borrowed value needs to live until here
| | |
| | `_t1` dropped here while still borrowed
| borrowed value does not live long enough
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:20:56
|
LL | let _x = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // `let x = ...; x`
| ^^^ - - borrowed value needs to live until here
| | |
| | `_t1` dropped here while still borrowed
| borrowed value does not live long enough
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:24:56
|
LL | _y = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // `let x = ...; x`
| ^^^ - - borrowed value needs to live until here
| | |
| | `_t1` dropped here while still borrowed
| borrowed value does not live long enough
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:30:56
|
LL | fn f_local_ref() { let mut _t1 = D(Box::new("t1")); D(&_t1).unit() } // suggest `;`
| ^^^ - `_t1` dropped here while still borrowed
| |
| borrowed value does not live long enough
|
= note: values in a scope are dropped in the opposite order they are created
error[E0597]: `_t1` does not live long enough
--> $DIR/issue-54556-used-vs-unused-tails.rs:32:56
|
LL | fn f() -> String { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } // `let x = ...; x`
| ^^^ - `_t1` dropped here while still borrowed
| |
| borrowed value does not live long enough
|
= note: values in a scope are dropped in the opposite order they are created
error: aborting due to 9 previous errors
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,14 @@
error[E0506]: cannot assign to `x` because it is borrowed
--> $DIR/issue-54556-wrap-it-up.rs:27:5
|
LL | let wrap = Wrap { p: &mut x };
| ------ borrow of `x` occurs here
...
LL | x = 1; //~ ERROR cannot assign to `x` because it is borrowed [E0506]
| ^^^^^ assignment to borrowed `x` occurs here
LL | }
| - borrow might be used here, when `foo` is dropped and runs the destructor for type `Foo<'_>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0506`.

View file

@ -0,0 +1,28 @@
// This is testing how the diagnostic from issue #54556 behaves when
// the destructor code is attached to a place held in a field of the
// temporary being dropped.
//
// Eventually it would be nice if the diagnostic would actually report
// that specific place and its type that implements the `Drop` trait.
// But for the short term, it is acceptable to just print out the
// whole type of the temporary.
#![allow(warnings)]
struct Wrap<'p> { p: &'p mut i32 }
impl<'p> Drop for Wrap<'p> {
fn drop(&mut self) {
*self.p += 1;
}
}
struct Foo<'p> { a: String, b: Wrap<'p> }
fn main() {
let mut x = 0;
let wrap = Wrap { p: &mut x };
let s = String::from("str");
let foo = Foo { a: s, b: wrap };
x = 1; //~ ERROR cannot assign to `x` because it is borrowed [E0506]
}

View file

@ -0,0 +1,12 @@
error[E0506]: cannot assign to `x` because it is borrowed
--> $DIR/issue-54556-wrap-it-up.rs:27:5
|
LL | let wrap = Wrap { p: &mut x };
| - borrow of `x` occurs here
...
LL | x = 1; //~ ERROR cannot assign to `x` because it is borrowed [E0506]
| ^^^^^ assignment to borrowed `x` occurs here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0506`.

View file

@ -8,7 +8,7 @@ LL | x = 1; //~ ERROR cannot assign to `x` because it is borrowed [E0506]
| ^^^^^ assignment to borrowed `x` occurs here
LL | // FIXME ^ Should not error in the future with implicit dtors, only manually implemented ones
LL | }
| - borrow later used here, when `foo` is dropped
| - borrow might be used here, when `foo` is dropped and runs the destructor for type `Foo<'_>`
error: aborting due to previous error

View file

@ -7,7 +7,7 @@ LL | let wrap = Wrap { p: &mut x };
LL | x = 1; //~ ERROR cannot assign to `x` because it is borrowed [E0506]
| ^^^^^ assignment to borrowed `x` occurs here
LL | }
| - borrow later used here, when `foo` is dropped
| - borrow might be used here, when `foo` is dropped and runs the destructor for type `Foo<'_>`
error: aborting due to previous error

View file

@ -8,7 +8,7 @@ LL | x = 1; //~ ERROR cannot assign to `x` because it is borrowed [E0506]
| ^^^^^ assignment to borrowed `x` occurs here
LL | // FIXME ^ This currently errors and it should not.
LL | }
| - borrow later used here, when `foo` is dropped
| - borrow might be used here, when `foo` is dropped and runs the destructor for type `Foo<'_>`
error: aborting due to previous error

View file

@ -6,7 +6,7 @@ LL | let wrap = Wrap { p: &mut x };
LL | x = 1; //~ ERROR cannot assign to `x` because it is borrowed [E0506]
| ^^^^^ assignment to borrowed `x` occurs here
LL | }
| - borrow later used here, when `wrap` is dropped
| - borrow might be used here, when `wrap` is dropped and runs the `Drop` code for type `Wrap`
error: aborting due to previous error

View file

@ -2,9 +2,16 @@ error[E0597]: `*a` does not live long enough
--> $DIR/destructor-restrictions.rs:18:10
|
LL | *a.borrow() + 1
| ^ borrowed value does not live long enough
| ^---------
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
LL | }; //~^ ERROR `*a` does not live long enough
| - `*a` dropped here while still borrowed
| -- ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::cell::Ref<'_, i32>`
| |
| `*a` dropped here while still borrowed
|
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.
error: aborting due to previous error

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `*m` dropped here while still borrowed
| borrow later used here, when `m` is dropped
| borrow might be used here, when `m` is dropped and runs the destructor for type `std::boxed::Box<dyn Trait<'_>>`
error: aborting due to previous error

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `b2` dropped here while still borrowed
| borrow later used here, when `b1` is dropped
| borrow might be used here, when `b1` is dropped and runs the destructor for type `B<'_>`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -22,7 +22,7 @@ LL | }
| -
| |
| `b3` dropped here while still borrowed
| borrow later used here, when `b1` is dropped
| borrow might be used here, when `b1` is dropped and runs the destructor for type `B<'_>`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -36,7 +36,7 @@ LL | }
| -
| |
| `b1` dropped here while still borrowed
| borrow later used here, when `b1` is dropped
| borrow might be used here, when `b1` is dropped and runs the destructor for type `B<'_>`
error: aborting due to 3 previous errors

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `d2` dropped here while still borrowed
| borrow later used here, when `d1` is dropped
| borrow might be used here, when `d1` is dropped and runs the `Drop` code for type `D`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -22,7 +22,7 @@ LL | }
| -
| |
| `d1` dropped here while still borrowed
| borrow later used here, when `d1` is dropped
| borrow might be used here, when `d1` is dropped and runs the `Drop` code for type `D`
error: aborting due to 2 previous errors

View file

@ -7,7 +7,7 @@ LL | }
| -
| |
| `bomb` dropped here while still borrowed
| borrow later used here, when `_w` is dropped
| borrow might be used here, when `_w` is dropped and runs the destructor for type `Wrap<&[&str]>`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -21,7 +21,7 @@ LL | }
| -
| |
| `v` dropped here while still borrowed
| borrow later used here, when `_w` is dropped
| borrow might be used here, when `_w` is dropped and runs the destructor for closure
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `c2` dropped here while still borrowed
| borrow later used here, when `c1` is dropped
| borrow might be used here, when `c1` is dropped and runs the destructor for type `C<'_>`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -22,7 +22,7 @@ LL | }
| -
| |
| `c3` dropped here while still borrowed
| borrow later used here, when `c1` is dropped
| borrow might be used here, when `c1` is dropped and runs the destructor for type `C<'_>`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -36,7 +36,7 @@ LL | }
| -
| |
| `c1` dropped here while still borrowed
| borrow later used here, when `c1` is dropped
| borrow might be used here, when `c1` is dropped and runs the destructor for type `C<'_>`
error: aborting due to 3 previous errors

View file

@ -2,17 +2,32 @@ error[E0597]: `y` does not live long enough
--> $DIR/issue-23338-locals-die-before-temps-of-body.rs:20:5
|
LL | y.borrow().clone()
| ^ borrowed value does not live long enough
| ^---------
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
LL | }
| - `y` dropped here while still borrowed
| -
| |
| `y` dropped here while still borrowed
| ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::cell::Ref<'_, std::string::String>`
|
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.
error[E0597]: `y` does not live long enough
--> $DIR/issue-23338-locals-die-before-temps-of-body.rs:27:9
|
LL | y.borrow().clone()
| ^ borrowed value does not live long enough
| ^---------
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
LL | };
| - `y` dropped here while still borrowed
| -- ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::cell::Ref<'_, std::string::String>`
| |
| `y` dropped here while still borrowed
|
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.
error: aborting due to 2 previous errors

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `d1` dropped here while still borrowed
| borrow later used here, when `_d` is dropped
| borrow might be used here, when `_d` is dropped and runs the `Drop` code for type `D_Child`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -7,7 +7,7 @@ LL | }
| -
| |
| `d1` dropped here while still borrowed
| borrow later used here, when `_d` is dropped
| borrow might be used here, when `_d` is dropped and runs the `Drop` code for type `D_HasSelfMethod`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -20,7 +20,7 @@ LL | }
| -
| |
| `d1` dropped here while still borrowed
| borrow later used here, when `_d` is dropped
| borrow might be used here, when `_d` is dropped and runs the `Drop` code for type `D_HasMethodWithSelfArg`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -33,7 +33,7 @@ LL | }
| -
| |
| `d1` dropped here while still borrowed
| borrow later used here, when `_d` is dropped
| borrow might be used here, when `_d` is dropped and runs the `Drop` code for type `D_HasType`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -7,7 +7,7 @@ LL | }
| -
| |
| `d1` dropped here while still borrowed
| borrow later used here, when `d2` is dropped
| borrow might be used here, when `d2` is dropped and runs the `Drop` code for type `D`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `container` dropped here while still borrowed
| borrow later used here, when `container` is dropped
| borrow might be used here, when `container` is dropped and runs the destructor for type `Container<'_>`
error: aborting due to previous error

View file

@ -7,7 +7,7 @@ LL | }
| -
| |
| `ticking` dropped here while still borrowed
| borrow later used here, when `zook` is dropped
| borrow might be used here, when `zook` is dropped and runs the `Drop` code for type `Zook`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -7,7 +7,7 @@ LL | }
| -
| |
| `x` dropped here while still borrowed
| borrow later used here, when `y` is dropped
| borrow might be used here, when `y` is dropped and runs the `Drop` code for type `std::sync::Arc`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -20,7 +20,7 @@ LL | }
| -
| |
| `x` dropped here while still borrowed
| borrow later used here, when `y` is dropped
| borrow might be used here, when `y` is dropped and runs the `Drop` code for type `std::rc::Rc`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| here, drop of `foo` needs exclusive access to `foo.data`, because the type `Foo<Concrete<'_>>` implements the `Drop` trait
| borrow later used here, when `foo` is dropped
| borrow might be used here, when `foo` is dropped and runs the `Drop` code for type `Foo`
|
= note: consider using a `let` binding to create a longer lived value

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `first_dropped` dropped here while still borrowed
| borrow later used here, when `foo1` is dropped
| borrow might be used here, when `foo1` is dropped and runs the `Drop` code for type `Foo`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `first_dropped` dropped here while still borrowed
| borrow later used here, when `foo1` is dropped
| borrow might be used here, when `foo1` is dropped and runs the `Drop` code for type `Foo`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `first_dropped` dropped here while still borrowed
| borrow later used here, when `foo1` is dropped
| borrow might be used here, when `foo1` is dropped and runs the `Drop` code for type `Foo`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -7,7 +7,7 @@ LL | let ss: &isize = &id(1);
LL | }
| - temporary value is freed at the end of this statement
LL | }
| - borrow later used here, when `blah` is dropped
| - borrow might be used here, when `blah` is dropped and runs the destructor for type `std::boxed::Box<dyn Foo>`
|
= note: consider using a `let` binding to create a longer lived value

View file

@ -62,7 +62,7 @@ LL | }
| - `z` dropped here while still borrowed
...
LL | }
| - borrow later used here, when `tx` is dropped
| - borrow might be used here, when `tx` is dropped and runs the `Drop` code for type `std::sync::mpsc::Sender`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `c2` dropped here while still borrowed
| borrow later used here, when `c1` is dropped
| borrow might be used here, when `c1` is dropped and runs the destructor for type `C<'_>`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -22,7 +22,7 @@ LL | }
| -
| |
| `c1` dropped here while still borrowed
| borrow later used here, when `c1` is dropped
| borrow might be used here, when `c1` is dropped and runs the destructor for type `C<'_>`
error: aborting due to 2 previous errors

View file

@ -8,7 +8,7 @@ LL | }
| -
| |
| `x` dropped here while still borrowed
| borrow later used here, when `v` is dropped
| borrow might be used here, when `v` is dropped and runs the `Drop` code for type `Bag`
|
= note: values in a scope are dropped in the opposite order they are defined
@ -22,7 +22,7 @@ LL | }
| -
| |
| `y` dropped here while still borrowed
| borrow later used here, when `v` is dropped
| borrow might be used here, when `v` is dropped and runs the `Drop` code for type `Bag`
|
= note: values in a scope are dropped in the opposite order they are defined

View file

@ -10,7 +10,7 @@ LL | }
| -
| |
| `factorial` dropped here while still borrowed
| borrow later used here, when `factorial` is dropped
| borrow might be used here, when `factorial` is dropped and runs the destructor for type `std::option::Option<std::boxed::Box<dyn std::ops::Fn(u32) -> u32>>`
error[E0506]: cannot assign to `factorial` because it is borrowed
--> $DIR/unboxed-closures-failed-recursive-fn-1.rs:30:5