From d91eb541f3697f7bd99958295cfc5e4217456982 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 2 May 2025 18:24:12 +0200 Subject: [PATCH 1/5] Implement temporary lifetime extension for tuple ctors. --- .../rustc_hir_analysis/src/check/region.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/region.rs b/compiler/rustc_hir_analysis/src/check/region.rs index 7cb31a64e13e6..e2f15af68c3c0 100644 --- a/compiler/rustc_hir_analysis/src/check/region.rs +++ b/compiler/rustc_hir_analysis/src/check/region.rs @@ -10,6 +10,7 @@ use std::mem; use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; +use rustc_hir::def::{CtorKind, DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{Arm, Block, Expr, LetStmt, Pat, PatKind, Stmt}; @@ -752,13 +753,16 @@ fn resolve_local<'tcx>( record_rvalue_scope_if_borrow_expr(visitor, arm.body, blk_id); } } - hir::ExprKind::Call(..) | hir::ExprKind::MethodCall(..) => { - // FIXME(@dingxiangfei2009): choose call arguments here - // for candidacy for extended parameter rule application - } - hir::ExprKind::Index(..) => { - // FIXME(@dingxiangfei2009): select the indices - // as candidate for rvalue scope rules + hir::ExprKind::Call(func, args) => { + // Recurse into tuple constructors, such as `Some(&temp())`. + if let hir::ExprKind::Path(path) = &func.kind + && let hir::QPath::Resolved(None, path) = path + && let Res::SelfCtor(_) | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) = path.res + { + for arg in args { + record_rvalue_scope_if_borrow_expr(visitor, arg, blk_id); + } + } } _ => {} } From dec02c1663c708b58231a4c170f995a95e10ddb7 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 2 May 2025 18:25:14 +0200 Subject: [PATCH 2/5] Update tests. --- src/tools/tidy/src/issues.txt | 1 - .../consts/const-mut-refs/mut_ref_in_final.rs | 2 +- .../const-mut-refs/mut_ref_in_final.stderr | 10 +-- tests/ui/consts/issue-54224.rs | 12 ---- tests/ui/consts/issue-54224.stderr | 23 ------- tests/ui/consts/promote-not.rs | 3 +- tests/ui/consts/promote-not.stderr | 66 +++++++++---------- tests/ui/coroutine/auto-trait-regions.rs | 4 +- tests/ui/coroutine/auto-trait-regions.stderr | 44 ++++++------- tests/ui/static/static-drop-scope.rs | 6 -- tests/ui/static/static-drop-scope.stderr | 30 ++------- 11 files changed, 69 insertions(+), 132 deletions(-) delete mode 100644 tests/ui/consts/issue-54224.rs delete mode 100644 tests/ui/consts/issue-54224.stderr diff --git a/src/tools/tidy/src/issues.txt b/src/tools/tidy/src/issues.txt index e6b5aa59622d2..5ec5fa4775941 100644 --- a/src/tools/tidy/src/issues.txt +++ b/src/tools/tidy/src/issues.txt @@ -760,7 +760,6 @@ ui/consts/issue-46553.rs ui/consts/issue-47789.rs ui/consts/issue-50439.rs ui/consts/issue-52023-array-size-pointer-cast.rs -ui/consts/issue-54224.rs ui/consts/issue-54348.rs ui/consts/issue-54387.rs ui/consts/issue-54582.rs diff --git a/tests/ui/consts/const-mut-refs/mut_ref_in_final.rs b/tests/ui/consts/const-mut-refs/mut_ref_in_final.rs index bc534be6832e5..e90360a33b4a2 100644 --- a/tests/ui/consts/const-mut-refs/mut_ref_in_final.rs +++ b/tests/ui/consts/const-mut-refs/mut_ref_in_final.rs @@ -18,7 +18,7 @@ const B: *mut i32 = &mut 4; //~ ERROR mutable references are not allowed const B2: Option<&mut i32> = None; // Not ok, can't prove that no mutable allocation ends up in final value -const B3: Option<&mut i32> = Some(&mut 42); //~ ERROR temporary value dropped while borrowed +const B3: Option<&mut i32> = Some(&mut 42); //~ ERROR mutable references are not allowed const fn helper(x: &mut i32) -> Option<&mut i32> { Some(x) } const B4: Option<&mut i32> = helper(&mut 42); //~ ERROR temporary value dropped while borrowed diff --git a/tests/ui/consts/const-mut-refs/mut_ref_in_final.stderr b/tests/ui/consts/const-mut-refs/mut_ref_in_final.stderr index 1f49f08c40156..8ee47b676826a 100644 --- a/tests/ui/consts/const-mut-refs/mut_ref_in_final.stderr +++ b/tests/ui/consts/const-mut-refs/mut_ref_in_final.stderr @@ -4,15 +4,11 @@ error[E0764]: mutable references are not allowed in the final value of constants LL | const B: *mut i32 = &mut 4; | ^^^^^^ -error[E0716]: temporary value dropped while borrowed - --> $DIR/mut_ref_in_final.rs:21:40 +error[E0764]: mutable references are not allowed in the final value of constants + --> $DIR/mut_ref_in_final.rs:21:35 | LL | const B3: Option<&mut i32> = Some(&mut 42); - | ----------^^- - | | | | - | | | temporary value is freed at the end of this statement - | | creates a temporary value which is freed while still in use - | using this value as a constant requires that borrow lasts for `'static` + | ^^^^^^^ error[E0716]: temporary value dropped while borrowed --> $DIR/mut_ref_in_final.rs:24:42 diff --git a/tests/ui/consts/issue-54224.rs b/tests/ui/consts/issue-54224.rs deleted file mode 100644 index f1947933d6707..0000000000000 --- a/tests/ui/consts/issue-54224.rs +++ /dev/null @@ -1,12 +0,0 @@ -const FOO: Option<&[[u8; 3]]> = Some(&[*b"foo"]); //~ ERROR temporary value dropped while borrowed - -use std::borrow::Cow; - -pub const X: [u8; 3] = *b"ABC"; -pub const Y: Cow<'static, [ [u8; 3] ]> = Cow::Borrowed(&[X]); - - -pub const Z: Cow<'static, [ [u8; 3] ]> = Cow::Borrowed(&[*b"ABC"]); -//~^ ERROR temporary value dropped while borrowed - -fn main() {} diff --git a/tests/ui/consts/issue-54224.stderr b/tests/ui/consts/issue-54224.stderr deleted file mode 100644 index 55fe55759df54..0000000000000 --- a/tests/ui/consts/issue-54224.stderr +++ /dev/null @@ -1,23 +0,0 @@ -error[E0716]: temporary value dropped while borrowed - --> $DIR/issue-54224.rs:1:39 - | -LL | const FOO: Option<&[[u8; 3]]> = Some(&[*b"foo"]); - | ------^^^^^^^^^- - | | | | - | | | temporary value is freed at the end of this statement - | | creates a temporary value which is freed while still in use - | using this value as a constant requires that borrow lasts for `'static` - -error[E0716]: temporary value dropped while borrowed - --> $DIR/issue-54224.rs:9:57 - | -LL | pub const Z: Cow<'static, [ [u8; 3] ]> = Cow::Borrowed(&[*b"ABC"]); - | ---------------^^^^^^^^^- - | | | | - | | | temporary value is freed at the end of this statement - | | creates a temporary value which is freed while still in use - | using this value as a constant requires that borrow lasts for `'static` - -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0716`. diff --git a/tests/ui/consts/promote-not.rs b/tests/ui/consts/promote-not.rs index 207baccd6abb5..74e0efb858aa0 100644 --- a/tests/ui/consts/promote-not.rs +++ b/tests/ui/consts/promote-not.rs @@ -3,10 +3,11 @@ #![allow(unconditional_panic)] use std::cell::Cell; +use std::convert::identity; use std::mem::ManuallyDrop; // We do not promote mutable references. -static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]); //~ ERROR temporary value dropped while borrowed +static mut TEST1: &mut [i32] = identity(&mut [1, 2, 3]); //~ ERROR temporary value dropped while borrowed static mut TEST2: &'static mut [i32] = { let x = &mut [1,2,3]; //~ ERROR temporary value dropped while borrowed diff --git a/tests/ui/consts/promote-not.stderr b/tests/ui/consts/promote-not.stderr index d8b6091dc9aab..ec552d9dd7d48 100644 --- a/tests/ui/consts/promote-not.stderr +++ b/tests/ui/consts/promote-not.stderr @@ -1,15 +1,15 @@ error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:9:50 + --> $DIR/promote-not.rs:10:46 | -LL | static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]); - | ----------^^^^^^^^^- - | | | | - | | | temporary value is freed at the end of this statement - | | creates a temporary value which is freed while still in use - | using this value as a static requires that borrow lasts for `'static` +LL | static mut TEST1: &mut [i32] = identity(&mut [1, 2, 3]); + | --------------^^^^^^^^^- + | | | | + | | | temporary value is freed at the end of this statement + | | creates a temporary value which is freed while still in use + | using this value as a static requires that borrow lasts for `'static` error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:12:18 + --> $DIR/promote-not.rs:13:18 | LL | let x = &mut [1,2,3]; | ^^^^^^^ creates a temporary value which is freed while still in use @@ -19,7 +19,7 @@ LL | }; | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:34:29 + --> $DIR/promote-not.rs:35:29 | LL | let _x: &'static i32 = &unsafe { U { x: 0 }.x }; | ------------ ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -29,7 +29,7 @@ LL | }; | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:40:29 + --> $DIR/promote-not.rs:41:29 | LL | let _val: &'static _ = &(Cell::new(1), 2).1; | ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -39,7 +39,7 @@ LL | }; | - temporary value is freed at the end of this statement error[E0493]: destructor of `String` cannot be evaluated at compile-time - --> $DIR/promote-not.rs:47:14 + --> $DIR/promote-not.rs:48:14 | LL | let x = &String::new(); | ^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constants @@ -48,7 +48,7 @@ LL | }; | - value is dropped here error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:59:33 + --> $DIR/promote-not.rs:60:33 | LL | let _x: &'static u32 = &mk_panic(); | ------------ ^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -58,7 +58,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:21:32 + --> $DIR/promote-not.rs:22:32 | LL | let _x: &'static () = &foo(); | ----------- ^^^^^ creates a temporary value which is freed while still in use @@ -68,7 +68,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:29:29 + --> $DIR/promote-not.rs:30:29 | LL | let _x: &'static i32 = &unsafe { U { x: 0 }.x }; | ------------ ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -78,7 +78,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:65:29 + --> $DIR/promote-not.rs:66:29 | LL | let _val: &'static _ = &(Cell::new(1), 2).0; | ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -89,7 +89,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:66:29 + --> $DIR/promote-not.rs:67:29 | LL | let _val: &'static _ = &(Cell::new(1), 2).1; | ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -100,7 +100,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:69:29 + --> $DIR/promote-not.rs:70:29 | LL | let _val: &'static _ = &(1/0); | ---------- ^^^^^ creates a temporary value which is freed while still in use @@ -111,7 +111,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:70:29 + --> $DIR/promote-not.rs:71:29 | LL | let _val: &'static _ = &(1/(1-1)); | ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use @@ -122,7 +122,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:71:29 + --> $DIR/promote-not.rs:72:29 | LL | let _val: &'static _ = &((1+1)/(1-1)); | ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -133,7 +133,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:72:29 + --> $DIR/promote-not.rs:73:29 | LL | let _val: &'static _ = &(i32::MIN/-1); | ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -144,7 +144,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:73:29 + --> $DIR/promote-not.rs:74:29 | LL | let _val: &'static _ = &(i32::MIN/(0-1)); | ---------- ^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -155,7 +155,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:74:29 + --> $DIR/promote-not.rs:75:29 | LL | let _val: &'static _ = &(-128i8/-1); | ---------- ^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -166,7 +166,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:75:29 + --> $DIR/promote-not.rs:76:29 | LL | let _val: &'static _ = &(1%0); | ---------- ^^^^^ creates a temporary value which is freed while still in use @@ -177,7 +177,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:76:29 + --> $DIR/promote-not.rs:77:29 | LL | let _val: &'static _ = &(1%(1-1)); | ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use @@ -188,7 +188,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:77:29 + --> $DIR/promote-not.rs:78:29 | LL | let _val: &'static _ = &([1,2,3][4]+1); | ---------- ^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -199,7 +199,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:81:29 + --> $DIR/promote-not.rs:82:29 | LL | let _val: &'static _ = &TEST_DROP; | ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use @@ -210,7 +210,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:83:29 + --> $DIR/promote-not.rs:84:29 | LL | let _val: &'static _ = &&TEST_DROP; | ---------- ^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -221,7 +221,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:83:30 + --> $DIR/promote-not.rs:84:30 | LL | let _val: &'static _ = &&TEST_DROP; | ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use @@ -232,7 +232,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:86:29 + --> $DIR/promote-not.rs:87:29 | LL | let _val: &'static _ = &(&TEST_DROP,); | ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -243,7 +243,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:86:31 + --> $DIR/promote-not.rs:87:31 | LL | let _val: &'static _ = &(&TEST_DROP,); | ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use @@ -254,7 +254,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:89:29 + --> $DIR/promote-not.rs:90:29 | LL | let _val: &'static _ = &[&TEST_DROP; 1]; | ---------- ^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use @@ -265,7 +265,7 @@ LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:89:31 + --> $DIR/promote-not.rs:90:31 | LL | let _val: &'static _ = &[&TEST_DROP; 1]; | ---------- ^^^^^^^^^ - temporary value is freed at the end of this statement @@ -274,7 +274,7 @@ LL | let _val: &'static _ = &[&TEST_DROP; 1]; | type annotation requires that borrow lasts for `'static` error[E0716]: temporary value dropped while borrowed - --> $DIR/promote-not.rs:98:26 + --> $DIR/promote-not.rs:99:26 | LL | let x: &'static _ = &UnionWithCell { f1: 0 }; | ---------- ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use diff --git a/tests/ui/coroutine/auto-trait-regions.rs b/tests/ui/coroutine/auto-trait-regions.rs index 1c7f0304ddbec..f115896a473cd 100644 --- a/tests/ui/coroutine/auto-trait-regions.rs +++ b/tests/ui/coroutine/auto-trait-regions.rs @@ -43,8 +43,8 @@ fn main() { // Disallow impls which relates lifetimes in the coroutine interior let gen = #[coroutine] move || { let a = A(&mut true, &mut true, No); - //~^ ERROR temporary value dropped while borrowed - //~| ERROR temporary value dropped while borrowed + //~^ ERROR borrow may still be in use when coroutine yields + //~| ERROR borrow may still be in use when coroutine yields yield; assert_foo(a); }; diff --git a/tests/ui/coroutine/auto-trait-regions.stderr b/tests/ui/coroutine/auto-trait-regions.stderr index a9a0bde2ba019..77b5f3ce57c4a 100644 --- a/tests/ui/coroutine/auto-trait-regions.stderr +++ b/tests/ui/coroutine/auto-trait-regions.stderr @@ -1,36 +1,34 @@ -error[E0716]: temporary value dropped while borrowed - --> $DIR/auto-trait-regions.rs:45:24 +error[E0626]: borrow may still be in use when coroutine yields + --> $DIR/auto-trait-regions.rs:45:19 | +LL | let gen = #[coroutine] move || { + | ------- within this coroutine LL | let a = A(&mut true, &mut true, No); - | ^^^^ - temporary value is freed at the end of this statement - | | - | creates a temporary value which is freed while still in use + | ^^^^^^^^^ ... -LL | assert_foo(a); - | - borrow later used here +LL | yield; + | ----- possible yield occurs here | -help: consider using a `let` binding to create a longer lived value - | -LL ~ let binding = true; -LL ~ let a = A(&mut binding, &mut true, No); +help: add `static` to mark this coroutine as unmovable | +LL | let gen = #[coroutine] static move || { + | ++++++ -error[E0716]: temporary value dropped while borrowed - --> $DIR/auto-trait-regions.rs:45:35 +error[E0626]: borrow may still be in use when coroutine yields + --> $DIR/auto-trait-regions.rs:45:30 | +LL | let gen = #[coroutine] move || { + | ------- within this coroutine LL | let a = A(&mut true, &mut true, No); - | ^^^^ - temporary value is freed at the end of this statement - | | - | creates a temporary value which is freed while still in use + | ^^^^^^^^^ ... -LL | assert_foo(a); - | - borrow later used here - | -help: consider using a `let` binding to create a longer lived value +LL | yield; + | ----- possible yield occurs here | -LL ~ let binding = true; -LL ~ let a = A(&mut true, &mut binding, No); +help: add `static` to mark this coroutine as unmovable | +LL | let gen = #[coroutine] static move || { + | ++++++ error: implementation of `Foo` is not general enough --> $DIR/auto-trait-regions.rs:31:5 @@ -52,4 +50,4 @@ LL | assert_foo(gen); error: aborting due to 4 previous errors -For more information about this error, try `rustc --explain E0716`. +For more information about this error, try `rustc --explain E0626`. diff --git a/tests/ui/static/static-drop-scope.rs b/tests/ui/static/static-drop-scope.rs index 74b224c9be085..ddcabeb12cc74 100644 --- a/tests/ui/static/static-drop-scope.rs +++ b/tests/ui/static/static-drop-scope.rs @@ -4,12 +4,6 @@ impl Drop for WithDtor { fn drop(&mut self) {} } -static PROMOTION_FAIL_S: Option<&'static WithDtor> = Some(&WithDtor); -//~^ ERROR destructor of - -const PROMOTION_FAIL_C: Option<&'static WithDtor> = Some(&WithDtor); -//~^ ERROR destructor of - static EARLY_DROP_S: i32 = (WithDtor, 0).1; //~^ ERROR destructor of diff --git a/tests/ui/static/static-drop-scope.stderr b/tests/ui/static/static-drop-scope.stderr index 24658bc601e7b..0fdf081e23432 100644 --- a/tests/ui/static/static-drop-scope.stderr +++ b/tests/ui/static/static-drop-scope.stderr @@ -1,21 +1,5 @@ -error[E0493]: destructor of `WithDtor` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:7:60 - | -LL | static PROMOTION_FAIL_S: Option<&'static WithDtor> = Some(&WithDtor); - | ^^^^^^^^- value is dropped here - | | - | the destructor for this type cannot be evaluated in statics - -error[E0493]: destructor of `WithDtor` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:10:59 - | -LL | const PROMOTION_FAIL_C: Option<&'static WithDtor> = Some(&WithDtor); - | ^^^^^^^^- value is dropped here - | | - | the destructor for this type cannot be evaluated in constants - error[E0493]: destructor of `(WithDtor, i32)` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:13:28 + --> $DIR/static-drop-scope.rs:7:28 | LL | static EARLY_DROP_S: i32 = (WithDtor, 0).1; | ^^^^^^^^^^^^^ - value is dropped here @@ -23,7 +7,7 @@ LL | static EARLY_DROP_S: i32 = (WithDtor, 0).1; | the destructor for this type cannot be evaluated in statics error[E0493]: destructor of `(WithDtor, i32)` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:16:27 + --> $DIR/static-drop-scope.rs:10:27 | LL | const EARLY_DROP_C: i32 = (WithDtor, 0).1; | ^^^^^^^^^^^^^ - value is dropped here @@ -31,7 +15,7 @@ LL | const EARLY_DROP_C: i32 = (WithDtor, 0).1; | the destructor for this type cannot be evaluated in constants error[E0493]: destructor of `(Option, i32)` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:27:34 + --> $DIR/static-drop-scope.rs:21:34 | LL | const EARLY_DROP_C_OPTION: i32 = (Some(WithDtor), 0).1; | ^^^^^^^^^^^^^^^^^^^ - value is dropped here @@ -39,7 +23,7 @@ LL | const EARLY_DROP_C_OPTION: i32 = (Some(WithDtor), 0).1; | the destructor for this type cannot be evaluated in constants error[E0493]: destructor of `(Option, i32)` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:32:43 + --> $DIR/static-drop-scope.rs:26:43 | LL | const EARLY_DROP_C_OPTION_CONSTANT: i32 = (HELPER, 0).1; | ^^^^^^^^^^^ - value is dropped here @@ -47,7 +31,7 @@ LL | const EARLY_DROP_C_OPTION_CONSTANT: i32 = (HELPER, 0).1; | the destructor for this type cannot be evaluated in constants error[E0493]: destructor of `T` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:19:24 + --> $DIR/static-drop-scope.rs:13:24 | LL | const fn const_drop(_: T) {} | ^ - value is dropped here @@ -55,7 +39,7 @@ LL | const fn const_drop(_: T) {} | the destructor for this type cannot be evaluated in constant functions error[E0493]: destructor of `(T, ())` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:23:5 + --> $DIR/static-drop-scope.rs:17:5 | LL | (x, ()).1 | ^^^^^^^ the destructor for this type cannot be evaluated in constant functions @@ -63,6 +47,6 @@ LL | LL | } | - value is dropped here -error: aborting due to 8 previous errors +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0493`. From 7c8251f16aa0346c31c11efa2c6b7ca8c47ce352 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 2 May 2025 23:20:14 +0200 Subject: [PATCH 3/5] Add comment. --- compiler/rustc_hir_analysis/src/check/region.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/rustc_hir_analysis/src/check/region.rs b/compiler/rustc_hir_analysis/src/check/region.rs index e2f15af68c3c0..c458878da15b5 100644 --- a/compiler/rustc_hir_analysis/src/check/region.rs +++ b/compiler/rustc_hir_analysis/src/check/region.rs @@ -755,6 +755,9 @@ fn resolve_local<'tcx>( } hir::ExprKind::Call(func, args) => { // Recurse into tuple constructors, such as `Some(&temp())`. + // + // That way, there is no difference between `Some(..)` and `Some { 0: .. }`, + // even though the former is syntactically a function call. if let hir::ExprKind::Path(path) = &func.kind && let hir::QPath::Resolved(None, path) = path && let Res::SelfCtor(_) | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) = path.res From 2dce0dc54cbb547e23aed43eff75477aedeee936 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sat, 3 May 2025 08:42:41 +0200 Subject: [PATCH 4/5] Add tests. --- ...temporary-lifetime-extension-tuple-ctor.rs | 25 +++++++++++++++++++ ...orary-lifetime-extension-tuple-ctor.stderr | 19 ++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs create mode 100644 tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr diff --git a/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs new file mode 100644 index 0000000000000..cf60991e51910 --- /dev/null +++ b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs @@ -0,0 +1,25 @@ +//@ edition:2024 + +fn temp() -> String { + String::from("Hello") +} + +#[derive(Debug)] +struct X<'a>(&'a String); + +fn main() { + let a = &temp(); + let b = Some(&temp()); + let c = Option::Some::<&String>(&temp()); + use Option::Some as S; + let d = S(&temp()); + let e = X(&temp()); + let f = Some(Ok::<_, ()>(std::borrow::Cow::Borrowed(if true { + &temp() + } else { + panic!() + }))); + let some = Some; // Turn the ctor into a regular function. + let g = some(&temp()); //~ERROR temporary value dropped while borrowe + println!("{a:?} {b:?} {c:?} {d:?} {e:?} {f:?} {g:?}"); +} diff --git a/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr new file mode 100644 index 0000000000000..8a4b7e8ae43b5 --- /dev/null +++ b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr @@ -0,0 +1,19 @@ +error[E0716]: temporary value dropped while borrowed + --> $DIR/temporary-lifetime-extension-tuple-ctor.rs:23:19 + | +LL | let g = some(&temp()); + | ^^^^^^ - temporary value is freed at the end of this statement + | | + | creates a temporary value which is freed while still in use +LL | println!("{a:?} {b:?} {c:?} {d:?} {e:?} {f:?} {g:?}"); + | ----- borrow later used here + | +help: consider using a `let` binding to create a longer lived value + | +LL ~ let binding = temp(); +LL ~ let g = some(&binding); + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0716`. From c57879cb7c3a0f52fa2ef0e984619c4f8a15bb83 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 7 May 2025 14:29:23 +0200 Subject: [PATCH 5/5] Add test for temporary lifetime extension in `Self()` syntax. --- .../temporary-lifetime-extension-tuple-ctor.rs | 11 +++++++++++ .../temporary-lifetime-extension-tuple-ctor.stderr | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs index cf60991e51910..bb537f855a4ab 100644 --- a/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs +++ b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.rs @@ -7,6 +7,17 @@ fn temp() -> String { #[derive(Debug)] struct X<'a>(&'a String); +trait T<'a> { + const A: X<'a>; + const B: X<'a>; +} + +impl<'a> T<'a> for X<'a> { + // Check both Self() and X() syntax: + const A: X<'a> = Self(&String::new()); + const B: X<'a> = X(&String::new()); +} + fn main() { let a = &temp(); let b = Some(&temp()); diff --git a/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr index 8a4b7e8ae43b5..66f9140f63cff 100644 --- a/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr +++ b/tests/ui/lifetimes/temporary-lifetime-extension-tuple-ctor.stderr @@ -1,5 +1,5 @@ error[E0716]: temporary value dropped while borrowed - --> $DIR/temporary-lifetime-extension-tuple-ctor.rs:23:19 + --> $DIR/temporary-lifetime-extension-tuple-ctor.rs:34:19 | LL | let g = some(&temp()); | ^^^^^^ - temporary value is freed at the end of this statement