forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#121346 - m-ou-se:temp-lifetime-if-else-match,…
… r=compiler-errors Propagate temporary lifetime extension into if and match. This PR makes this work: ```rust let a = if true { ..; &temp() // used to error, but now gets lifetime extended } else { ..; &temp() // used to error, but now gets lifetime extended }; ``` and ```rust let a = match () { _ => { ..; &temp() // used to error, but now gets lifetime extended } }; ``` to make it consistent with: ```rust let a = { ..; &temp() // lifetime is extended }; ``` This is one small part of [the temporary lifetimes work](rust-lang/lang-team#253). This part is backwards compatible (so doesn't need be edition-gated), because all code affected by this change previously resulted in a hard error.
- Loading branch information
Showing
4 changed files
with
119 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,69 @@ | ||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/let_underscore_temporary.rs:10:14 | ||
error[E0597]: `a` does not live long enough | ||
--> $DIR/let_underscore_temporary.rs:11:9 | ||
| | ||
LL | let _ = if let Some(s) = &mut num { | ||
| _____________- | ||
LL | | *s += 1; | ||
LL | | s | ||
LL | | } else { | ||
LL | | &mut 0 | ||
| | ^ creates a temporary value which is freed while still in use | ||
LL | | | ||
LL | | }; | ||
| | - | ||
| | | | ||
| |_____temporary value is freed at the end of this statement | ||
| borrow later used here | ||
| | ||
= note: consider using a `let` binding to create a longer lived value | ||
LL | let a = 0; | ||
| - binding `a` declared here | ||
LL | &a | ||
| ^^ borrowed value does not live long enough | ||
LL | | ||
LL | }; | ||
| - `a` dropped here while still borrowed | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/let_underscore_temporary.rs:24:14 | ||
| | ||
LL | let _ = if let Some(ref mut s) = num { | ||
| _____________- | ||
LL | | *s += 1; | ||
LL | | s | ||
LL | | } else { | ||
LL | | &mut 0 | ||
| | ^ creates a temporary value which is freed while still in use | ||
LL | | | ||
LL | | }; | ||
| | - | ||
| | | | ||
| |_____temporary value is freed at the end of this statement | ||
| borrow later used here | ||
error[E0597]: `a` does not live long enough | ||
--> $DIR/let_underscore_temporary.rs:26:9 | ||
| | ||
= note: consider using a `let` binding to create a longer lived value | ||
LL | let a = 0; | ||
| - binding `a` declared here | ||
LL | &a | ||
| ^^ borrowed value does not live long enough | ||
LL | | ||
LL | }; | ||
| - `a` dropped here while still borrowed | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/let_underscore_temporary.rs:36:14 | ||
| | ||
LL | let _: _ = if let Some(s) = &mut num { | ||
| ________________- | ||
LL | | *s += 1; | ||
LL | | s | ||
LL | | } else { | ||
LL | | &mut 0 | ||
| | ^ creates a temporary value which is freed while still in use | ||
LL | | | ||
LL | | }; | ||
| | - | ||
| | | | ||
| |_____temporary value is freed at the end of this statement | ||
| borrow later used here | ||
error[E0597]: `a` does not live long enough | ||
--> $DIR/let_underscore_temporary.rs:39:9 | ||
| | ||
= note: consider using a `let` binding to create a longer lived value | ||
LL | let a = 0; | ||
| - binding `a` declared here | ||
LL | &a | ||
| ^^ borrowed value does not live long enough | ||
LL | | ||
LL | }; | ||
| - `a` dropped here while still borrowed | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/let_underscore_temporary.rs:50:14 | ||
error[E0597]: `a` does not live long enough | ||
--> $DIR/let_underscore_temporary.rs:54:9 | ||
| | ||
LL | let _: _ = if let Some(ref mut s) = num { | ||
| ________________- | ||
LL | | *s += 1; | ||
LL | | s | ||
LL | | } else { | ||
LL | | &mut 0 | ||
| | ^ creates a temporary value which is freed while still in use | ||
LL | | | ||
LL | | }; | ||
| | - | ||
| | | | ||
| |_____temporary value is freed at the end of this statement | ||
| borrow later used here | ||
| | ||
= note: consider using a `let` binding to create a longer lived value | ||
LL | let a = 0; | ||
| - binding `a` declared here | ||
LL | &a | ||
| ^^ borrowed value does not live long enough | ||
LL | | ||
LL | }; | ||
| - `a` dropped here while still borrowed | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/let_underscore_temporary.rs:66:14 | ||
| | ||
LL | match if let Some(s) = &mut num { | ||
| ___________- | ||
LL | | *s += 1; | ||
LL | | s | ||
LL | | } else { | ||
LL | | &mut 0 | ||
| | ^ creates a temporary value which is freed while still in use | ||
LL | | | ||
LL | | } { | ||
| | - | ||
| | | | ||
| |_____temporary value is freed at the end of this statement | ||
| borrow later used here | ||
error[E0597]: `a` does not live long enough | ||
--> $DIR/let_underscore_temporary.rs:71:9 | ||
| | ||
= note: consider using a `let` binding to create a longer lived value | ||
LL | let a = 0; | ||
| - binding `a` declared here | ||
LL | &a | ||
| ^^ borrowed value does not live long enough | ||
LL | | ||
LL | } { | ||
| - `a` dropped here while still borrowed | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/let_underscore_temporary.rs:86:14 | ||
| | ||
LL | match if let Some(ref mut s) = num { | ||
| ___________- | ||
LL | | *s += 1; | ||
LL | | s | ||
LL | | } else { | ||
LL | | &mut 0 | ||
| | ^ creates a temporary value which is freed while still in use | ||
LL | | | ||
LL | | } { | ||
| | - | ||
| | | | ||
| |_____temporary value is freed at the end of this statement | ||
| borrow later used here | ||
error[E0597]: `a` does not live long enough | ||
--> $DIR/let_underscore_temporary.rs:92:9 | ||
| | ||
= note: consider using a `let` binding to create a longer lived value | ||
LL | let a = 0; | ||
| - binding `a` declared here | ||
LL | &a | ||
| ^^ borrowed value does not live long enough | ||
LL | | ||
LL | } { | ||
| - `a` dropped here while still borrowed | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0716`. | ||
For more information about this error, try `rustc --explain E0597`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//@ check-pass | ||
|
||
fn temp() -> (String, i32) { | ||
(String::from("Hello"), 1) | ||
} | ||
|
||
fn main() { | ||
let a = &temp(); | ||
let b = [(&temp(),)]; | ||
let c = &temp().0; | ||
let d = &temp().0[..]; | ||
let e = { | ||
let _ = 123; | ||
&(*temp().0)[..] | ||
}; | ||
let f = if true { | ||
&temp() | ||
} else { | ||
&temp() | ||
}; | ||
let g = match true { | ||
true => &temp(), | ||
false => { | ||
let _ = 123; | ||
&temp() | ||
} | ||
}; | ||
let h = match temp() { | ||
// The {} moves the value, making a new temporary. | ||
owned_non_temporary => &{ owned_non_temporary }, | ||
}; | ||
println!("{a:?} {b:?} {c:?} {d:?} {e:?} {f:?} {g:?} {h:?}"); | ||
} |