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

+5-2
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

+1-1
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

+7-2
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

+13-8
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

+1-1
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

+2-2
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

+1-1
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

+2-2
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

+20-20
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

+1-1
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.

src/librustc_mir/transform/deaggregator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub struct Deaggregator;
88
impl MirPass for Deaggregator {
99
fn run_pass<'a, 'tcx>(&self,
1010
tcx: TyCtxt<'a, 'tcx, 'tcx>,
11-
_source: MirSource,
11+
_source: MirSource<'tcx>,
1212
mir: &mut Mir<'tcx>) {
1313
let (basic_blocks, local_decls) = mir.basic_blocks_and_local_decls_mut();
1414
let local_decls = &*local_decls;

src/librustc_mir/transform/dump_mir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl MirPass for Marker {
2020

2121
fn run_pass<'a, 'tcx>(&self,
2222
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
23-
_source: MirSource,
23+
_source: MirSource<'tcx>,
2424
_mir: &mut Mir<'tcx>)
2525
{
2626
}
@@ -41,7 +41,7 @@ impl fmt::Display for Disambiguator {
4141
pub fn on_mir_pass<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
4242
pass_num: &dyn fmt::Display,
4343
pass_name: &str,
44-
source: MirSource,
44+
source: MirSource<'tcx>,
4545
mir: &Mir<'tcx>,
4646
is_after: bool) {
4747
if mir_util::dump_enabled(tcx, pass_name, source) {

src/librustc_mir/transform/elaborate_drops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ pub struct ElaborateDrops;
2323
impl MirPass for ElaborateDrops {
2424
fn run_pass<'a, 'tcx>(&self,
2525
tcx: TyCtxt<'a, 'tcx, 'tcx>,
26-
src: MirSource,
26+
src: MirSource<'tcx>,
2727
mir: &mut Mir<'tcx>)
2828
{
2929
debug!("elaborate_drops({:?} @ {:?})", src, mir.span);
3030

31-
let id = tcx.hir().as_local_node_id(src.def_id).unwrap();
32-
let param_env = tcx.param_env(src.def_id).with_reveal_all();
31+
let id = tcx.hir().as_local_node_id(src.def_id()).unwrap();
32+
let param_env = tcx.param_env(src.def_id()).with_reveal_all();
3333
let move_data = match MoveData::gather_moves(mir, tcx) {
3434
Ok(move_data) => move_data,
3535
Err((move_data, _move_errors)) => {

src/librustc_mir/transform/erase_regions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct EraseRegions;
5353
impl MirPass for EraseRegions {
5454
fn run_pass<'a, 'tcx>(&self,
5555
tcx: TyCtxt<'a, 'tcx, 'tcx>,
56-
_: MirSource,
56+
_: MirSource<'tcx>,
5757
mir: &mut Mir<'tcx>) {
5858
EraseRegionsVisitor::new(tcx).visit_mir(mir);
5959
}

src/librustc_mir/transform/generator.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,14 @@ impl<'tcx> Visitor<'tcx> for StorageIgnored {
376376
fn locals_live_across_suspend_points(
377377
tcx: TyCtxt<'a, 'tcx, 'tcx>,
378378
mir: &Mir<'tcx>,
379-
source: MirSource,
379+
source: MirSource<'tcx>,
380380
movable: bool,
381381
) -> (
382382
liveness::LiveVarSet<Local>,
383383
FxHashMap<BasicBlock, liveness::LiveVarSet<Local>>,
384384
) {
385385
let dead_unwinds = BitSet::new_empty(mir.basic_blocks().len());
386-
let node_id = tcx.hir().as_local_node_id(source.def_id).unwrap();
386+
let node_id = tcx.hir().as_local_node_id(source.def_id()).unwrap();
387387

388388
// Calculate when MIR locals have live storage. This gives us an upper bound of their
389389
// lifetimes.
@@ -484,7 +484,7 @@ fn locals_live_across_suspend_points(
484484
}
485485

486486
fn compute_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
487-
source: MirSource,
487+
source: MirSource<'tcx>,
488488
upvars: Vec<Ty<'tcx>>,
489489
interior: Ty<'tcx>,
490490
movable: bool,
@@ -635,7 +635,7 @@ fn create_generator_drop_shim<'a, 'tcx>(
635635
tcx: TyCtxt<'a, 'tcx, 'tcx>,
636636
transform: &TransformVisitor<'a, 'tcx>,
637637
def_id: DefId,
638-
source: MirSource,
638+
source: MirSource<'tcx>,
639639
gen_ty: Ty<'tcx>,
640640
mir: &Mir<'tcx>,
641641
drop_clean: BasicBlock) -> Mir<'tcx> {
@@ -758,7 +758,7 @@ fn create_generator_resume_function<'a, 'tcx>(
758758
tcx: TyCtxt<'a, 'tcx, 'tcx>,
759759
transform: TransformVisitor<'a, 'tcx>,
760760
def_id: DefId,
761-
source: MirSource,
761+
source: MirSource<'tcx>,
762762
mir: &mut Mir<'tcx>) {
763763
// Poison the generator when it unwinds
764764
for block in mir.basic_blocks_mut() {
@@ -869,7 +869,7 @@ fn create_cases<'a, 'tcx, F>(mir: &mut Mir<'tcx>,
869869
impl MirPass for StateTransform {
870870
fn run_pass<'a, 'tcx>(&self,
871871
tcx: TyCtxt<'a, 'tcx, 'tcx>,
872-
source: MirSource,
872+
source: MirSource<'tcx>,
873873
mir: &mut Mir<'tcx>) {
874874
let yield_ty = if let Some(yield_ty) = mir.yield_ty {
875875
yield_ty
@@ -880,7 +880,7 @@ impl MirPass for StateTransform {
880880

881881
assert!(mir.generator_drop.is_none());
882882

883-
let def_id = source.def_id;
883+
let def_id = source.def_id();
884884

885885
// The first argument is the generator type passed by value
886886
let gen_ty = mir.local_decls.raw[1].ty;

0 commit comments

Comments
 (0)