Skip to content

Commit

Permalink
Auto merge of rust-lang#116170 - matthewjasper:remove-thir-destructio…
Browse files Browse the repository at this point in the history
…n-scopes, r=cjgillot

Don't include destruction scopes in THIR

They are not used by anyone, and add memory/performance overhead.
  • Loading branch information
bors committed Dec 9, 2023
2 parents ce67033 + 43adf41 commit 1dfb228
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 468 deletions.
4 changes: 0 additions & 4 deletions compiler/rustc_middle/src/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,6 @@ impl ScopeTree {
}
}

pub fn opt_destruction_scope(&self, n: hir::ItemLocalId) -> Option<Scope> {
self.destruction_scopes.get(&n).cloned()
}

pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
assert!(var != lifetime.item_local_id());
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ pub struct Block {
/// This does *not* include labels on loops, e.g. `'label: loop {}`.
pub targeted_by_break: bool,
pub region_scope: region::Scope,
pub opt_destruction_scope: Option<region::Scope>,
/// The span of the block, including the opening braces,
/// the label, and the `unsafe` keyword, if present.
pub span: Span,
Expand Down Expand Up @@ -193,7 +192,6 @@ pub enum BlockSafety {
#[derive(Clone, Debug, HashStable)]
pub struct Stmt<'tcx> {
pub kind: StmtKind<'tcx>,
pub opt_destruction_scope: Option<region::Scope>,
}

#[derive(Clone, Debug, HashStable)]
Expand Down Expand Up @@ -1224,12 +1222,12 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
mod size_asserts {
use super::*;
// tidy-alphabetical-start
static_assert_size!(Block, 56);
static_assert_size!(Block, 48);
static_assert_size!(Expr<'_>, 64);
static_assert_size!(ExprKind<'_>, 40);
static_assert_size!(Pat<'_>, 64);
static_assert_size!(PatKind<'_>, 48);
static_assert_size!(Stmt<'_>, 56);
static_assert_size!(Stmt<'_>, 48);
static_assert_size!(StmtKind<'_>, 48);
// tidy-alphabetical-end
}
156 changes: 66 additions & 90 deletions compiler/rustc_mir_build/src/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,33 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ast_block: BlockId,
source_info: SourceInfo,
) -> BlockAnd<()> {
let Block {
region_scope,
opt_destruction_scope,
span,
ref stmts,
expr,
targeted_by_break,
safety_mode,
} = self.thir[ast_block];
let Block { region_scope, span, ref stmts, expr, targeted_by_break, safety_mode } =
self.thir[ast_block];
let expr = expr.map(|expr| &self.thir[expr]);
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
if targeted_by_break {
this.in_breakable_scope(None, destination, span, |this| {
Some(this.ast_block_stmts(
destination,
block,
span,
stmts,
expr,
safety_mode,
region_scope,
))
})
} else {
this.ast_block_stmts(
self.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
if targeted_by_break {
this.in_breakable_scope(None, destination, span, |this| {
Some(this.ast_block_stmts(
destination,
block,
span,
stmts,
expr,
safety_mode,
region_scope,
)
}
})
))
})
} else {
this.ast_block_stmts(
destination,
block,
span,
stmts,
expr,
safety_mode,
region_scope,
)
}
})
}

Expand Down Expand Up @@ -92,20 +83,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

let source_info = this.source_info(span);
for stmt in stmts {
let Stmt { ref kind, opt_destruction_scope } = this.thir[*stmt];
let Stmt { ref kind } = this.thir[*stmt];
match kind {
StmtKind::Expr { scope, expr } => {
this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
let si = (*scope, source_info);
unpack!(
block = this.in_opt_scope(
opt_destruction_scope.map(|de| (de, source_info)),
|this| {
let si = (*scope, source_info);
this.in_scope(si, LintLevel::Inherited, |this| {
this.stmt_expr(block, &this.thir[*expr], Some(*scope))
})
}
)
block = this.in_scope(si, LintLevel::Inherited, |this| {
this.stmt_expr(block, &this.thir[*expr], Some(*scope))
})
);
}
StmtKind::Let {
Expand Down Expand Up @@ -221,43 +207,38 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

let init = &this.thir[*initializer];
let initializer_span = init.span;
let scope = (*init_scope, source_info);
let failure = unpack!(
block = this.in_opt_scope(
opt_destruction_scope.map(|de| (de, source_info)),
|this| {
let scope = (*init_scope, source_info);
this.in_scope(scope, *lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
pattern,
None,
Some((Some(&destination), initializer_span)),
);
this.visit_primary_bindings(
pattern,
UserTypeProjections::none(),
&mut |this, _, _, _, node, span, _, _| {
this.storage_live_binding(
block,
node,
span,
OutsideGuard,
true,
);
},
);
this.ast_let_else(
block = this.in_scope(scope, *lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
pattern,
None,
Some((Some(&destination), initializer_span)),
);
this.visit_primary_bindings(
pattern,
UserTypeProjections::none(),
&mut |this, _, _, _, node, span, _, _| {
this.storage_live_binding(
block,
init,
initializer_span,
*else_block,
&last_remainder_scope,
pattern,
)
})
}
)
node,
span,
OutsideGuard,
true,
);
},
);
this.ast_let_else(
block,
init,
initializer_span,
*else_block,
&last_remainder_scope,
pattern,
)
})
);
this.cfg.goto(failure, source_info, failure_entry);

Expand Down Expand Up @@ -298,25 +279,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if let Some(init) = initializer {
let init = &this.thir[*init];
let initializer_span = init.span;
let scope = (*init_scope, source_info);

unpack!(
block = this.in_opt_scope(
opt_destruction_scope.map(|de| (de, source_info)),
|this| {
let scope = (*init_scope, source_info);
this.in_scope(scope, *lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
pattern,
None,
Some((None, initializer_span)),
);
this.expr_into_pattern(block, pattern, init)
// irrefutable pattern
})
},
)
block = this.in_scope(scope, *lint_level, |this| {
this.declare_bindings(
visibility_scope,
remainder_span,
pattern,
None,
Some((None, initializer_span)),
);
this.expr_into_pattern(block, &pattern, init)
// irrefutable pattern
})
)
} else {
let scope = (*init_scope, source_info);
Expand Down
21 changes: 0 additions & 21 deletions compiler/rustc_mir_build/src/build/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,27 +536,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
(then_block, else_block)
}

