Skip to content

Commit f94942d

Browse files
committed
Auto merge of #83842 - LeSeulArtichaut:thir-vec, r=nikomatsakis
Store THIR in `IndexVec`s instead of an `Arena` This is a necessary step to store the THIR in a query: #85273. See [relevant discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/278509-project-thir-unsafeck/topic/THIR-dependent.20queries.20design). r? `@ghost` cc `@cjgillot` `@nikomatsakis`
2 parents 3bcaeb0 + 7093a21 commit f94942d

File tree

20 files changed

+777
-723
lines changed

20 files changed

+777
-723
lines changed

compiler/rustc_driver/src/pretty.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ use rustc_hir_pretty as pprust_hir;
88
use rustc_middle::hir::map as hir_map;
99
use rustc_middle::ty::{self, TyCtxt};
1010
use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
11-
use rustc_mir_build::thir;
1211
use rustc_session::config::{Input, PpAstTreeMode, PpHirMode, PpMode, PpSourceMode};
1312
use rustc_session::Session;
1413
use rustc_span::symbol::Ident;
1514
use rustc_span::FileName;
1615

1716
use std::cell::Cell;
18-
use std::fmt::Write;
1917
use std::path::Path;
2018

2119
pub use self::PpMode::*;
@@ -490,18 +488,8 @@ fn print_with_analysis(
490488
}
491489

492490
ThirTree => {
493-
let mut out = String::new();
494-
abort_on_err(rustc_typeck::check_crate(tcx), tcx.sess);
495-
debug!("pretty printing THIR tree");
496-
for did in tcx.body_owners() {
497-
let hir = tcx.hir();
498-
let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(did)));
499-
let arena = thir::Arena::default();
500-
let thir =
501-
thir::build_thir(tcx, ty::WithOptConstParam::unknown(did), &arena, &body.value);
502-
let _ = writeln!(out, "{:?}:\n{:#?}\n", did, thir);
503-
}
504-
out
491+
// FIXME(rust-lang/project-thir-unsafeck#8)
492+
todo!()
505493
}
506494

507495
_ => unreachable!(),

