Skip to content

Commit

Permalink
Auto merge of #83842 - LeSeulArtichaut:thir-vec, r=nikomatsakis
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed May 19, 2021
2 parents 3bcaeb0 + 7093a21 commit f94942d
Show file tree
Hide file tree
Showing 20 changed files with 777 additions and 723 deletions.
16 changes: 2 additions & 14 deletions compiler/rustc_driver/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ use rustc_hir_pretty as pprust_hir;
use rustc_middle::hir::map as hir_map;
use rustc_middle::ty::{self, TyCtxt};
use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
use rustc_mir_build::thir;
use rustc_session::config::{Input, PpAstTreeMode, PpHirMode, PpMode, PpSourceMode};
use rustc_session::Session;
use rustc_span::symbol::Ident;
use rustc_span::FileName;

use std::cell::Cell;
use std::fmt::Write;
use std::path::Path;

pub use self::PpMode::*;
Expand Down Expand Up @@ -490,18 +488,8 @@ fn print_with_analysis(
}

ThirTree => {
let mut out = String::new();
abort_on_err(rustc_typeck::check_crate(tcx), tcx.sess);
debug!("pretty printing THIR tree");
for did in tcx.body_owners() {
let hir = tcx.hir();
let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(did)));
let arena = thir::Arena::default();
let thir =
thir::build_thir(tcx, ty::WithOptConstParam::unknown(did), &arena, &body.value);
let _ = writeln!(out, "{:?}:\n{:#?}\n", did, thir);
}
out
// FIXME(rust-lang/project-thir-unsafeck#8)
todo!()
}

