Skip to content

Commit d4abc31

Browse files
authored
Rollup merge of #129627 - dingxiangfei2009:ensure-tail-expr-in-let-block-works, r=jieyouxu
Ensure that tail expr receive lifetime extension cc `@jieyouxu` `@traviscross` It just came to me that we should add a test to make sure that we honor the contract from the temporary lifetime rule #121346. We should continue to implement this rule in Edition 2021 onward and shorter tail expression lifetime should not override it. This is a small PR to improve our assurance and establish a stronger contract. Tracked by #123739
2 parents 71042b4 + aee8152 commit d4abc31

3 files changed

+21
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
("Hello", 1) [(("Hello", 1),)] "Hello" "Hello" "Hello" ("Hello", 1) ("Hello", 1) ("Hello", 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
("Hello", 1) [(("Hello", 1),)] "Hello" "Hello" "Hello" ("Hello", 1) ("Hello", 1) ("Hello", 1)

tests/ui/lifetimes/temporary-lifetime-extension.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
//@ check-pass
1+
// This is a test for the new temporary lifetime behaviour as implemented for RFC 3606.
2+
// In essence, with #3606 we can write the following variable initialisation without
3+
// a borrow checking error because the temporary lifetime is automatically extended.
4+
// ```rust
5+
// let x = if condition() {
6+
// &something()
7+
// } else {
8+
// &something_else()
9+
// };
10+
// ```
11+
// More details can be found in https://github.com/rust-lang/rfcs/pull/3606
12+
13+
//@ run-pass
14+
//@ check-run-results
15+
//@ revisions: edition2021 edition2024
16+
//@ [edition2021] edition: 2021
17+
//@ [edition2024] edition: 2024
18+
//@ [edition2024] compile-flags: -Z unstable-options
219

320
fn temp() -> (String, i32) {
421
(String::from("Hello"), 1)
@@ -13,11 +30,7 @@ fn main() {
1330
let _ = 123;
1431
&(*temp().0)[..]
1532
};
16-
let f = if true {
17-
&temp()
18-
} else {
19-
&temp()
20-
};
33+
let f = if true { &temp() } else { &temp() };
2134
let g = match true {
2235
true => &temp(),
2336
false => {

0 commit comments

Comments
 (0)