Skip to content

Commit 2a2b4d7

Browse files
Pull thir::Cx out of the MIR Builder
1 parent 60def4d commit 2a2b4d7

File tree

17 files changed

+254
-329
lines changed

17 files changed

+254
-329
lines changed

compiler/rustc_mir_build/src/build/block.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
110110
let_scope_stack.push(remainder_scope);
111111

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

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

176175
// Then, the block may have an optional trailing expression which is a “return” value
177176
// of the block, which is stored into `destination`.
178-
let tcx = this.hir.tcx();
177+
let tcx = this.tcx;
179178
let destination_ty = destination.ty(&this.local_decls, tcx).ty;
180179
if let Some(expr) = expr {
181180
let tail_result_is_ignored =
@@ -195,7 +194,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
195194
if destination_ty.is_unit() {
196195
// We only want to assign an implicit `()` as the return value of the block if the
197196
// block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
198-
this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
197+
this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
199198
}
200199
}
201200
// Finally, we pop all the let scopes before exiting out from the scope of block
@@ -221,7 +220,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
221220
Safety::Safe => {}
222221
// no longer treat `unsafe fn`s as `unsafe` contexts (see RFC #2585)
223222
Safety::FnUnsafe
224-
if self.hir.tcx().lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, hir_id).0
223+
if self.tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, hir_id).0
225224
!= Level::Allow => {}
226225
_ => return,
227226
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
136136
});
137137
}
138138

139-
let tcx = this.hir.tcx();
139+
let tcx = this.tcx;
140140

141141
if tcx.features().unsized_fn_params {
142142
let ty = expr.ty;
143143
let span = expr.span;
144-
let param_env = this.hir.param_env;
144+
let param_env = this.param_env;
145145

146146
if !ty.is_sized(tcx.at(span), param_env) {
147147
// !sized means !copy, so this is an unsized move

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
353353
expr: &Expr<'tcx>,
354354
) -> BlockAnd<Place<'tcx>> {
355355
let place_builder = unpack!(block = self.as_place_builder(block, expr));
356-
block.and(place_builder.into_place(self.hir.tcx(), self.hir.typeck_results()))
356+
block.and(place_builder.into_place(self.tcx, self.typeck_results))
357357
}
358358

359359
/// This is used when constructing a compound `Place`, so that we can avoid creating
@@ -377,7 +377,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
377377
expr: &Expr<'tcx>,
378378
) -> BlockAnd<Place<'tcx>> {
379379
let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
380-
block.and(place_builder.into_place(self.hir.tcx(), self.hir.typeck_results()))
380+
block.and(place_builder.into_place(self.tcx, self.typeck_results))
381381
}
382382

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