_ => unreachable!(),
Expand Down
33 changes: 21 additions & 12 deletions compiler/rustc_mir_build/src/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
&mut self,
destination: Place<'tcx>,
block: BasicBlock,
ast_block: &Block<'_, 'tcx>,
ast_block: &Block,
source_info: SourceInfo,
) -> BlockAnd<()> {
let Block {
region_scope,
opt_destruction_scope,
span,
stmts,
ref stmts,
expr,
targeted_by_break,
safety_mode,
} = *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 {
Expand All @@ -32,13 +33,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
destination,
block,
span,
stmts,
&stmts,
expr,
safety_mode,
))
})
} else {
this.ast_block_stmts(destination, block, span, stmts, expr, safety_mode)
this.ast_block_stmts(destination, block, span, &stmts, expr, safety_mode)
}
})
})
Expand All @@ -49,8 +50,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
destination: Place<'tcx>,
mut block: BasicBlock,
span: Span,
stmts: &[Stmt<'_, 'tcx>],
expr: Option<&Expr<'_, 'tcx>>,
stmts: &[StmtId],
expr: Option<&Expr<'tcx>>,
safety_mode: BlockSafety,
) -> BlockAnd<()> {
let this = self;
Expand Down Expand Up @@ -78,23 +79,30 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.update_source_scope_for_safety_mode(span, safety_mode);

let source_info = this.source_info(span);
for Stmt { kind, opt_destruction_scope } in stmts {
for stmt in stmts {
let Stmt { ref kind, opt_destruction_scope } = this.thir[*stmt];
match kind {
&StmtKind::Expr { scope, expr } => {
StmtKind::Expr { scope, expr } => {
this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
unpack!(
block = this.in_opt_scope(
opt_destruction_scope.map(|de| (de, source_info)),
|this| {
let si = (scope, source_info);
let si = (*scope, source_info);
this.in_scope(si, LintLevel::Inherited, |this| {
this.stmt_expr(block, expr, Some(scope))
this.stmt_expr(block, &this.thir[*expr], Some(*scope))
})
}
)
);
}
StmtKind::Let { remainder_scope, init_scope, pattern, initializer, lint_level } => {
StmtKind::Let {
remainder_scope,
init_scope,
ref pattern,
initializer,
lint_level,
} => {
let ignores_expr_result = matches!(*pattern.kind, PatKind::Wild);
this.block_context.push(BlockFrame::Statement { ignores_expr_result });

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

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

unpack!(
Expand Down Expand Up @@ -145,7 +154,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

debug!("ast_block_stmts: pattern={:?}", pattern);
this.visit_primary_bindings(
&pattern,
pattern,
UserTypeProjections::none(),
&mut |this, _, _, _, node, span, _, _| {
this.storage_live_binding(block, node, span, OutsideGuard, true);
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_mir_build/src/build/expr/as_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ use rustc_middle::ty::CanonicalUserTypeAnnotation;
impl<'a, 'tcx> Builder<'a, 'tcx> {
/// Compile `expr`, yielding a compile-time constant. Assumes that
/// `expr` is a valid compile-time constant!
crate fn as_constant(&mut self, expr: &Expr<'_, 'tcx>) -> Constant<'tcx> {
crate fn as_constant(&mut self, expr: &Expr<'tcx>) -> Constant<'tcx> {
let this = self;
let Expr { ty, temp_lifetime: _, span, ref kind } = *expr;
match *kind {
ExprKind::Scope { region_scope: _, lint_level: _, value } => this.as_constant(value),
ExprKind::Scope { region_scope: _, lint_level: _, value } => {
this.as_constant(&this.thir[value])
}
ExprKind::Literal { literal, user_ty, const_id: _ } => {
let user_ty = user_ty.map(|user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
Expand Down
19 changes: 11 additions & 8 deletions compiler/rustc_mir_build/src/build/expr/as_operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
crate fn as_local_operand(
&mut self,
block: BasicBlock,
expr: &Expr<'_, 'tcx>,
expr: &Expr<'tcx>,
) -> BlockAnd<Operand<'tcx>> {
let local_scope = self.local_scope();
self.as_operand(block, Some(local_scope), expr)
Expand Down Expand Up @@ -74,7 +74,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
crate fn as_local_call_operand(
&mut self,
block: BasicBlock,
expr: &Expr<'_, 'tcx>,
expr: &Expr<'tcx>,
) -> BlockAnd<Operand<'tcx>> {
let local_scope = self.local_scope();
self.as_call_operand(block, Some(local_scope), expr)
Expand All @@ -93,16 +93,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
&mut self,
mut block: BasicBlock,
scope: Option<region::Scope>,
expr: &Expr<'_, 'tcx>,
expr: &Expr<'tcx>,
) -> BlockAnd<Operand<'tcx>> {
debug!("as_operand(block={:?}, expr={:?})", block, expr);
let this = self;

if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
let source_info = this.source_info(expr.span);
let region_scope = (region_scope, source_info);
return this
.in_scope(region_scope, lint_level, |this| this.as_operand(block, scope, value));
return this.in_scope(region_scope, lint_level, |this| {
this.as_operand(block, scope, &this.thir[value])
});
}

let category = Category::of(&expr.kind).unwrap();
Expand All @@ -123,7 +124,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
&mut self,
mut block: BasicBlock,
scope: Option<region::Scope>,
expr: &Expr<'_, 'tcx>,
expr: &Expr<'tcx>,
) -> BlockAnd<Operand<'tcx>> {
debug!("as_call_operand(block={:?}, expr={:?})", block, expr);
let this = self;
Expand All @@ -132,7 +133,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let source_info = this.source_info(expr.span);
let region_scope = (region_scope, source_info);
return this.in_scope(region_scope, lint_level, |this| {
this.as_call_operand(block, scope, value)
this.as_call_operand(block, scope, &this.thir[value])
});
}

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

// Return the operand *tmp0 to be used as the call argument
let place = Place {
Expand Down
40 changes: 25 additions & 15 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
crate fn as_place(
&mut self,
mut block: BasicBlock,
expr: &Expr<'_, 'tcx>,
expr: &Expr<'tcx>,
) -> BlockAnd<Place<'tcx>> {
let place_builder = unpack!(block = self.as_place_builder(block, expr));
block.and(place_builder.into_place(self.tcx, self.typeck_results))
Expand All @@ -392,7 +392,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
crate fn as_place_builder(
&mut self,
block: BasicBlock,
expr: &Expr<'_, 'tcx>,
expr: &Expr<'tcx>,
) -> BlockAnd<PlaceBuilder<'tcx>> {
self.expr_as_place(block, expr, Mutability::Mut, None)
}
Expand All @@ -405,7 +405,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
crate fn as_read_only_place(
&mut self,
mut block: BasicBlock,
expr: &Expr<'_, 'tcx>,
expr: &Expr<'tcx>,
) -> BlockAnd<Place<'tcx>> {
let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
block.and(place_builder.into_place(self.tcx, self.typeck_results))
Expand All @@ -420,15 +420,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fn as_read_only_place_builder(
&mut self,
block: BasicBlock,
expr: &Expr<'_, 'tcx>,
expr: &Expr<'tcx>,
) -> BlockAnd<PlaceBuilder<'tcx>> {
self.expr_as_place(block, expr, Mutability::Not, None)
}

fn expr_as_place(
&mut self,
mut block: BasicBlock,
expr: &Expr<'_, 'tcx>,
expr: &Expr<'tcx>,
mutability: Mutability,
fake_borrow_temps: Option<&mut Vec<Local>>,
) -> BlockAnd<PlaceBuilder<'tcx>> {
Expand All @@ -440,23 +440,27 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
match expr.kind {
ExprKind::Scope { region_scope, lint_level, value } => {
this.in_scope((region_scope, source_info), lint_level, |this| {
this.expr_as_place(block, value, mutability, fake_borrow_temps)
this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps)
})
}
ExprKind::Field { lhs, name } => {
let place_builder =
unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
let place_builder = unpack!(
block =
this.expr_as_place(block, &this.thir[lhs], mutability, fake_borrow_temps,)
);
block.and(place_builder.field(name, expr.ty))
}
ExprKind::Deref { arg } => {
let place_builder =
unpack!(block = this.expr_as_place(block, arg, mutability, fake_borrow_temps,));
let place_builder = unpack!(
block =
this.expr_as_place(block, &this.thir[arg], mutability, fake_borrow_temps,)
);
block.and(place_builder.deref())
}
ExprKind::Index { lhs, index } => this.lower_index_expression(
block,
lhs,
index,
&this.thir[lhs],
&this.thir[index],
mutability,
fake_borrow_temps,
expr.temp_lifetime,
Expand All @@ -481,7 +485,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

ExprKind::PlaceTypeAscription { source, user_ty } => {
let place_builder = unpack!(
block = this.expr_as_place(block, source, mutability, fake_borrow_temps,)
block = this.expr_as_place(
block,
&this.thir[source],
mutability,
fake_borrow_temps,
)
);
if let Some(user_ty) = user_ty {
let annotation_index =
Expand Down Expand Up @@ -509,6 +518,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(place_builder)
}
ExprKind::ValueTypeAscription { source, user_ty } => {
let source = &this.thir[source];
let temp =
unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
if let Some(user_ty) = user_ty {
Expand Down Expand Up @@ -613,8 +623,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fn lower_index_expression(
&mut self,
mut block: BasicBlock,
base: &Expr<'_, 'tcx>,
index: &Expr<'_, 'tcx>,
base: &Expr<'tcx>,
index: &Expr<'tcx>,
mutability: Mutability,
fake_borrow_temps: Option<&mut Vec<Local>>,
temp_lifetime: Option<region::Scope>,
Expand Down
Loading

0 comments on commit f94942d

Please sign in to comment.