Skip to content

Temporary lifetime extension through tuple struct and tuple variant constructors #140593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions compiler/rustc_hir_analysis/src/check/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -752,13 +753,19 @@ 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())`.
//
// 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
{
for arg in args {
record_rvalue_scope_if_borrow_expr(visitor, arg, blk_id);
}
}
}
_ => {}
}
Expand Down
1 change: 0 additions & 1 deletion src/tools/tidy/src/issues.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const-mut-refs/mut_ref_in_final.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions tests/ui/consts/const-mut-refs/mut_ref_in_final.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions tests/ui/consts/issue-54224.rs

This file was deleted.

23 changes: 0 additions & 23 deletions tests/ui/consts/issue-54224.stderr

This file was deleted.

3 changes: 2 additions & 1 deletion tests/ui/consts/promote-not.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 33 additions & 33 deletions tests/ui/consts/promote-not.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/coroutine/auto-trait-regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
Loading
Loading