Skip to content

Commit de111e6

Browse files
committed
Auto merge of #58103 - RalfJung:mir-shim-dump, r=eddyb
Make -Zdump-mir dump shims Fixes #53532 by (a) making the MIR shim generation use the MIR pass infrastructure, and (b) fixing said infrastructure to handle the fallout. Cc @eddyb @oli-obk
2 parents 2e08bb1 + 544b3a1 commit de111e6

28 files changed

+173
-130
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,19 @@ impl<'hir> Map<'hir> {
371371
let def_id = self.local_def_id(variant.node.data.id());
372372
Some(Def::Variant(def_id))
373373
}
374-
Node::Field(_) |
374+
Node::StructCtor(variant) => {
375+
let def_id = self.local_def_id(variant.id());
376+
Some(Def::StructCtor(def_id, def::CtorKind::from_hir(variant)))
377+
}
375378
Node::AnonConst(_) |
379+
Node::Field(_) |
376380
Node::Expr(_) |
377381
Node::Stmt(_) |
378382
Node::PathSegment(_) |
379383
Node::Ty(_) |
380384
Node::TraitRef(_) |
381385
Node::Pat(_) |
382386
Node::Binding(_) |
383-
Node::StructCtor(_) |
384387
Node::Lifetime(_) |
385388
Node::Visibility(_) |
386389
Node::Block(_) |

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
209209

210210
fn dump_mir_results<'a, 'gcx, 'tcx>(
211211
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
212-
source: MirSource,
212+
source: MirSource<'tcx>,
213213
mir: &Mir<'tcx>,
214214
regioncx: &RegionInferenceContext<'_>,
215215
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,8 +2427,13 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
24272427
pub struct TypeckMir;
24282428

24292429
impl MirPass for TypeckMir {
2430-
fn run_pass<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &mut Mir<'tcx>) {
2431-
let def_id = src.def_id;
2430+
fn run_pass<'a, 'tcx>(
2431+
&self,
2432+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
2433+
src: MirSource<'tcx>,
2434+
mir: &mut Mir<'tcx>,
2435+
) {
2436+
let def_id = src.def_id();
24322437
debug!("run_pass: {:?}", def_id);
24332438

24342439
// When NLL is enabled, the borrow checker runs the typeck

src/librustc_mir/shim.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ use syntax_pos::Span;
1616
use std::fmt;
1717
use std::iter;
1818

19-
use crate::transform::{add_moves_for_packed_drops, add_call_guards};
20-
use crate::transform::{remove_noop_landing_pads, no_landing_pads, simplify};
19+
use crate::transform::{
20+
add_moves_for_packed_drops, add_call_guards,
21+
remove_noop_landing_pads, no_landing_pads, simplify, run_passes
22+
};
2123
use crate::util::elaborate_drops::{self, DropElaborator, DropStyle, DropFlagMode};
2224
use crate::util::patch::MirPatch;
2325

@@ -113,12 +115,15 @@ fn make_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
113115
}
114116
};
115117
debug!("make_shim({:?}) = untransformed {:?}", instance, result);
116-
add_moves_for_packed_drops::add_moves_for_packed_drops(
117-
tcx, &mut result, instance.def_id());
118-
no_landing_pads::no_landing_pads(tcx, &mut result);
119-
remove_noop_landing_pads::remove_noop_landing_pads(tcx, &mut result);
120-
simplify::simplify_cfg(&mut result);
121-
add_call_guards::CriticalCallEdges.add_call_guards(&mut result);
118+
119+
run_passes(tcx, &mut result, instance, MirPhase::Const, &[
120+
&add_moves_for_packed_drops::AddMovesForPackedDrops,
121+
&no_landing_pads::NoLandingPads,
122+
&remove_noop_landing_pads::RemoveNoopLandingPads,
123+
&simplify::SimplifyCfg::new("make_shim"),
124+
&add_call_guards::CriticalCallEdges,
125+
]);
126+
122127
debug!("make_shim({:?}) = {:?}", instance, result);
123128

124129
tcx.alloc_mir(result)