compiler/rustc_mir_build/src/build/block.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1212
&mut self,
1313
destination: Place<'tcx>,
1414
block: BasicBlock,
15-
ast_block: &Block<'_, 'tcx>,
15+
ast_block: &Block,
1616
source_info: SourceInfo,
1717
) -> BlockAnd<()> {
1818
let Block {
1919
region_scope,
2020
opt_destruction_scope,
2121
span,
22-
stmts,
22+
ref stmts,
2323
expr,
2424
targeted_by_break,
2525
safety_mode,
2626
} = *ast_block;
27+
let expr = expr.map(|expr| &self.thir[expr]);
2728
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
2829
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
2930
if targeted_by_break {
@@ -32,13 +33,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3233
destination,
3334
block,
3435
span,
35-
stmts,
36+
&stmts,
3637
expr,
3738
safety_mode,
3839
))
3940
})
4041
} else {
41-
this.ast_block_stmts(destination, block, span, stmts, expr, safety_mode)
42+
this.ast_block_stmts(destination, block, span, &stmts, expr, safety_mode)
4243
}
4344
})
4445
})
@@ -49,8 +50,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4950
destination: Place<'tcx>,
5051
mut block: BasicBlock,
5152
span: Span,
52-
stmts: &[Stmt<'_, 'tcx>],
53-
expr: Option<&Expr<'_, 'tcx>>,
53+
stmts: &[StmtId],
54+
expr: Option<&Expr<'tcx>>,
5455
safety_mode: BlockSafety,
5556
) -> BlockAnd<()> {
5657
let this = self;
@@ -78,23 +79,30 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7879
this.update_source_scope_for_safety_mode(span, safety_mode);
7980

8081
let source_info = this.source_info(span);
81-
for Stmt { kind, opt_destruction_scope } in stmts {
82+
for stmt in stmts {
83+
let Stmt { ref kind, opt_destruction_scope } = this.thir[*stmt];
8284
match kind {
83-
&StmtKind::Expr { scope, expr } => {
85+
StmtKind::Expr { scope, expr } => {
8486
this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
8587
unpack!(
8688
block = this.in_opt_scope(
8789
opt_destruction_scope.map(|de| (de, source_info)),
8890
|this| {
89-
let si = (scope, source_info);
91+
let si = (*scope, source_info);
9092
this.in_scope(si, LintLevel::Inherited, |this| {
91-
this.stmt_expr(block, expr, Some(scope))
93+
this.stmt_expr(block, &this.thir[*expr], Some(*scope))
9294
})
9395
}
9496
)
9597
);
9698
}
97-
StmtKind::Let { remainder_scope, init_scope, pattern, initializer, lint_level } => {
99+
StmtKind::Let {
100+
remainder_scope,
101+
init_scope,
102+
ref pattern,
103+
initializer,
104+
lint_level,
105+
} => {
98106
let ignores_expr_result = matches!(*pattern.kind, PatKind::Wild);
99107
this.block_context.push(BlockFrame::Statement { ignores_expr_result });
100108

@@ -110,6 +118,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
110118

111119
// Evaluate the initializer, if present.
112120
if let Some(init) = initializer {
121+
let init = &this.thir[*init];
113122
let initializer_span = init.span;
114123

115124
unpack!(
@@ -145,7 +154,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
145154

146155
debug!("ast_block_stmts: pattern={:?}", pattern);
147156
this.visit_primary_bindings(
148-
&pattern,
157+
pattern,
149158
UserTypeProjections::none(),
150159
&mut |this, _, _, _, node, span, _, _| {
151160
this.storage_live_binding(block, node, span, OutsideGuard, true);

compiler/rustc_mir_build/src/build/expr/as_constant.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ use rustc_middle::ty::CanonicalUserTypeAnnotation;
88
impl<'a, 'tcx> Builder<'a, 'tcx> {
99
/// Compile `expr`, yielding a compile-time constant. Assumes that
1010
/// `expr` is a valid compile-time constant!
11-
crate fn as_constant(&mut self, expr: &Expr<'_, 'tcx>) -> Constant<'tcx> {
11+
crate fn as_constant(&mut self, expr: &Expr<'tcx>) -> Constant<'tcx> {
1212
let this = self;
1313
let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
1414
match *kind {
15-
ExprKind::Scope { region_scope: _, lint_level: _, value } => this.as_constant(value),
15+
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
16+
this.as_constant(&this.thir[value])
17+
}
1618
ExprKind::Literal { literal, user_ty, const_id: _ } => {
1719
let user_ty = user_ty.map(|user_ty| {
1820
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {

compiler/rustc_mir_build/src/build/expr/as_operand.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1717
crate fn as_local_operand(
1818
&mut self,
1919
block: BasicBlock,
20-
expr: &Expr<'_, 'tcx>,
20+
expr: &Expr<'tcx>,
2121
) -> BlockAnd<Operand<'tcx>> {
2222
let local_scope = self.local_scope();
2323
self.as_operand(block, Some(local_scope), expr)
@@ -74,7 +74,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7474
crate fn as_local_call_operand(
7575
&mut self,
7676
block: BasicBlock,
77-
expr: &Expr<'_, 'tcx>,
77+
expr: &Expr<'tcx>,
7878
) -> BlockAnd<Operand<'tcx>> {
7979
let local_scope = self.local_scope();
8080
self.as_call_operand(block, Some(local_scope), expr)
@@ -93,16 +93,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9393
&mut self,
9494
mut block: BasicBlock,
9595
scope: Option<region::Scope>,
96-
expr: &Expr<'_, 'tcx>,
96+
expr: &Expr<'tcx>,
9797
) -> BlockAnd<Operand<'tcx>> {
9898
debug!("as_operand(block={:?}, expr={:?})", block, expr);
9999
let this = self;
100100

101101
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
102102
let source_info = this.source_info(expr.span);
103103
let region_scope = (region_scope, source_info);
104-
return this
105-
.in_scope(region_scope, lint_level, |this| this.as_operand(block, scope, value));
104+
return this.in_scope(region_scope, lint_level, |this| {
105+
this.as_operand(block, scope, &this.thir[value])
106+
});
106107
}
107108

108109
let category = Category::of(&expr.kind).unwrap();
@@ -123,7 +124,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
123124
&mut self,
124125
mut block: BasicBlock,
125126
scope: Option<region::Scope>,
126-
expr: &Expr<'_, 'tcx>,
127+
expr: &Expr<'tcx>,
127128
) -> BlockAnd<Operand<'tcx>> {
128129
debug!("as_call_operand(block={:?}, expr={:?})", block, expr);
129130
let this = self;
@@ -132,7 +133,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
132133
let source_info = this.source_info(expr.span);
133134
let region_scope = (region_scope, source_info);
134135
return this.in_scope(region_scope, lint_level, |this| {
135-
this.as_call_operand(block, scope, value)
136+
this.as_call_operand(block, scope, &this.thir[value])
136137
});
137138
}
138139

@@ -151,7 +152,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
151152
// type, and that value is coming from the deref of a box.
152153
if let ExprKind::Deref { arg } = expr.kind {
153154
// Generate let tmp0 = arg0
154-
let operand = unpack!(block = this.as_temp(block, scope, arg, Mutability::Mut));
155+
let operand = unpack!(
156+
block = this.as_temp(block, scope, &this.thir[arg], Mutability::Mut)
157+
);
155158

156159
// Return the operand *tmp0 to be used as the call argument
157160
let place = Place {

compiler/rustc_mir_build/src/build/expr/as_place.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
381381
crate fn as_place(
382382
&mut self,
383383
mut block: BasicBlock,
384-
expr: &Expr<'_, 'tcx>,
384+
expr: &Expr<'tcx>,
385385
) -> BlockAnd<Place<'tcx>> {
386386
let place_builder = unpack!(block = self.as_place_builder(block, expr));
387387
block.and(place_builder.into_place(self.tcx, self.typeck_results))
@@ -392,7 +392,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
392392
crate fn as_place_builder(
393393
&mut self,
394394
block: BasicBlock,
395-
expr: &Expr<'_, 'tcx>,
395+
expr: &Expr<'tcx>,
396396
) -> BlockAnd<PlaceBuilder<'tcx>> {
397397
self.expr_as_place(block, expr, Mutability::Mut, None)
398398
}
@@ -405,7 +405,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
405405
crate fn as_read_only_place(
406406
&mut self,
407407
mut block: BasicBlock,
408-
expr: &Expr<'_, 'tcx>,
408+
expr: &Expr<'tcx>,
409409
) -> BlockAnd<Place<'tcx>> {
410410
let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
411411
block.and(place_builder.into_place(self.tcx, self.typeck_results))
@@ -420,15 +420,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
420420
fn as_read_only_place_builder(
421421
&mut self,
422422
block: BasicBlock,
423-
expr: &Expr<'_, 'tcx>,
423+
expr: &Expr<'tcx>,
424424
) -> BlockAnd<PlaceBuilder<'tcx>> {
425425
self.expr_as_place(block, expr, Mutability::Not, None)
426426
}
427427

428428
fn expr_as_place(
429429
&mut self,
430430
mut block: BasicBlock,
431-
expr: &Expr<'_, 'tcx>,
431+
expr: &Expr<'tcx>,
432432
mutability: Mutability,
433433
fake_borrow_temps: Option<&mut Vec<Local>>,
434434
) -> BlockAnd<PlaceBuilder<'tcx>> {
@@ -440,23 +440,27 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
440440
match expr.kind {
441441
ExprKind::Scope { region_scope, lint_level, value } => {
442442
this.in_scope((region_scope, source_info), lint_level, |this| {
443-
this.expr_as_place(block, value, mutability, fake_borrow_temps)
443+
this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps)
444444
})
445445
}
446446
ExprKind::Field { lhs, name } => {
447-
let place_builder =
448-
unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
447+
let place_builder = unpack!(
448+
block =
449+
this.expr_as_place(block, &this.thir[lhs], mutability, fake_borrow_temps,)
450+
);
449451
block.and(place_builder.field(name, expr.ty))
450452
}
451453
ExprKind::Deref { arg } => {
452-
let place_builder =
453-
unpack!(block = this.expr_as_place(block, arg, mutability, fake_borrow_temps,));
454+
let place_builder = unpack!(
455+
block =
456+
this.expr_as_place(block, &this.thir[arg], mutability, fake_borrow_temps,)
457+
);
454458
block.and(place_builder.deref())
455459
}
456460
ExprKind::Index { lhs, index } => this.lower_index_expression(
457461
block,
458-
lhs,
459-
index,
462+
&this.thir[lhs],
463+
&this.thir[index],
460464
mutability,
461465
fake_borrow_temps,
462466
expr.temp_lifetime,
@@ -481,7 +485,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
481485

482486
ExprKind::PlaceTypeAscription { source, user_ty } => {
483487
let place_builder = unpack!(
484-
block = this.expr_as_place(block, source, mutability, fake_borrow_temps,)
488+
block = this.expr_as_place(
489+
block,
490+
&this.thir[source],
491+
mutability,
492+
fake_borrow_temps,
493+
)
485494
);
486495
if let Some(user_ty) = user_ty {
487496
let annotation_index =
@@ -509,6 +518,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
509518
block.and(place_builder)
510519
}
511520
ExprKind::ValueTypeAscription { source, user_ty } => {
521+
let source = &this.thir[source];
512522
let temp =
513523
unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
514524
if let Some(user_ty) = user_ty {
@@ -613,8 +623,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
613623
fn lower_index_expression(
614624
&mut self,
615625
mut block: BasicBlock,
616-
base: &Expr<'_, 'tcx>,
617-
index: &Expr<'_, 'tcx>,
626+
base: &Expr<'tcx>,
627+
index: &Expr<'tcx>,
618628
mutability: Mutability,
619629
fake_borrow_temps: Option<&mut Vec<Local>>,
620630
temp_lifetime: Option<region::Scope>,

0 commit comments

Comments
 (0)