Skip to content

Commit 937b3a4

Browse files
committed
introduce new type for required items of a MIR body
1 parent 95318b4 commit 937b3a4

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::ty::fold::{FallibleTypeFolder, TypeFoldable};
99
use crate::ty::print::{pretty_print_const, with_no_trimmed_paths};
1010
use crate::ty::print::{FmtPrinter, Printer};
1111
use crate::ty::visit::TypeVisitableExt;
12-
use crate::ty::{self, Instance, List, Ty, TyCtxt};
12+
use crate::ty::{self, List, Ty, TyCtxt};
1313
use crate::ty::{AdtDef, InstanceDef, UserTypeAnnotationIndex};
1414
use crate::ty::{GenericArg, GenericArgsRef};
1515

@@ -310,6 +310,13 @@ impl<'tcx> CoroutineInfo<'tcx> {
310310
}
311311
}
312312

313+
/// Some item that needs to monomorphize successfully for a MIR body to be considered well-formed.
314+
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
315+
pub enum RequiredItem<'tcx> {
316+
Fn(DefId, GenericArgsRef<'tcx>),
317+
Drop(Ty<'tcx>),
318+
}
319+
313320
/// The lowered representation of a single function.
314321
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
315322
pub struct Body<'tcx> {
@@ -375,8 +382,8 @@ pub struct Body<'tcx> {
375382
/// We hold in this field all the constants we are not able to evaluate yet.
376383
pub required_consts: Vec<ConstOperand<'tcx>>,
377384

378-
/// Functions that need to monomorphize successfully for this MIR to be well-formed.
379-
pub required_fns: Vec<Instance<'tcx>>,
385+
/// Further items that need to monomorphize successfully for this MIR to be well-formed.
386+
pub required_items: Vec<RequiredItem<'tcx>>,
380387

381388
/// Does this body use generic parameters. This is used for the `ConstEvaluatable` check.
382389
///
@@ -448,7 +455,7 @@ impl<'tcx> Body<'tcx> {
448455
var_debug_info,
449456
span,
450457
required_consts: Vec::new(),
451-
required_fns: Vec::new(),
458+
required_items: Vec::new(),
452459
is_polymorphic: false,
453460
injection_phase: None,
454461
tainted_by_errors,
@@ -477,7 +484,7 @@ impl<'tcx> Body<'tcx> {
477484
spread_arg: None,
478485
span: DUMMY_SP,
479486
required_consts: Vec::new(),
480-
required_fns: Vec::new(),
487+
required_items: Vec::new(),
481488
var_debug_info: Vec::new(),
482489
is_polymorphic: false,
483490
injection_phase: None,

compiler/rustc_mir_build/src/build/custom/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(super) fn build_custom_mir<'tcx>(
5656
var_debug_info: Vec::new(),
5757
span,
5858
required_consts: Vec::new(),
59-
required_fns: Vec::new(),
59+
required_items: Vec::new(),
6060
is_polymorphic: false,
6161
tainted_by_errors: None,
6262
injection_phase: None,

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,14 @@ fn mir_promoted(
344344
}
345345

346346
let mut required_consts = Vec::new();
347-
let mut required_fns = Vec::new();
347+
let mut required_items = Vec::new();
348348
let mut required_consts_visitor =
349-
RequiredConstsVisitor::new(tcx, &body, &mut required_consts, &mut required_fns);
349+
RequiredConstsVisitor::new(tcx, &body, &mut required_consts, &mut required_items);
350350
for (bb, bb_data) in traversal::reverse_postorder(&body) {
351351
required_consts_visitor.visit_basic_block_data(bb, bb_data);
352352
}
353353
body.required_consts = required_consts;
354-
body.required_fns = required_fns;
354+
body.required_items = required_items;
355355

356356
// What we need to run borrowck etc.
357357
let promote_pass = promote_consts::PromoteTemps::default();

compiler/rustc_mir_transform/src/required_consts.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
use rustc_hir::LangItem;
21
use rustc_middle::mir::visit::Visitor;
3-
use rustc_middle::mir::{self, Const, ConstOperand, Location};
4-
use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, TyCtxt};
2+
use rustc_middle::mir::{self, Const, ConstOperand, Location, RequiredItem};
3+
use rustc_middle::ty::{self, ConstKind, TyCtxt};
54

65
pub struct RequiredConstsVisitor<'a, 'tcx> {
76
tcx: TyCtxt<'tcx>,
87
body: &'a mir::Body<'tcx>,
98
required_consts: &'a mut Vec<ConstOperand<'tcx>>,
10-
required_fns: &'a mut Vec<Instance<'tcx>>,
9+
required_items: &'a mut Vec<RequiredItem<'tcx>>,
1110
}
1211

1312
impl<'a, 'tcx> RequiredConstsVisitor<'a, 'tcx> {
1413
pub fn new(
1514
tcx: TyCtxt<'tcx>,
1615
body: &'a mir::Body<'tcx>,
1716
required_consts: &'a mut Vec<ConstOperand<'tcx>>,
18-
required_fns: &'a mut Vec<Instance<'tcx>>,
17+
required_items: &'a mut Vec<RequiredItem<'tcx>>,
1918
) -> Self {
20-
RequiredConstsVisitor { tcx, body, required_consts, required_fns }
19+
RequiredConstsVisitor { tcx, body, required_consts, required_items }
2120
}
2221
}
2322

@@ -33,11 +32,8 @@ impl<'tcx> Visitor<'tcx> for RequiredConstsVisitor<'_, 'tcx> {
3332
Const::Val(_val, ty) => {
3433
// This is how function items get referenced: via zero-sized constants of `FnDef` type
3534
if let ty::FnDef(def_id, args) = ty.kind() {
36-
debug!("adding to required_fns: {def_id:?}");
37-
// FIXME maybe we shouldn't use `Instance`? We can't use `Instance::new`, it is
38-
// for codegen. But `Instance` feels like the right representation... Check what
39-
// the regular collector does.
40-
self.required_fns.push(Instance { def: InstanceDef::Item(*def_id), args });
35+
debug!("adding to required_items: {def_id:?}");
36+
self.required_items.push(RequiredItem::Fn(*def_id, args));
4137
}
4238
}
4339
}
@@ -51,11 +47,7 @@ impl<'tcx> Visitor<'tcx> for RequiredConstsVisitor<'_, 'tcx> {
5147
// `visit_constant`. But we do need to handle `Drop`.
5248
mir::TerminatorKind::Drop { place, .. } => {
5349
let ty = place.ty(self.body, self.tcx).ty;
54-
let def_id = self.tcx.require_lang_item(LangItem::DropInPlace, None);
55-
let args = self.tcx.mk_args(&[ty.into()]);
56-
// FIXME: same as above (we cannot use `Instance::resolve_drop_in_place` as this is
57-
// still generic).
58-
self.required_fns.push(Instance { def: InstanceDef::Item(def_id), args });
50+
self.required_items.push(RequiredItem::Drop(ty));
5951
}
6052
_ => {}
6153
}

0 commit comments

Comments
 (0)