Skip to content
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
373 changes: 205 additions & 168 deletions compiler/rustc_hir_analysis/src/check/region.rs

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions compiler/rustc_middle/src/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable};
use rustc_span::{DUMMY_SP, Span};
use tracing::debug;

use crate::thir::TempLifetime;
use crate::ty::{self, TyCtxt};

/// Represents a statically-describable scope that can be used to
Expand Down Expand Up @@ -226,7 +227,7 @@ pub struct ScopeTree {
/// `rustc_hir_analysis::check::region` and in the [Reference].
///
/// [Reference]: https://doc.rust-lang.org/nightly/reference/destructors.html#temporary-lifetime-extension
extended_temp_scopes: ItemLocalMap<Option<Scope>>,
extended_temp_scopes: ItemLocalMap<TempLifetime>,

/// Backwards incompatible scoping that will be introduced in future editions.
/// This information is used later for linting to identify locals and
Expand All @@ -251,12 +252,13 @@ impl ScopeTree {
}

/// Make an association between a sub-expression and an extended lifetime
pub fn record_extended_temp_scope(&mut self, var: hir::ItemLocalId, lifetime: Option<Scope>) {
pub fn record_extended_temp_scope(&mut self, var: hir::ItemLocalId, lifetime: TempLifetime) {
debug!(?var, ?lifetime);
if let Some(lifetime) = lifetime {
if let Some(lifetime) = lifetime.temp_lifetime {
assert!(var != lifetime.local_id);
}
self.extended_temp_scopes.insert(var, lifetime);
let old_lifetime = self.extended_temp_scopes.insert(var, lifetime);
assert!(old_lifetime.is_none_or(|old| old == lifetime));
}

/// Returns the narrowest scope that encloses `id`, if any.
Expand Down Expand Up @@ -332,16 +334,16 @@ impl ScopeTree {
/// Returns the scope when the temp created by `expr_id` will be cleaned up.
/// It also emits a lint on potential backwards incompatible change to the temporary scope
/// which is *for now* always shortening.
pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> (Option<Scope>, Option<Scope>) {
pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> TempLifetime {
// Check for a designated extended temporary scope.
if let Some(&s) = self.extended_temp_scopes.get(&expr_id) {
debug!("temporary_scope({expr_id:?}) = {s:?} [custom]");
return (s, None);
return s;
}

// Otherwise, locate the innermost terminating scope.
let (scope, backward_incompatible) =
let (scope, backwards_incompatible) =
self.default_temporary_scope(Scope { local_id: expr_id, data: ScopeData::Node });
(Some(scope), backward_incompatible)
TempLifetime { temp_lifetime: Some(scope), backwards_incompatible }
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub struct Expr<'tcx> {
}

/// Temporary lifetime information for THIR expressions
#[derive(Clone, Copy, Debug, HashStable)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, HashStable)]
pub struct TempLifetime {
/// Lifetime for temporaries as expected.
/// This should be `None` in a constant context.
Expand Down
79 changes: 24 additions & 55 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
let tcx = self.tcx;
let expr_ty = self.typeck_results.expr_ty(expr);
let (temp_lifetime, backwards_incompatible) =
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);

let kind = match expr.kind {
// Here comes the interesting stuff:
Expand Down Expand Up @@ -372,7 +371,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
let arg_tys = args.iter().map(|e| self.typeck_results.expr_ty_adjusted(e));
let tupled_args = Expr {
ty: Ty::new_tup_from_iter(tcx, arg_tys),
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime,
span: expr.span,
kind: ExprKind::Tuple { fields: self.mirror_exprs(args) },
};
Expand All @@ -398,7 +397,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
}
let value = &args[0];
return Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime,
ty: expr_ty,
span: expr.span,
kind: ExprKind::Box { value: self.mirror_expr(value) },
Expand Down Expand Up @@ -502,17 +501,17 @@ impl<'tcx> ThirBuildCx<'tcx> {
expr: Some(arg),
safety_mode: BlockSafety::Safe,
});
let (temp_lifetime, backwards_incompatible) =
self.region_scope_tree.temporary_scope(arg_expr.hir_id.local_id);
arg = self.thir.exprs.push(Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime: self
.region_scope_tree
.temporary_scope(arg_expr.hir_id.local_id),
ty: arg_ty,
span: arg_expr.span,
kind: ExprKind::Block { block },
});
}
let expr = self.thir.exprs.push(Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime,
ty,
span: expr.span,
kind: ExprKind::Borrow { borrow_kind: mutbl.to_borrow_kind(), arg },
Expand Down Expand Up @@ -995,12 +994,10 @@ impl<'tcx> ThirBuildCx<'tcx> {
}
} else {
let block_ty = self.typeck_results.node_type(body.hir_id);
let (temp_lifetime, backwards_incompatible) =
self.region_scope_tree.temporary_scope(body.hir_id.local_id);
let block = self.mirror_block(body);
let body = self.thir.exprs.push(Expr {
ty: block_ty,
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime: self.region_scope_tree.temporary_scope(body.hir_id.local_id),
span: self.thir[block].span,
kind: ExprKind::Block { block },
});
Expand All @@ -1022,17 +1019,13 @@ impl<'tcx> ThirBuildCx<'tcx> {
expr, cast_ty.hir_id, user_ty,
);

let cast = self.mirror_expr_cast(
source,
TempLifetime { temp_lifetime, backwards_incompatible },
expr.span,
);
let cast = self.mirror_expr_cast(source, temp_lifetime, expr.span);

if let Some(user_ty) = user_ty {
// NOTE: Creating a new Expr and wrapping a Cast inside of it may be
// inefficient, revisit this when performance becomes an issue.
let cast_expr = self.thir.exprs.push(Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime,
ty: expr_ty,
span: expr.span,
kind: cast,
Expand Down Expand Up @@ -1091,12 +1084,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
hir::ExprKind::Err(_) => unreachable!("cannot lower a `hir::ExprKind::Err` to THIR"),
};

Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
ty: expr_ty,
span: expr.span,
kind,
}
Expr { temp_lifetime, ty: expr_ty, span: expr.span, kind }
}

fn user_args_applied_to_res(
Expand Down Expand Up @@ -1140,8 +1128,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
span: Span,
overloaded_callee: Option<Ty<'tcx>>,
) -> Expr<'tcx> {
let (temp_lifetime, backwards_incompatible) =
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let (ty, user_ty) = match overloaded_callee {
Some(fn_def) => (fn_def, None),
None => {
Expand All @@ -1157,12 +1144,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
)
}
};
Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
ty,
span,
kind: ExprKind::ZstLiteral { user_ty },
}
Expr { temp_lifetime, ty, span, kind: ExprKind::ZstLiteral { user_ty } }
}

fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
Expand Down Expand Up @@ -1235,21 +1217,15 @@ impl<'tcx> ThirBuildCx<'tcx> {
Res::Def(DefKind::Static { .. }, id) => {
// this is &raw for extern static or static mut, and & for other statics
let ty = self.tcx.static_ptr_ty(id, self.typing_env);
let (temp_lifetime, backwards_incompatible) =
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let kind = if self.tcx.is_thread_local_static(id) {
ExprKind::ThreadLocalRef(id)
} else {
let alloc_id = self.tcx.reserve_and_set_static_alloc(id);
ExprKind::StaticRef { alloc_id, ty, def_id: id }
};
ExprKind::Deref {
arg: self.thir.exprs.push(Expr {
ty,
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
span: expr.span,
kind,
}),
arg: self.thir.exprs.push(Expr { ty, temp_lifetime, span: expr.span, kind }),
}
}

Expand Down Expand Up @@ -1317,13 +1293,12 @@ impl<'tcx> ThirBuildCx<'tcx> {

// construct the complete expression `foo()` for the overloaded call,
// which will yield the &T type
let (temp_lifetime, backwards_incompatible) =
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let fun = self.method_callee(expr, span, overloaded_callee);
let fun = self.thir.exprs.push(fun);
let fun_ty = self.thir[fun].ty;
let ref_expr = self.thir.exprs.push(Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime,
ty: ref_ty,
span,
kind: ExprKind::Call { ty: fun_ty, fun, args, from_hir_call: false, fn_span: span },
Expand All @@ -1338,8 +1313,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
closure_expr: &'tcx hir::Expr<'tcx>,
place: HirPlace<'tcx>,
) -> Expr<'tcx> {
let (temp_lifetime, backwards_incompatible) =
self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
let var_ty = place.base_ty;

// The result of capture analysis in `rustc_hir_typeck/src/upvar.rs` represents a captured path
Expand All @@ -1353,7 +1327,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
};

let mut captured_place_expr = Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime,
ty: var_ty,
span: closure_expr.span,
kind: self.convert_var(var_hir_id),
Expand Down Expand Up @@ -1381,12 +1355,8 @@ impl<'tcx> ThirBuildCx<'tcx> {
}
};