pub(crate) fn in_opt_scope<F, R>(
&mut self,
opt_scope: Option<(region::Scope, SourceInfo)>,
f: F,
) -> BlockAnd<R>
where
F: FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<R>,
{
debug!("in_opt_scope(opt_scope={:?})", opt_scope);
if let Some(region_scope) = opt_scope {
self.push_scope(region_scope);
}
let mut block;
let rv = unpack!(block = f(self));
if let Some(region_scope) = opt_scope {
unpack!(block = self.pop_scope(region_scope, block));
}
debug!("in_scope: exiting opt_scope={:?} block={:?}", opt_scope, block);
block.and(rv)
}

/// Convenience wrapper that pushes a scope and then executes `f`
/// to build its contents, popping the scope afterwards.
#[instrument(skip(self, f), level = "debug")]
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_mir_build/src/thir/cx/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ impl<'tcx> Cx<'tcx> {
// We have to eagerly lower the "spine" of the statements
// in order to get the lexical scoping correctly.
let stmts = self.mirror_stmts(block.hir_id.local_id, block.stmts);
let opt_destruction_scope =
self.region_scope_tree.opt_destruction_scope(block.hir_id.local_id);
let block = Block {
targeted_by_break: block.targeted_by_break,
region_scope: region::Scope {
id: block.hir_id.local_id,
data: region::ScopeData::Node,
},
opt_destruction_scope,
span: block.span,
stmts,
expr: block.expr.map(|expr| self.mirror_expr(expr)),
Expand Down Expand Up @@ -49,7 +46,6 @@ impl<'tcx> Cx<'tcx> {
.enumerate()
.filter_map(|(index, stmt)| {
let hir_id = stmt.hir_id;
let opt_dxn_ext = self.region_scope_tree.opt_destruction_scope(hir_id.local_id);
match stmt.kind {
hir::StmtKind::Expr(expr) | hir::StmtKind::Semi(expr) => {
let stmt = Stmt {
Expand All @@ -60,7 +56,6 @@ impl<'tcx> Cx<'tcx> {
},
expr: self.mirror_expr(expr),
},
opt_destruction_scope: opt_dxn_ext,
};
Some(self.thir.stmts.push(stmt))
}
Expand Down Expand Up @@ -122,7 +117,6 @@ impl<'tcx> Cx<'tcx> {
lint_level: LintLevel::Explicit(local.hir_id),
span,
},
opt_destruction_scope: opt_dxn_ext,
};
Some(self.thir.stmts.push(stmt))
}
Expand Down
18 changes: 1 addition & 17 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'tcx> Cx<'tcx> {

trace!(?expr.ty, "after adjustments");

// Next, wrap this up in the expr's scope.
// Finally, wrap this up in the expr's scope.
expr = Expr {
temp_lifetime: expr.temp_lifetime,
ty: expr.ty,
Expand All @@ -66,22 +66,6 @@ impl<'tcx> Cx<'tcx> {
},
};

// Finally, create a destruction scope, if any.
if let Some(region_scope) =
self.region_scope_tree.opt_destruction_scope(hir_expr.hir_id.local_id)
{
expr = Expr {
temp_lifetime: expr.temp_lifetime,
ty: expr.ty,
span: hir_expr.span,
kind: ExprKind::Scope {
region_scope,
value: self.thir.exprs.push(expr),
lint_level: LintLevel::Inherited,
},
};
}

// OK, all done!
self.thir.exprs.push(expr)
}
Expand Down
23 changes: 3 additions & 20 deletions compiler/rustc_mir_build/src/thir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,11 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
}

fn print_block(&mut self, block_id: BlockId, depth_lvl: usize) {
let Block {
targeted_by_break,
opt_destruction_scope,
span,
region_scope,
stmts,
expr,
safety_mode,
} = &self.thir.blocks[block_id];
let Block { targeted_by_break, span, region_scope, stmts, expr, safety_mode } =
&self.thir.blocks[block_id];

print_indented!(self, "Block {", depth_lvl);
print_indented!(self, format!("targeted_by_break: {}", targeted_by_break), depth_lvl + 1);
print_indented!(
self,
format!("opt_destruction_scope: {:?}", opt_destruction_scope),
depth_lvl + 1
);
print_indented!(self, format!("span: {:?}", span), depth_lvl + 1);
print_indented!(self, format!("region_scope: {:?}", region_scope), depth_lvl + 1);
print_indented!(self, format!("safety_mode: {:?}", safety_mode), depth_lvl + 1);
Expand All @@ -133,14 +121,9 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
}

fn print_stmt(&mut self, stmt_id: StmtId, depth_lvl: usize) {
let Stmt { kind, opt_destruction_scope } = &self.thir.stmts[stmt_id];
let Stmt { kind } = &self.thir.stmts[stmt_id];

print_indented!(self, "Stmt {", depth_lvl);
print_indented!(
self,
format!("opt_destruction_scope: {:?}", opt_destruction_scope),
depth_lvl + 1
);

match kind {
StmtKind::Expr { scope, expr } => {
Expand Down
Loading

0 comments on commit 1dfb228

Please sign in to comment.