465-
let place =
466-
place_builder.clone().into_place(this.hir.tcx(), this.hir.typeck_results());
465+
let place = place_builder.clone().into_place(this.tcx, this.typeck_results);
467466
this.cfg.push(
468467
block,
469468
Statement {
@@ -557,12 +556,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
557556
upvar_id: ty::UpvarId,
558557
) -> BlockAnd<PlaceBuilder<'tcx>> {
559558
let closure_ty = self
560-
.hir
561-
.typeck_results()
562-
.node_type(self.hir.tcx().hir().local_def_id_to_hir_id(upvar_id.closure_expr_id));
559+
.typeck_results
560+
.node_type(self.tcx.hir().local_def_id_to_hir_id(upvar_id.closure_expr_id));
563561

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

609607
block = self.bounds_check(
610608
block,
611-
base_place.clone().into_place(self.hir.tcx(), self.hir.typeck_results()),
609+
base_place.clone().into_place(self.tcx, self.typeck_results),
612610
idx,
613611
expr_span,
614612
source_info,
@@ -617,8 +615,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
617615
if is_outermost_index {
618616
self.read_fake_borrows(block, fake_borrow_temps, source_info)
619617
} else {
620-
base_place =
621-
base_place.expect_upvars_resolved(self.hir.tcx(), self.hir.typeck_results());
618+
base_place = base_place.expect_upvars_resolved(self.tcx, self.typeck_results);
622619
self.add_fake_borrows_of_base(
623620
&base_place,
624621
block,
@@ -639,8 +636,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
639636
expr_span: Span,
640637
source_info: SourceInfo,
641638
) -> BasicBlock {
642-
let usize_ty = self.hir.usize_ty();
643-
let bool_ty = self.hir.bool_ty();
639+
let usize_ty = self.tcx.types.usize;
640+
let bool_ty = self.tcx.types.bool;
644641
// bounds check:
645642
let len = self.temp(usize_ty, expr_span);
646643
let lt = self.temp(bool_ty, expr_span);
@@ -670,7 +667,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
670667
expr_span: Span,
671668
source_info: SourceInfo,
672669
) {
673-
let tcx = self.hir.tcx();
670+
let tcx = self.tcx;
674671
let local = match base_place.base {
675672
PlaceBase::Local(local) => local,
676673
PlaceBase::Upvar { .. } => bug!("Expected PlacseBase::Local found Upvar"),

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6161
ExprKind::Unary { op, arg } => {
6262
let arg = unpack!(block = this.as_operand(block, scope, &arg));
6363
// Check for -MIN on signed integers
64-
if this.hir.check_overflow() && *op == UnOp::Neg && expr.ty.is_signed() {
65-
let bool_ty = this.hir.bool_ty();
64+
if this.check_overflow && *op == UnOp::Neg && expr.ty.is_signed() {
65+
let bool_ty = this.tcx.types.bool;
6666

6767
let minval = this.minval_literal(expr_span, expr.ty);
6868
let is_min = this.temp(bool_ty, expr_span);
@@ -105,7 +105,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
105105
// initialize the box contents:
106106
unpack!(
107107
block = this.expr_into_dest(
108-
this.hir.tcx().mk_place_deref(Place::from(result)),
108+
this.tcx.mk_place_deref(Place::from(result)),
109109
block,
110110
&value
111111
)
@@ -148,7 +148,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
148148
// to the same MIR as `let x = ();`.
149149

150150
// first process the set of fields
151-
let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
151+
let el_ty = expr.ty.sequence_element_type(this.tcx);
152152
let fields: Vec<_> = fields
153153
.into_iter()
154154
.map(|f| unpack!(block = this.as_operand(block, scope, &f)))
@@ -221,7 +221,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
221221
block.and(Rvalue::Use(Operand::Constant(box Constant {
222222
span: expr_span,
223223
user_ty: None,
224-
literal: ty::Const::zero_sized(this.hir.tcx(), this.hir.tcx().types.unit),
224+
literal: ty::Const::zero_sized(this.tcx, this.tcx.types.unit),
225225
})))
226226
}
227227
ExprKind::Yield { .. }
@@ -273,9 +273,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
273273
rhs: Operand<'tcx>,
274274
) -> BlockAnd<Rvalue<'tcx>> {
275275
let source_info = self.source_info(span);
276-
let bool_ty = self.hir.bool_ty();
277-
if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
278-
let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty]);
276+
let bool_ty = self.tcx.types.bool;
277+
if self.check_overflow && op.is_checkable() && ty.is_integral() {
278+
let result_tup = self.tcx.intern_tup(&[ty, bool_ty]);
279279
let result_value = self.temp(result_tup, span);
280280

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

290-
let tcx = self.hir.tcx();
290+
let tcx = self.tcx;
291291
let val = tcx.mk_place_field(result_value, val_fld, ty);
292292
let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
293293

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

394394
match enclosing_upvars_resolved.as_ref() {
395395
PlaceRef {
@@ -426,13 +426,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
426426
Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
427427
};
428428

429-
let arg_place = arg_place_builder.into_place(this.hir.tcx(), this.hir.typeck_results());
429+
let arg_place = arg_place_builder.into_place(this.tcx, this.typeck_results);
430430

431431
this.cfg.push_assign(
432432
block,
433433
source_info,
434434
Place::from(temp),
435-
Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, borrow_kind, arg_place),
435+
Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place),
436436
);
437437

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

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

465465
self.literal_operand(span, literal)
466466
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5959
}
6060
match expr.kind {
6161
ExprKind::StaticRef { def_id, .. } => {
62-
assert!(!this.hir.tcx().is_thread_local_static(def_id));
62+
assert!(!this.tcx.is_thread_local_static(def_id));
6363
local_decl.internal = true;
6464
local_decl.local_info =
6565
Some(box LocalInfo::StaticRef { def_id, is_thread_local: false });
6666
}
6767
ExprKind::ThreadLocalRef(def_id) => {
68-
assert!(this.hir.tcx().is_thread_local_static(def_id));
68+
assert!(this.tcx.is_thread_local_static(def_id));
6969
local_decl.internal = true;
7070
local_decl.local_info =
7171
Some(box LocalInfo::StaticRef { def_id, is_thread_local: true });

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use rustc_ast::InlineAsmOptions;
77
use rustc_data_structures::fx::FxHashMap;
88
use rustc_data_structures::stack::ensure_sufficient_stack;
99
use rustc_hir as hir;
10+
use rustc_index::vec::Idx;
1011
use rustc_middle::mir::*;
11-
use rustc_middle::ty::CanonicalUserTypeAnnotation;
12+
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation};
1213

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

5960
let mut then_block = this.cfg.start_new_block();
6061
let mut else_block = this.cfg.start_new_block();
61-
let term = TerminatorKind::if_(this.hir.tcx(), operand, then_block, else_block);
62+
let term = TerminatorKind::if_(this.tcx, operand, then_block, else_block);
6263
this.cfg.terminate(block, source_info, term);
6364

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

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

138139
let rhs = unpack!(else_block = this.as_local_operand(else_block, &rhs));
139-
let term = TerminatorKind::if_(this.hir.tcx(), rhs, true_block, false_block);
140+
let term = TerminatorKind::if_(this.tcx, rhs, true_block, false_block);
140141
this.cfg.terminate(else_block, source_info, term);
141142

142143
this.cfg.push_assign_constant(
143144
true_block,
144145
source_info,
145146
destination,
146-
Constant { span: expr_span, user_ty: None, literal: this.hir.true_literal() },
147+
Constant {
148+
span: expr_span,
149+
user_ty: None,
150+
literal: ty::Const::from_bool(this.tcx, true),
151+
},
147152
);
148153

149154
this.cfg.push_assign_constant(
150155
false_block,
151156
source_info,
152157
destination,
153-
Constant { span: expr_span, user_ty: None, literal: this.hir.false_literal() },
158+
Constant {
159+
span: expr_span,
160+
user_ty: None,
161+
literal: ty::Const::from_bool(this.tcx, false),
162+
},
154163
);
155164

156165
// Link up both branches:
@@ -241,8 +250,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
241250
BorrowKind::Shared => unpack!(block = this.as_read_only_place(block, &arg)),
242251
_ => unpack!(block = this.as_place(block, &arg)),
243252
};
244-
let borrow =
245-
Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, *borrow_kind, arg_place);
253+
let borrow = Rvalue::Ref(this.tcx.lifetimes.re_erased, *borrow_kind, arg_place);
246254
this.cfg.push_assign(block, source_info, destination, borrow);
247255
block.unit()
248256
}
@@ -272,7 +280,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
272280
})
273281
.collect();
274282

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

277286
let fields: Vec<_> = if let Some(FruInfo { base, field_types }) = base {
278287
let place_builder = unpack!(block = this.as_place_builder(block, &base));
@@ -290,7 +299,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
290299
this.consume_by_copy_or_move(
291300
place_builder
292301
.field(n, ty)
293-
.into_place(this.hir.tcx(), this.hir.typeck_results()),
302+
.into_place(this.tcx, this.typeck_results),
294303
)
295304
}
296305
})
@@ -398,7 +407,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
398407
| ExprKind::AssignOp { .. }
399408
| ExprKind::LlvmInlineAsm { .. } => {
400409
unpack!(block = this.stmt_expr(block, expr, None));
401-
this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
410+
this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
402411
block.unit()
403412
}
404413

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4040

4141
// Generate better code for things that don't need to be
4242
// dropped.
43-
if this.hir.needs_drop(lhs.ty) {
43+
if lhs.ty.needs_drop(this.tcx, this.param_env) {
4444
let rhs = unpack!(block = this.as_local_operand(block, &rhs));
4545
let lhs = unpack!(block = this.as_place(block, &lhs));
4646
unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));

0 commit comments

Comments
 (0)