captured_place_expr = Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
ty: proj.ty,
span: closure_expr.span,
kind,
};
captured_place_expr =
Expr { temp_lifetime, ty: proj.ty, span: closure_expr.span, kind };
}

captured_place_expr
Expand All @@ -1401,8 +1371,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
let upvar_capture = captured_place.info.capture_kind;
let captured_place_expr =
self.convert_captured_hir_place(closure_expr, captured_place.place.clone());
let (temp_lifetime, backwards_incompatible) =
self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);

match upvar_capture {
ty::UpvarCapture::ByValue => captured_place_expr,
Expand All @@ -1411,7 +1380,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
let expr_id = self.thir.exprs.push(captured_place_expr);

Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime,
ty: upvar_ty,
span: closure_expr.span,
kind: ExprKind::ByUse { expr: expr_id, span },
Expand All @@ -1428,7 +1397,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
}
};
Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
temp_lifetime,
ty: upvar_ty,
span: closure_expr.span,
kind: ExprKind::Borrow {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ fn slice_index_range(_1: &[u32], _2: std::ops::Range<usize>) -> &[u32] {
StorageDead(_10);
StorageDead(_9);
_0 = &(*_12);
StorageDead(_12);
StorageDead(_8);
StorageDead(_12);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ fn slice_index_range(_1: &[u32], _2: std::ops::Range<usize>) -> &[u32] {
StorageDead(_10);
StorageDead(_9);
_0 = &(*_12);
StorageDead(_12);
StorageDead(_8);
StorageDead(_12);
Comment on lines -69 to +70
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be inlined from

if let Some(new_len) = usize::checked_sub(self.end, self.start)
&& self.end <= slice.len()
{
// SAFETY: `self` is checked to be valid and in bounds above.
unsafe { &*get_offset_len_noubcheck(slice, self.start, new_len) }
} else {
slice_index_fail(self.start, self.end, slice.len())
}

where previously the storage for the slice pointer returned by get_offset_len_noubcheck was dropped before the result of the bounds check1 and now it's dropped after, since it now lives past the execution of the if expression.

Footnotes

  1. See here and here for why that bool lives until then: individual boolean conditions have their scopes overridden so they live to the end of the if expression. I have to assume this is so they can each can have a single StorageDead, rather than one in each branch following each condition's switch terminator.

return;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/ui/borrowck/format-args-temporary-scopes.e2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/format-args-temporary-scopes.rs:33:64
|
LL | println!("{:?}{:?}", (), if true { std::convert::identity(&format!("")) } else { "" });
| ----------------------------------^^^^^^^^^^^---------------
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary value which is freed while still in use
| borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0716`.
39 changes: 26 additions & 13 deletions tests/ui/borrowck/format-args-temporary-scopes.e2024.stderr
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/format-args-temporary-scopes.rs:13:25
--> $DIR/format-args-temporary-scopes.rs:17:48
|
LL | println!("{:?}", { &temp() });
| ---^^^^^---
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary value which is freed while still in use
LL | println!("{:?}", { std::convert::identity(&temp()) });
| --------------------------^^^^^^---
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary value which is freed while still in use
| borrow later used here
|
= note: consider using a `let` binding to create a longer lived value

error[E0716]: temporary value dropped while borrowed
--> $DIR/format-args-temporary-scopes.rs:19:29
--> $DIR/format-args-temporary-scopes.rs:24:52
|
LL | println!("{:?}{:?}", { &temp() }, ());
| ---^^^^^---
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary value which is freed while still in use
LL | println!("{:?}{:?}", { std::convert::identity(&temp()) }, ());
| --------------------------^^^^^^---
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary value which is freed while still in use
| borrow later used here
|
= note: consider using a `let` binding to create a longer lived value

error: aborting due to 2 previous errors
error[E0716]: temporary value dropped while borrowed
--> $DIR/format-args-temporary-scopes.rs:33:64
|
LL | println!("{:?}{:?}", (), if true { std::convert::identity(&format!("")) } else { "" });
| ------------------------^^^^^^^^^^^-
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary value which is freed while still in use
| borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0716`.
Loading
Loading