Skip to content

Commit

Permalink
Pull thir::Cx out of the MIR Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed Mar 9, 2021
1 parent 60def4d commit 2a2b4d7
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 329 deletions.
9 changes: 4 additions & 5 deletions compiler/rustc_mir_build/src/build/block.rs
Expand Up @@ -110,8 +110,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let_scope_stack.push(remainder_scope);

// Declare the bindings, which may create a source scope.
let remainder_span =
remainder_scope.span(this.hir.tcx(), &this.hir.region_scope_tree);
let remainder_span = remainder_scope.span(this.tcx, &this.region_scope_tree);

let visibility_scope =
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
Expand Down Expand Up @@ -175,7 +174,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

// Then, the block may have an optional trailing expression which is a “return” value
// of the block, which is stored into `destination`.
let tcx = this.hir.tcx();
let tcx = this.tcx;
let destination_ty = destination.ty(&this.local_decls, tcx).ty;
if let Some(expr) = expr {
let tail_result_is_ignored =
Expand All @@ -195,7 +194,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if destination_ty.is_unit() {
// We only want to assign an implicit `()` as the return value of the block if the
// block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
}
}
// Finally, we pop all the let scopes before exiting out from the scope of block
Expand All @@ -221,7 +220,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Safety::Safe => {}
// no longer treat `unsafe fn`s as `unsafe` contexts (see RFC #2585)
Safety::FnUnsafe
if self.hir.tcx().lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, hir_id).0
if self.tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, hir_id).0
!= Level::Allow => {}
_ => return,
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/build/expr/as_operand.rs
Expand Up @@ -136,12 +136,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
});
}

let tcx = this.hir.tcx();
let tcx = this.tcx;

if tcx.features().unsized_fn_params {
let ty = expr.ty;
let span = expr.span;
let param_env = this.hir.param_env;
let param_env = this.param_env;

if !ty.is_sized(tcx.at(span), param_env) {
// !sized means !copy, so this is an unsized move
Expand Down
25 changes: 11 additions & 14 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Expand Up @@ -353,7 +353,7 @@ impl<'a, 'tcx> Builder<'a, '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.hir.tcx(), self.hir.typeck_results()))
block.and(place_builder.into_place(self.tcx, self.typeck_results))
}

/// This is used when constructing a compound `Place`, so that we can avoid creating
Expand All @@ -377,7 +377,7 @@ impl<'a, 'tcx> Builder<'a, '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.hir.tcx(), self.hir.typeck_results()))
block.and(place_builder.into_place(self.tcx, self.typeck_results))
}

/// This is used when constructing a compound `Place`, so that we can avoid creating
Expand Down Expand Up @@ -462,8 +462,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
inferred_ty: expr.ty,
});