src/librustc_mir/transform/add_call_guards.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use self::AddCallGuards::*;
3333
impl MirPass for AddCallGuards {
3434
fn run_pass<'a, 'tcx>(&self,
3535
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
36-
_src: MirSource,
36+
_src: MirSource<'tcx>,
3737
mir: &mut Mir<'tcx>) {
3838
self.add_call_guards(mir);
3939
}

src/librustc_mir/transform/add_moves_for_packed_drops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ pub struct AddMovesForPackedDrops;
4242
impl MirPass for AddMovesForPackedDrops {
4343
fn run_pass<'a, 'tcx>(&self,
4444
tcx: TyCtxt<'a, 'tcx, 'tcx>,
45-
src: MirSource,
45+
src: MirSource<'tcx>,
4646
mir: &mut Mir<'tcx>)
4747
{
4848
debug!("add_moves_for_packed_drops({:?} @ {:?})", src, mir.span);
49-
add_moves_for_packed_drops(tcx, mir, src.def_id);
49+
add_moves_for_packed_drops(tcx, mir, src.def_id());
5050
}
5151
}
5252

src/librustc_mir/transform/add_retag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn may_have_reference<'a, 'gcx, 'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>)
7777
impl MirPass for AddRetag {
7878
fn run_pass<'a, 'tcx>(&self,
7979
tcx: TyCtxt<'a, 'tcx, 'tcx>,
80-
_src: MirSource,
80+
_src: MirSource<'tcx>,
8181
mir: &mut Mir<'tcx>)
8282
{
8383
if !tcx.sess.opts.debugging_opts.mir_emit_retag {

src/librustc_mir/transform/cleanup_post_borrowck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct DeleteAscribeUserType;
3535
impl MirPass for CleanAscribeUserType {
3636
fn run_pass<'a, 'tcx>(&self,
3737
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
38-
_source: MirSource,
38+
_source: MirSource<'tcx>,
3939
mir: &mut Mir<'tcx>) {
4040
let mut delete = DeleteAscribeUserType;
4141
delete.visit_mir(mir);
@@ -69,7 +69,7 @@ pub struct DeleteFakeBorrows {
6969
impl MirPass for CleanFakeReadsAndBorrows {
7070
fn run_pass<'a, 'tcx>(&self,
7171
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
72-
_source: MirSource,
72+
_source: MirSource<'tcx>,
7373
mir: &mut Mir<'tcx>) {
7474
let mut delete_reads = DeleteAndRecordFakeReads::default();
7575
delete_reads.visit_mir(mir);

src/librustc_mir/transform/const_prop.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,31 @@ pub struct ConstProp;
3030
impl MirPass for ConstProp {
3131
fn run_pass<'a, 'tcx>(&self,
3232
tcx: TyCtxt<'a, 'tcx, 'tcx>,
33-
source: MirSource,
33+
source: MirSource<'tcx>,
3434
mir: &mut Mir<'tcx>) {
3535
// will be evaluated by miri and produce its errors there
3636
if source.promoted.is_some() {
3737
return;
3838
}
3939

4040
use rustc::hir::map::blocks::FnLikeNode;
41-
let node_id = tcx.hir().as_local_node_id(source.def_id)
41+
let node_id = tcx.hir().as_local_node_id(source.def_id())
4242
.expect("Non-local call to local provider is_const_fn");
4343

4444
let is_fn_like = FnLikeNode::from_node(tcx.hir().get(node_id)).is_some();
45-
let is_assoc_const = match tcx.describe_def(source.def_id) {
45+
let is_assoc_const = match tcx.describe_def(source.def_id()) {
4646
Some(Def::AssociatedConst(_)) => true,
4747
_ => false,
4848
};
4949

5050
// Only run const prop on functions, methods, closures and associated constants
5151
if !is_fn_like && !is_assoc_const {
5252
// skip anon_const/statics/consts because they'll be evaluated by miri anyway
53-
trace!("ConstProp skipped for {:?}", source.def_id);
53+
trace!("ConstProp skipped for {:?}", source.def_id());
5454
return
5555
}
5656

57-
trace!("ConstProp starting for {:?}", source.def_id);
57+
trace!("ConstProp starting for {:?}", source.def_id());
5858

5959
// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
6060
// constants, instead of just checking for const-folding succeeding.
@@ -63,7 +63,7 @@ impl MirPass for ConstProp {
6363
let mut optimization_finder = ConstPropagator::new(mir, tcx, source);
6464
optimization_finder.visit_mir(mir);
6565

66-
trace!("ConstProp done for {:?}", source.def_id);
66+
trace!("ConstProp done for {:?}", source.def_id());
6767
}
6868
}
6969

@@ -74,7 +74,7 @@ struct ConstPropagator<'a, 'mir, 'tcx:'a+'mir> {
7474
ecx: EvalContext<'a, 'mir, 'tcx, CompileTimeInterpreter<'a, 'mir, 'tcx>>,
7575
mir: &'mir Mir<'tcx>,
7676
tcx: TyCtxt<'a, 'tcx, 'tcx>,
77-
source: MirSource,
77+
source: MirSource<'tcx>,
7878
places: IndexVec<Local, Option<Const<'tcx>>>,
7979
can_const_prop: IndexVec<Local, bool>,
8080
param_env: ParamEnv<'tcx>,
@@ -107,10 +107,10 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
107107
fn new(
108108
mir: &'mir Mir<'tcx>,
109109
tcx: TyCtxt<'a, 'tcx, 'tcx>,
110-
source: MirSource,
110+
source: MirSource<'tcx>,
111111
) -> ConstPropagator<'a, 'mir, 'tcx> {
112-
let param_env = tcx.param_env(source.def_id);
113-
let ecx = mk_eval_cx(tcx, tcx.def_span(source.def_id), param_env);
112+
let param_env = tcx.param_env(source.def_id());
113+
let ecx = mk_eval_cx(tcx, tcx.def_span(source.def_id()), param_env);
114114
ConstPropagator {
115115
ecx,
116116
mir,
@@ -284,13 +284,13 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
284284
_ => None,
285285
},
286286
Place::Promoted(ref promoted) => {
287-
let generics = self.tcx.generics_of(self.source.def_id);
287+
let generics = self.tcx.generics_of(self.source.def_id());
288288
if generics.requires_monomorphization(self.tcx) {
289289
// FIXME: can't handle code with generics
290290
return None;
291291
}
292-
let substs = Substs::identity_for_item(self.tcx, self.source.def_id);
293-
let instance = Instance::new(self.source.def_id, substs);
292+
let substs = Substs::identity_for_item(self.tcx, self.source.def_id());
293+
let instance = Instance::new(self.source.def_id(), substs);
294294
let cid = GlobalId {
295295
instance,
296296
promoted: Some(promoted.0),
@@ -358,10 +358,10 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
358358
)))
359359
}
360360
Rvalue::UnaryOp(op, ref arg) => {
361-
let def_id = if self.tcx.is_closure(self.source.def_id) {
362-
self.tcx.closure_base_def_id(self.source.def_id)
361+
let def_id = if self.tcx.is_closure(self.source.def_id()) {
362+
self.tcx.closure_base_def_id(self.source.def_id())
363363
} else {
364-
self.source.def_id
364+
self.source.def_id()
365365
};
366366
let generics = self.tcx.generics_of(def_id);
367367
if generics.requires_monomorphization(self.tcx) {
@@ -398,10 +398,10 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
398398
Rvalue::BinaryOp(op, ref left, ref right) => {
399399
trace!("rvalue binop {:?} for {:?} and {:?}", op, left, right);
400400
let right = self.eval_operand(right, source_info)?;
401-
let def_id = if self.tcx.is_closure(self.source.def_id) {
402-
self.tcx.closure_base_def_id(self.source.def_id)
401+
let def_id = if self.tcx.is_closure(self.source.def_id()) {
402+
self.tcx.closure_base_def_id(self.source.def_id())
403403
} else {
404-
self.source.def_id
404+
self.source.def_id()
405405
};
406406
let generics = self.tcx.generics_of(def_id);
407407
if generics.requires_monomorphization(self.tcx) {
@@ -608,7 +608,7 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
608608
let node_id = self
609609
.tcx
610610
.hir()
611-
.as_local_node_id(self.source.def_id)
611+
.as_local_node_id(self.source.def_id())
612612
.expect("some part of a failing const eval must be local");
613613
use rustc::mir::interpret::EvalErrorKind::*;
614614
let msg = match msg {

src/librustc_mir/transform/copy_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct CopyPropagation;
3030
impl MirPass for CopyPropagation {
3131
fn run_pass<'a, 'tcx>(&self,
3232
tcx: TyCtxt<'a, 'tcx, 'tcx>,
33-
_source: MirSource,
33+
_source: MirSource<'tcx>,
3434
mir: &mut Mir<'tcx>) {
3535
// We only run when the MIR optimization level is > 1.
3636
// This avoids a slow pass, and messing up debug info.

0 commit comments

Comments
 (0)