Skip to content

Commit cb80c76

Browse files
authored
Unrolled build for #141069
Rollup merge of #141069 - chenyukang:yukang-fix-137486-suggest-mut, r=davidtwco Suggest mut when possbile for temporary value dropped while borrowed Fixes #137486
2 parents 573a015 + d5d4cec commit cb80c76

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3229,8 +3229,20 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
32293229
Applicability::MaybeIncorrect,
32303230
);
32313231
}
3232+
3233+
let mutability = if matches!(borrow.kind(), BorrowKind::Mut { .. }) {
3234+
"mut "
3235+
} else {
3236+
""
3237+
};
3238+
32323239
if !is_format_arguments_item {
3233-
let addition = format!("let binding = {};\n{}", s, " ".repeat(p));
3240+
let addition = format!(
3241+
"let {}binding = {};\n{}",
3242+
mutability,
3243+
s,
3244+
" ".repeat(p)
3245+
);
32343246
err.multipart_suggestion_verbose(
32353247
msg,
32363248
vec![

tests/ui/coroutine/auto-trait-regions.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | assert_foo(a);
1111
|
1212
help: consider using a `let` binding to create a longer lived value
1313
|
14-
LL ~ let binding = true;
14+
LL ~ let mut binding = true;
1515
LL ~ let a = A(&mut binding, &mut true, No);
1616
|
1717

@@ -28,7 +28,7 @@ LL | assert_foo(a);
2828
|
2929
help: consider using a `let` binding to create a longer lived value
3030
|
31-
LL ~ let binding = true;
31+
LL ~ let mut binding = true;
3232
LL ~ let a = A(&mut true, &mut binding, No);
3333
|
3434

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ run-rustfix
2+
#![allow(unused_assignments)]
3+
4+
use std::pin::Pin;
5+
fn main() {
6+
let mut s = String::from("hello");
7+
let mut ref_s = &mut s;
8+
9+
let mut binding = String::from("world");
10+
ref_s = &mut binding; //~ ERROR temporary value dropped while borrowed [E0716]
11+
12+
print!("r1 = {}", ref_s);
13+
14+
let mut val: u8 = 5;
15+
let mut s = Pin::new(&mut val);
16+
let mut ref_s = &mut s;
17+
18+
let mut val2: u8 = 10;
19+
let mut binding = Pin::new(&mut val2);
20+
ref_s = &mut binding; //~ ERROR temporary value dropped while borrowed [E0716]
21+
22+
print!("r1 = {}", ref_s);
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ run-rustfix
2+
#![allow(unused_assignments)]
3+
4+
use std::pin::Pin;
5+
fn main() {
6+
let mut s = String::from("hello");
7+
let mut ref_s = &mut s;
8+
9+
ref_s = &mut String::from("world"); //~ ERROR temporary value dropped while borrowed [E0716]
10+
11+
print!("r1 = {}", ref_s);
12+
13+
let mut val: u8 = 5;
14+
let mut s = Pin::new(&mut val);
15+
let mut ref_s = &mut s;
16+
17+
let mut val2: u8 = 10;
18+
ref_s = &mut Pin::new(&mut val2); //~ ERROR temporary value dropped while borrowed [E0716]
19+
20+
print!("r1 = {}", ref_s);
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/sugg-mut-for-binding-issue-137486.rs:9:18
3+
|
4+
LL | ref_s = &mut String::from("world");
5+
| ^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
6+
| |
7+
| creates a temporary value which is freed while still in use
8+
LL |
9+
LL | print!("r1 = {}", ref_s);
10+
| ----- borrow later used here
11+
|
12+
help: consider using a `let` binding to create a longer lived value
13+
|
14+
LL ~ let mut binding = String::from("world");
15+
LL ~ ref_s = &mut binding;
16+
|
17+
18+
error[E0716]: temporary value dropped while borrowed
19+
--> $DIR/sugg-mut-for-binding-issue-137486.rs:18:18
20+
|
21+
LL | ref_s = &mut Pin::new(&mut val2);
22+
| ^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
23+
| |
24+
| creates a temporary value which is freed while still in use
25+
LL |
26+
LL | print!("r1 = {}", ref_s);
27+
| ----- borrow later used here
28+
|
29+
help: consider using a `let` binding to create a longer lived value
30+
|
31+
LL ~ let mut binding = Pin::new(&mut val2);
32+
LL ~ ref_s = &mut binding;
33+
|
34+
35+
error: aborting due to 2 previous errors
36+
37+
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)