Skip to content

Commit 43adf41

Browse files
committed
Don't include destruction scopes in THIR
They are not used by anyone, and add memory/performance overhead.
1 parent 0a83e43 commit 43adf41

File tree

11 files changed

+235
-468
lines changed

11 files changed

+235
-468
lines changed

compiler/rustc_middle/src/middle/region.rs

-4
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,6 @@ impl ScopeTree {
349349
}
350350
}
351351

352-
pub fn opt_destruction_scope(&self, n: hir::ItemLocalId) -> Option<Scope> {
353-
self.destruction_scopes.get(&n).cloned()
354-
}
355-
356352
pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
357353
debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
358354
assert!(var != lifetime.item_local_id());

compiler/rustc_middle/src/thir.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ pub struct Block {
134134
/// This does *not* include labels on loops, e.g. `'label: loop {}`.
135135
pub targeted_by_break: bool,
136136
pub region_scope: region::Scope,
137-
pub opt_destruction_scope: Option<region::Scope>,
138137
/// The span of the block, including the opening braces,
139138
/// the label, and the `unsafe` keyword, if present.
140139
pub span: Span,
@@ -193,7 +192,6 @@ pub enum BlockSafety {
193192
#[derive(Clone, Debug, HashStable)]
194193
pub struct Stmt<'tcx> {
195194
pub kind: StmtKind<'tcx>,
196-
pub opt_destruction_scope: Option<region::Scope>,
197195
}
198196

199197
#[derive(Clone, Debug, HashStable)]
@@ -1224,12 +1222,12 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
12241222
mod size_asserts {
12251223
use super::*;
12261224
// tidy-alphabetical-start
1227-
static_assert_size!(Block, 56);
1225+
static_assert_size!(Block, 48);
12281226
static_assert_size!(Expr<'_>, 64);
12291227
static_assert_size!(ExprKind<'_>, 40);
12301228
static_assert_size!(Pat<'_>, 64);
12311229
static_assert_size!(PatKind<'_>, 48);
1232-
static_assert_size!(Stmt<'_>, 56);
1230+
static_assert_size!(Stmt<'_>, 48);
12331231
static_assert_size!(StmtKind<'_>, 48);
12341232
// tidy-alphabetical-end
12351233
}

compiler/rustc_mir_build/src/build/block.rs

+66-90
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,33 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1313
ast_block: BlockId,
1414
source_info: SourceInfo,
1515
) -> BlockAnd<()> {
16-
let Block {
17-
region_scope,
18-
opt_destruction_scope,
19-
span,
20-
ref stmts,
21-
expr,
22-
targeted_by_break,
23-
safety_mode,
24-
} = self.thir[ast_block];
16+
let Block { region_scope, span, ref stmts, expr, targeted_by_break, safety_mode } =
17+
self.thir[ast_block];
2518
let expr = expr.map(|expr| &self.thir[expr]);
26-
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
27-
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
28-
if targeted_by_break {
29-
this.in_breakable_scope(None, destination, span, |this| {
30-
Some(this.ast_block_stmts(
31-
destination,
32-
block,
33-
span,
34-
stmts,
35-
expr,
36-
safety_mode,
37-
region_scope,
38-
))
39-
})
40-
} else {
41-
this.ast_block_stmts(
19+
self.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
20+
if targeted_by_break {
21+
this.in_breakable_scope(None, destination, span, |this| {
22+
Some(this.ast_block_stmts(
4223
destination,
4324
block,
4425
span,
4526
stmts,
4627
expr,
4728
safety_mode,
4829
region_scope,
49-
)
50-
}
51-
})
30+
))
31+
})
32+
} else {
33+
this.ast_block_stmts(
34+
destination,
35+
block,
36+
span,
37+
stmts,
38+
expr,
39+
safety_mode,
40+
region_scope,
41+
)
42+
}
5243
})
5344
}
5445

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

9384
let source_info = this.source_info(span);
9485
for stmt in stmts {
95-
let Stmt { ref kind, opt_destruction_scope } = this.thir[*stmt];
86+
let Stmt { ref kind } = this.thir[*stmt];
9687
match kind {
9788
StmtKind::Expr { scope, expr } => {
9889
this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
90+
let si = (*scope, source_info);
9991
unpack!(
100-
block = this.in_opt_scope(
101-
opt_destruction_scope.map(|de| (de, source_info)),
102-
|this| {
103-
let si = (*scope, source_info);
104-
this.in_scope(si, LintLevel::Inherited, |this| {
105-
this.stmt_expr(block, &this.thir[*expr], Some(*scope))
106-
})
107-
}
108-
)
92+
block = this.in_scope(si, LintLevel::Inherited, |this| {
93+
this.stmt_expr(block, &this.thir[*expr], Some(*scope))
94+
})
10995
);
11096
}
11197
StmtKind::Let {
@@ -221,43 +207,38 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
221207

222208
let init = &this.thir[*initializer];
223209
let initializer_span = init.span;
210+
let scope = (*init_scope, source_info);
224211
let failure = unpack!(
225-
block = this.in_opt_scope(
226-
opt_destruction_scope.map(|de| (de, source_info)),
227-
|this| {
228-
let scope = (*init_scope, source_info);
229-
this.in_scope(scope, *lint_level, |this| {
230-
this.declare_bindings(
231-
visibility_scope,
232-
remainder_span,
233-
pattern,
234-
None,
235-
Some((Some(&destination), initializer_span)),
236-
);
237-
this.visit_primary_bindings(
238-
pattern,
239-
UserTypeProjections::none(),
240-
&mut |this, _, _, _, node, span, _, _| {
241-
this.storage_live_binding(
242-
block,
243-
node,
244-
span,
245-
OutsideGuard,
246-
true,
247-
);
248-
},
249-
);
250-
this.ast_let_else(
212+
block = this.in_scope(scope, *lint_level, |this| {
213+
this.declare_bindings(
214+
visibility_scope,
215+
remainder_span,
216+
pattern,
217+
None,
218+
Some((Some(&destination), initializer_span)),
219+
);
220+
this.visit_primary_bindings(
221+
pattern,
222+
UserTypeProjections::none(),
223+
&mut |this, _, _, _, node, span, _, _| {
224+
this.storage_live_binding(
251225
block,
252-
init,
253-
initializer_span,
254-
*else_block,
255-
&last_remainder_scope,
256-
pattern,
257-
)
258-
})
259-
}
260-
)
226+
node,
227+
span,
228+
OutsideGuard,
229+
true,
230+
);
231+
},
232+
);
233+
this.ast_let_else(
234+
block,
235+
init,
236+
initializer_span,
237+
*else_block,
238+
&last_remainder_scope,
239+
pattern,
240+
)
241+
})
261242
);
262243
this.cfg.goto(failure, source_info, failure_entry);
263244

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

302284
unpack!(
303-
block = this.in_opt_scope(
304-
opt_destruction_scope.map(|de| (de, source_info)),
305-
|this| {
306-
let scope = (*init_scope, source_info);
307-
this.in_scope(scope, *lint_level, |this| {
308-
this.declare_bindings(
309-
visibility_scope,
310-
remainder_span,
311-
pattern,
312-
None,
313-
Some((None, initializer_span)),
314-
);
315-
this.expr_into_pattern(block, pattern, init)
316-
// irrefutable pattern
317-
})
318-
},
319-
)
285+
block = this.in_scope(scope, *lint_level, |this| {
286+
this.declare_bindings(
287+
visibility_scope,
288+
remainder_span,
289+
pattern,
290+
None,
291+
Some((None, initializer_span)),
292+
);
293+
this.expr_into_pattern(block, &pattern, init)
294+
// irrefutable pattern
295+
})
320296
)
321297
} else {
322298
let scope = (*init_scope, source_info);

compiler/rustc_mir_build/src/build/scope.rs

-21
Original file line numberDiff line numberDiff line change
@@ -537,27 +537,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
537537
(then_block, else_block)
538538
}
539539

540-
pub(crate) fn in_opt_scope<F, R>(
541-
&mut self,
542-
opt_scope: Option<(region::Scope, SourceInfo)>,
543-
f: F,
544-
) -> BlockAnd<R>
545-
where
546-
F: FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<R>,
547-
{
548-
debug!("in_opt_scope(opt_scope={:?})", opt_scope);
549-
if let Some(region_scope) = opt_scope {
550-
self.push_scope(region_scope);
551-
}
552-
let mut block;
553-
let rv = unpack!(block = f(self));
554-
if let Some(region_scope) = opt_scope {
555-
unpack!(block = self.pop_scope(region_scope, block));
556-
}
557-
debug!("in_scope: exiting opt_scope={:?} block={:?}", opt_scope, block);
558-
block.and(rv)
559-
}
560-
561540
/// Convenience wrapper that pushes a scope and then executes `f`
562541
/// to build its contents, popping the scope afterwards.
563542
#[instrument(skip(self, f), level = "debug")]

compiler/rustc_mir_build/src/thir/cx/block.rs

-6
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ impl<'tcx> Cx<'tcx> {
1313
// We have to eagerly lower the "spine" of the statements
1414
// in order to get the lexical scoping correctly.
1515
let stmts = self.mirror_stmts(block.hir_id.local_id, block.stmts);
16-
let opt_destruction_scope =
17-
self.region_scope_tree.opt_destruction_scope(block.hir_id.local_id);
1816
let block = Block {
1917
targeted_by_break: block.targeted_by_break,
2018
region_scope: region::Scope {
2119
id: block.hir_id.local_id,
2220
data: region::ScopeData::Node,
2321
},
24-
opt_destruction_scope,
2522
span: block.span,
2623
stmts,
2724
expr: block.expr.map(|expr| self.mirror_expr(expr)),
@@ -49,7 +46,6 @@ impl<'tcx> Cx<'tcx> {
4946
.enumerate()
5047
.filter_map(|(index, stmt)| {
5148
let hir_id = stmt.hir_id;
52-
let opt_dxn_ext = self.region_scope_tree.opt_destruction_scope(hir_id.local_id);
5349
match stmt.kind {
5450
hir::StmtKind::Expr(expr) | hir::StmtKind::Semi(expr) => {
5551
let stmt = Stmt {
@@ -60,7 +56,6 @@ impl<'tcx> Cx<'tcx> {
6056
},
6157
expr: self.mirror_expr(expr),
6258
},
63-
opt_destruction_scope: opt_dxn_ext,
6459
};
6560
Some(self.thir.stmts.push(stmt))
6661
}
@@ -122,7 +117,6 @@ impl<'tcx> Cx<'tcx> {
122117
lint_level: LintLevel::Explicit(local.hir_id),
123118
span,
124119
},
125-
opt_destruction_scope: opt_dxn_ext,
126120
};
127121
Some(self.thir.stmts.push(stmt))
128122
}

compiler/rustc_mir_build/src/thir/cx/expr.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'tcx> Cx<'tcx> {
5454

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

57-
// Next, wrap this up in the expr's scope.
57+
// Finally, wrap this up in the expr's scope.
5858
expr = Expr {
5959
temp_lifetime: expr.temp_lifetime,
6060
ty: expr.ty,
@@ -66,22 +66,6 @@ impl<'tcx> Cx<'tcx> {
6666
},
6767
};
6868

69-
// Finally, create a destruction scope, if any.
70-
if let Some(region_scope) =
71-
self.region_scope_tree.opt_destruction_scope(hir_expr.hir_id.local_id)
72-
{
73-
expr = Expr {
74-
temp_lifetime: expr.temp_lifetime,
75-
ty: expr.ty,
76-
span: hir_expr.span,
77-
kind: ExprKind::Scope {
78-
region_scope,
79-
value: self.thir.exprs.push(expr),
80-
lint_level: LintLevel::Inherited,
81-
},
82-
};
83-
}
84-
8569
// OK, all done!
8670
self.thir.exprs.push(expr)
8771
}

compiler/rustc_mir_build/src/thir/print.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,11 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
9191
}
9292

9393
fn print_block(&mut self, block_id: BlockId, depth_lvl: usize) {
94-
let Block {
95-
targeted_by_break,
96-
opt_destruction_scope,
97-
span,
98-
region_scope,
99-
stmts,
100-
expr,
101-
safety_mode,
102-
} = &self.thir.blocks[block_id];
94+
let Block { targeted_by_break, span, region_scope, stmts, expr, safety_mode } =
95+
&self.thir.blocks[block_id];
10396

10497
print_indented!(self, "Block {", depth_lvl);
10598
print_indented!(self, format!("targeted_by_break: {}", targeted_by_break), depth_lvl + 1);
106-
print_indented!(
107-
self,
108-
format!("opt_destruction_scope: {:?}", opt_destruction_scope),
109-
depth_lvl + 1
110-
);
11199
print_indented!(self, format!("span: {:?}", span), depth_lvl + 1);
112100
print_indented!(self, format!("region_scope: {:?}", region_scope), depth_lvl + 1);
113101
print_indented!(self, format!("safety_mode: {:?}", safety_mode), depth_lvl + 1);
@@ -133,14 +121,9 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
133121
}
134122

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

138126
print_indented!(self, "Stmt {", depth_lvl);
139-
print_indented!(
140-
self,
141-
format!("opt_destruction_scope: {:?}", opt_destruction_scope),
142-
depth_lvl + 1
143-
);
144127

145128
match kind {
146129
StmtKind::Expr { scope, expr } => {

0 commit comments

Comments
 (0)