let place =
place_builder.clone().into_place(this.hir.tcx(), this.hir.typeck_results());
let place = place_builder.clone().into_place(this.tcx, this.typeck_results);
this.cfg.push(
block,
Statement {
Expand Down Expand Up @@ -557,12 +556,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
upvar_id: ty::UpvarId,
) -> BlockAnd<PlaceBuilder<'tcx>> {
let closure_ty = self
.hir
.typeck_results()
.node_type(self.hir.tcx().hir().local_def_id_to_hir_id(upvar_id.closure_expr_id));
.typeck_results
.node_type(self.tcx.hir().local_def_id_to_hir_id(upvar_id.closure_expr_id));

let closure_kind = if let ty::Closure(_, closure_substs) = closure_ty.kind() {
self.hir.infcx().closure_kind(closure_substs).unwrap()
self.infcx.closure_kind(closure_substs).unwrap()
} else {
// Generators are considered FnOnce.
ty::ClosureKind::FnOnce
Expand Down Expand Up @@ -608,7 +606,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

block = self.bounds_check(
block,
base_place.clone().into_place(self.hir.tcx(), self.hir.typeck_results()),
base_place.clone().into_place(self.tcx, self.typeck_results),
idx,
expr_span,
source_info,
Expand All @@ -617,8 +615,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if is_outermost_index {
self.read_fake_borrows(block, fake_borrow_temps, source_info)
} else {
base_place =
base_place.expect_upvars_resolved(self.hir.tcx(), self.hir.typeck_results());
base_place = base_place.expect_upvars_resolved(self.tcx, self.typeck_results);
self.add_fake_borrows_of_base(
&base_place,
block,
Expand All @@ -639,8 +636,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr_span: Span,
source_info: SourceInfo,
) -> BasicBlock {
let usize_ty = self.hir.usize_ty();
let bool_ty = self.hir.bool_ty();
let usize_ty = self.tcx.types.usize;
let bool_ty = self.tcx.types.bool;
// bounds check:
let len = self.temp(usize_ty, expr_span);
let lt = self.temp(bool_ty, expr_span);
Expand Down Expand Up @@ -670,7 +667,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr_span: Span,
source_info: SourceInfo,
) {
let tcx = self.hir.tcx();
let tcx = self.tcx;
let local = match base_place.base {
PlaceBase::Local(local) => local,
PlaceBase::Upvar { .. } => bug!("Expected PlacseBase::Local found Upvar"),
Expand Down
32 changes: 16 additions & 16 deletions compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Expand Up @@ -61,8 +61,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ExprKind::Unary { op, arg } => {
let arg = unpack!(block = this.as_operand(block, scope, &arg));
// Check for -MIN on signed integers
if this.hir.check_overflow() && *op == UnOp::Neg && expr.ty.is_signed() {
let bool_ty = this.hir.bool_ty();
if this.check_overflow && *op == UnOp::Neg && expr.ty.is_signed() {
let bool_ty = this.tcx.types.bool;

let minval = this.minval_literal(expr_span, expr.ty);
let is_min = this.temp(bool_ty, expr_span);
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// initialize the box contents:
unpack!(
block = this.expr_into_dest(
this.hir.tcx().mk_place_deref(Place::from(result)),
this.tcx.mk_place_deref(Place::from(result)),
block,
&value
)
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// to the same MIR as `let x = ();`.

// first process the set of fields
let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
let el_ty = expr.ty.sequence_element_type(this.tcx);
let fields: Vec<_> = fields
.into_iter()
.map(|f| unpack!(block = this.as_operand(block, scope, &f)))
Expand Down Expand Up @@ -221,7 +221,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(Rvalue::Use(Operand::Constant(box Constant {
span: expr_span,
user_ty: None,
literal: ty::Const::zero_sized(this.hir.tcx(), this.hir.tcx().types.unit),
literal: ty::Const::zero_sized(this.tcx, this.tcx.types.unit),
})))
}
ExprKind::Yield { .. }
Expand Down Expand Up @@ -273,9 +273,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
rhs: Operand<'tcx>,
) -> BlockAnd<Rvalue<'tcx>> {
let source_info = self.source_info(span);
let bool_ty = self.hir.bool_ty();
if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty]);
let bool_ty = self.tcx.types.bool;
if self.check_overflow && op.is_checkable() && ty.is_integral() {
let result_tup = self.tcx.intern_tup(&[ty, bool_ty]);
let result_value = self.temp(result_tup, span);

self.cfg.push_assign(
Expand All @@ -287,7 +287,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let val_fld = Field::new(0);
let of_fld = Field::new(1);

let tcx = self.hir.tcx();
let tcx = self.tcx;
let val = tcx.mk_place_field(result_value, val_fld, ty);
let of = tcx.mk_place_field(result_value, of_fld, bool_ty);

Expand Down Expand Up @@ -389,7 +389,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// is same as that of the capture in the parent closure.
PlaceBase::Upvar { .. } => {
let enclosing_upvars_resolved =
arg_place_builder.clone().into_place(this.hir.tcx(), this.hir.typeck_results());
arg_place_builder.clone().into_place(this.tcx, this.typeck_results);

match enclosing_upvars_resolved.as_ref() {
PlaceRef {
Expand Down Expand Up @@ -426,13 +426,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
};

let arg_place = arg_place_builder.into_place(this.hir.tcx(), this.hir.typeck_results());
let arg_place = arg_place_builder.into_place(this.tcx, this.typeck_results);

this.cfg.push_assign(
block,
source_info,
Place::from(temp),
Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, borrow_kind, arg_place),
Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place),
);

// See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
Expand All @@ -447,9 +447,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Helper to get a `-1` value of the appropriate type
fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
let param_ty = ty::ParamEnv::empty().and(ty);
let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
let n = (!0u128) >> (128 - bits);
let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
let literal = ty::Const::from_bits(self.tcx, n, param_ty);

self.literal_operand(span, literal)
}
Expand All @@ -458,9 +458,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
assert!(ty.is_signed());
let param_ty = ty::ParamEnv::empty().and(ty);
let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
let n = 1 << (bits - 1);
let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
let literal = ty::Const::from_bits(self.tcx, n, param_ty);

self.literal_operand(span, literal)
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/build/expr/as_temp.rs
Expand Up @@ -59,13 +59,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
match expr.kind {
ExprKind::StaticRef { def_id, .. } => {
assert!(!this.hir.tcx().is_thread_local_static(def_id));
assert!(!this.tcx.is_thread_local_static(def_id));
local_decl.internal = true;
local_decl.local_info =
Some(box LocalInfo::StaticRef { def_id, is_thread_local: false });
}
ExprKind::ThreadLocalRef(def_id) => {
assert!(this.hir.tcx().is_thread_local_static(def_id));
assert!(this.tcx.is_thread_local_static(def_id));
local_decl.internal = true;
local_decl.local_info =
Some(box LocalInfo::StaticRef { def_id, is_thread_local: true });
Expand Down
33 changes: 21 additions & 12 deletions compiler/rustc_mir_build/src/build/expr/into.rs
Expand Up @@ -7,8 +7,9 @@ use rustc_ast::InlineAsmOptions;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir;
use rustc_index::vec::Idx;
use rustc_middle::mir::*;
use rustc_middle::ty::CanonicalUserTypeAnnotation;
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation};

impl<'a, 'tcx> Builder<'a, 'tcx> {
/// Compile `expr`, storing the result into `destination`, which
Expand Down Expand Up @@ -58,7 +59,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

let mut then_block = this.cfg.start_new_block();
let mut else_block = this.cfg.start_new_block();
let term = TerminatorKind::if_(this.hir.tcx(), operand, then_block, else_block);
let term = TerminatorKind::if_(this.tcx, operand, then_block, else_block);
this.cfg.terminate(block, source_info, term);

unpack!(then_block = this.expr_into_dest(destination, then_block, &then));
Expand All @@ -68,7 +69,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Body of the `if` expression without an `else` clause must return `()`, thus
// we implicitly generate a `else {}` if it is not specified.
let correct_si = this.source_info(expr_span.shrink_to_hi());
this.cfg.push_assign_unit(else_block, correct_si, destination, this.hir.tcx());
this.cfg.push_assign_unit(else_block, correct_si, destination, this.tcx);
else_block
};

Expand Down Expand Up @@ -132,25 +133,33 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
LogicalOp::And => (else_block, false_block),
LogicalOp::Or => (true_block, else_block),
};
let term = TerminatorKind::if_(this.hir.tcx(), lhs, blocks.0, blocks.1);
let term = TerminatorKind::if_(this.tcx, lhs, blocks.0, blocks.1);
this.cfg.terminate(block, source_info, term);

let rhs = unpack!(else_block = this.as_local_operand(else_block, &rhs));
let term = TerminatorKind::if_(this.hir.tcx(), rhs, true_block, false_block);
let term = TerminatorKind::if_(this.tcx, rhs, true_block, false_block);
this.cfg.terminate(else_block, source_info, term);

this.cfg.push_assign_constant(
true_block,
source_info,
destination,
Constant { span: expr_span, user_ty: None, literal: this.hir.true_literal() },
Constant {
span: expr_span,
user_ty: None,
literal: ty::Const::from_bool(this.tcx, true),
},
);

this.cfg.push_assign_constant(
false_block,
source_info,
destination,
Constant { span: expr_span, user_ty: None, literal: this.hir.false_literal() },
Constant {
span: expr_span,
user_ty: None,
literal: ty::Const::from_bool(this.tcx, false),
},
);

// Link up both branches:
Expand Down Expand Up @@ -241,8 +250,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
BorrowKind::Shared => unpack!(block = this.as_read_only_place(block, &arg)),
_ => unpack!(block = this.as_place(block, &arg)),
};
let borrow =
Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, *borrow_kind, arg_place);
let borrow = Rvalue::Ref(this.tcx.lifetimes.re_erased, *borrow_kind, arg_place);
this.cfg.push_assign(block, source_info, destination, borrow);
block.unit()
}
Expand Down Expand Up @@ -272,7 +280,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
})
.collect();

let field_names = this.hir.all_fields(adt_def, *variant_index);
let field_names: Vec<_> =
(0..adt_def.variants[*variant_index].fields.len()).map(Field::new).collect();

let fields: Vec<_> = if let Some(FruInfo { base, field_types }) = base {
let place_builder = unpack!(block = this.as_place_builder(block, &base));
Expand All @@ -290,7 +299,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.consume_by_copy_or_move(
place_builder
.field(n, ty)
.into_place(this.hir.tcx(), this.hir.typeck_results()),
.into_place(this.tcx, this.typeck_results),
)
}
})
Expand Down Expand Up @@ -398,7 +407,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
| ExprKind::AssignOp { .. }
| ExprKind::LlvmInlineAsm { .. } => {
unpack!(block = this.stmt_expr(block, expr, None));
this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
block.unit()
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/expr/stmt.rs
Expand Up @@ -40,7 +40,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

// Generate better code for things that don't need to be
// dropped.
if this.hir.needs_drop(lhs.ty) {
if lhs.ty.needs_drop(this.tcx, this.param_env) {
let rhs = unpack!(block = this.as_local_operand(block, &rhs));
let lhs = unpack!(block = this.as_place(block, &lhs));
unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
Expand Down

0 comments on commit 2a2b4d7

Please sign in to comment.