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
19 changes: 15 additions & 4 deletions compiler/rustc_middle/src/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ pub struct ScopeTree {
pub backwards_incompatible_scope: UnordMap<hir::ItemLocalId, Scope>,
}

/// Temporary lifetime information for expressions, used when lowering to MIR.
#[derive(Clone, Copy, Debug, HashStable)]
pub struct TempLifetime {
/// The scope in which a temporary should be dropped. If `None`, no drop is scheduled; this is
/// the case for lifetime-extended temporaries extended by a const/static item or const block.
pub temp_lifetime: Option<Scope>,
/// If `Some(lt)`, indicates that the lifetime of this temporary will change to `lt` in a future edition.
/// If `None`, then no changes are expected, or lints are disabled.
pub backwards_incompatible: Option<Scope>,
}

impl ScopeTree {
pub fn record_scope_parent(&mut self, child: Scope, parent: Option<Scope>) {
debug!("{:?}.parent = {:?}", child, parent);
Expand Down Expand Up @@ -332,16 +343,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 TempLifetime { temp_lifetime: s, backwards_incompatible: None };
}

// 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 }
}
}
20 changes: 5 additions & 15 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,25 +256,15 @@ pub struct Expr<'tcx> {
/// The type of this expression
pub ty: Ty<'tcx>,

/// The lifetime of this expression if it should be spilled into a
/// temporary
pub temp_lifetime: TempLifetime,
/// The id of the HIR expression whose [temporary scope] should be used for this expression.
///
/// [temporary scope]: https://doc.rust-lang.org/reference/destructors.html#temporary-scopes
pub temp_scope_id: hir::ItemLocalId,
Comment on lines -259 to +262
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 could be removed entirely from thir::Expr, and we could use thir::ExprKind::Scopes to set up temporary scopes, but it's a little subtle/inconvenient for certain THIR lowerings (looking at &pin mut here) so for now I've opted to keep it simple.


/// span of the expression in the source
pub span: Span,
}

/// Temporary lifetime information for THIR expressions
#[derive(Clone, Copy, Debug, HashStable)]
pub struct TempLifetime {
/// Lifetime for temporaries as expected.
/// This should be `None` in a constant context.
pub temp_lifetime: Option<region::Scope>,
/// If `Some(lt)`, indicates that the lifetime of this temporary will change to `lt` in a future edition.
/// If `None`, then no changes are expected, or lints are disabled.
pub backwards_incompatible: Option<region::Scope>,
}

#[derive(Clone, Debug, HashStable)]
pub enum ExprKind<'tcx> {
/// `Scope`s are used to explicitly mark destruction scopes,
Expand Down Expand Up @@ -1127,7 +1117,7 @@ mod size_asserts {
use super::*;
// tidy-alphabetical-start
static_assert_size!(Block, 48);
static_assert_size!(Expr<'_>, 72);
static_assert_size!(Expr<'_>, 64);
static_assert_size!(ExprKind<'_>, 40);
static_assert_size!(Pat<'_>, 64);
static_assert_size!(PatKind<'_>, 48);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/thir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
expr: &'thir Expr<'tcx>,
) {
use ExprKind::*;
let Expr { kind, ty: _, temp_lifetime: _, span: _ } = expr;
let Expr { kind, ty: _, temp_scope_id: _, span: _ } = expr;
match *kind {
Scope { value, region_scope: _, lint_level: _ } => {
visitor.visit_expr(&visitor.thir()[value])
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/builder/expr/as_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
pub(crate) fn as_constant(&mut self, expr: &Expr<'tcx>) -> ConstOperand<'tcx> {
let this = self;
let tcx = this.tcx;
let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
let Expr { ty, temp_scope_id: _, span, ref kind } = *expr;
match kind {
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
this.as_constant(&this.thir[*value])
Expand All @@ -46,7 +46,7 @@ pub(crate) fn as_constant_inner<'tcx>(
push_cuta: impl FnMut(&Box<CanonicalUserType<'tcx>>) -> Option<UserTypeAnnotationIndex>,
tcx: TyCtxt<'tcx>,
) -> ConstOperand<'tcx> {
let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
let Expr { ty, temp_scope_id: _, span, ref kind } = *expr;
match *kind {
ExprKind::Literal { lit, neg } => {
let const_ = lit_to_mir_constant(tcx, LitToConstInput { lit: lit.node, ty, neg });
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_build/src/builder/expr/as_operand.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! See docs in build/expr/mod.rs

use rustc_middle::middle::region::TempLifetime;
use rustc_middle::mir::*;
use rustc_middle::thir::*;
use tracing::{debug, instrument};
Expand Down
23 changes: 12 additions & 11 deletions compiler/rustc_mir_build/src/builder/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(place_builder)
}
ExprKind::ValueTypeAscription { source, ref user_ty, user_ty_span } => {
let source_expr = &this.thir[source];
let temp = unpack!(
block = this.as_temp(block, source_expr.temp_lifetime, source, mutability)
);
let temp_lifetime =
this.region_scope_tree.temporary_scope(this.thir[source].temp_scope_id);
let temp = unpack!(block = this.as_temp(block, temp_lifetime, source, mutability));
if let Some(user_ty) = user_ty {
let ty_source_info = this.source_info(user_ty_span);
let annotation_index =
Expand Down Expand Up @@ -539,10 +538,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(place_builder.project(PlaceElem::UnwrapUnsafeBinder(expr.ty)))
}
ExprKind::ValueUnwrapUnsafeBinder { source } => {
let source_expr = &this.thir[source];
let temp = unpack!(
block = this.as_temp(block, source_expr.temp_lifetime, source, mutability)
);
let temp_lifetime =
this.region_scope_tree.temporary_scope(this.thir[source].temp_scope_id);
let temp = unpack!(block = this.as_temp(block, temp_lifetime, source, mutability));
block.and(PlaceBuilder::from(temp).project(PlaceElem::UnwrapUnsafeBinder(expr.ty)))
}

Expand Down Expand Up @@ -590,8 +588,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
| ExprKind::WrapUnsafeBinder { .. } => {
// these are not places, so we need to make a temporary.
debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Place)));
let temp =
unpack!(block = this.as_temp(block, expr.temp_lifetime, expr_id, mutability));
let temp_lifetime = this.region_scope_tree.temporary_scope(expr.temp_scope_id);
let temp = unpack!(block = this.as_temp(block, temp_lifetime, expr_id, mutability));
block.and(PlaceBuilder::from(temp))
}
}
Expand Down Expand Up @@ -637,7 +635,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Making this a *fresh* temporary means we do not have to worry about
// the index changing later: Nothing will ever change this temporary.
// The "retagging" transformation (for Stacked Borrows) relies on this.
let index_lifetime = self.thir[index].temp_lifetime;
// Using the enclosing temporary scope for the index ensures it will live past where this
// place is used. This lifetime may be larger than strictly necessary but it means we don't
// need to pass a scope for operands to `as_place`.
let index_lifetime = self.region_scope_tree.temporary_scope(self.thir[index].temp_scope_id);
let idx = unpack!(block = self.as_temp(block, index_lifetime, index, Mutability::Not));

block = self.bounds_check(block, &base_place, idx, expr_span, source_info);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_abi::FieldIdx;
use rustc_hir::lang_items::LangItem;
use rustc_index::{Idx, IndexVec};
use rustc_middle::bug;
use rustc_middle::middle::region;
use rustc_middle::middle::region::{self, TempLifetime};
use rustc_middle::mir::interpret::Scalar;
use rustc_middle::mir::*;
use rustc_middle::thir::*;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/builder/expr/as_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir::HirId;
use rustc_middle::middle::region::{Scope, ScopeData};
use rustc_middle::middle::region::{Scope, ScopeData, TempLifetime};
use rustc_middle::mir::*;
use rustc_middle::thir::*;
use tracing::{debug, instrument};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/builder/expr/stmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_middle::middle::region;
use rustc_middle::middle::region::{self, TempLifetime};
use rustc_middle::mir::*;
use rustc_middle::span_bug;
use rustc_middle::thir::*;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/builder/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir::{BindingMode, ByRef, LangItem, LetStmt, LocalSource, Node, Pinnedness};
use rustc_middle::middle::region;
use rustc_middle::middle::region::{self, TempLifetime};
use rustc_middle::mir::*;
use rustc_middle::thir::{self, *};
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, ValTree, ValTreeKind};
Expand Down
Loading
Loading