rustc: split off BodyOwnerKind from MirSource.

This commit is contained in:
Eduard-Mihai Burtescu 2017-11-10 19:20:35 +02:00
parent c79e8f4d1d
commit d6aa56f44e
35 changed files with 238 additions and 275 deletions

View file

@ -448,6 +448,28 @@ impl<'hir> Map<'hir> {
})
}
pub fn body_owner_kind(&self, id: NodeId) -> BodyOwnerKind {
// Handle constants in enum discriminants, types, and repeat expressions.
let def_id = self.local_def_id(id);
let def_key = self.def_key(def_id);
if def_key.disambiguated_data.data == DefPathData::Initializer {
return BodyOwnerKind::Const;
}
match self.get(id) {
NodeItem(&Item { node: ItemConst(..), .. }) |
NodeTraitItem(&TraitItem { node: TraitItemKind::Const(..), .. }) |
NodeImplItem(&ImplItem { node: ImplItemKind::Const(..), .. }) => {
BodyOwnerKind::Const
}
NodeItem(&Item { node: ItemStatic(_, m, _), .. }) => {
BodyOwnerKind::Static(m)
}
// Default to function if it's not a constant or static.
_ => BodyOwnerKind::Fn
}
}
pub fn ty_param_owner(&self, id: NodeId) -> NodeId {
match self.get(id) {
NodeItem(&Item { node: ItemTrait(..), .. }) => id,

View file

@ -1028,6 +1028,18 @@ impl Body {
}
}
#[derive(Copy, Clone, Debug)]
pub enum BodyOwnerKind {
/// Functions and methods.
Fn,
/// Constants and associated constants.
Const,
/// Initializer of a `static` item.
Static(Mutability),
}
/// An expression
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub struct Expr {

View file

@ -31,7 +31,6 @@ use hir;
use hir::def_id::DefId;
use hir::intravisit::{self, Visitor, NestedVisitorMap};
use hir::{Block, Arm, Pat, PatKind, Stmt, Expr, Local};
use mir::transform::MirSource;
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
StableHasherResult};
@ -1298,7 +1297,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionResolutionVisitor<'a, 'tcx> {
// The body of the every fn is a root scope.
self.cx.parent = self.cx.var_parent;
if let MirSource::Fn(_) = MirSource::from_node(self.tcx, owner_id) {
if let hir::BodyOwnerKind::Fn = self.tcx.hir.body_owner_kind(owner_id) {
self.visit_expr(&body.value);
} else {
// Only functions have an outer terminating (drop) scope, while

View file

@ -41,7 +41,6 @@ use syntax_pos::Span;
mod cache;
pub mod tcx;
pub mod visit;
pub mod transform;
pub mod traversal;
/// Types for locals

View file

@ -1,74 +0,0 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! See [the README](README.md) for details on writing your own pass.
use hir;
use hir::def_id::DefId;
use hir::map::DefPathData;
use mir::Promoted;
use ty::TyCtxt;
use syntax::ast::NodeId;
/// Where a specific Mir comes from.
#[derive(Debug, Copy, Clone)]
pub enum MirSource {
/// Functions and methods.
Fn(NodeId),
/// Constants and associated constants.
Const(NodeId),
/// Initializer of a `static` item.
Static(NodeId, hir::Mutability),
/// Promoted rvalues within a function.
Promoted(NodeId, Promoted),
}
impl<'a, 'gcx, 'tcx> MirSource {
pub fn from_local_def_id(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId) -> MirSource {
let id = tcx.hir.as_local_node_id(def_id).expect("mir source requires local def-id");
Self::from_node(tcx, id)
}
pub fn from_node(tcx: TyCtxt<'a, 'gcx, 'tcx>, id: NodeId) -> MirSource {
use hir::*;
// Handle constants in enum discriminants, types, and repeat expressions.
let def_id = tcx.hir.local_def_id(id);
let def_key = tcx.def_key(def_id);
if def_key.disambiguated_data.data == DefPathData::Initializer {
return MirSource::Const(id);
}
match tcx.hir.get(id) {
map::NodeItem(&Item { node: ItemConst(..), .. }) |
map::NodeTraitItem(&TraitItem { node: TraitItemKind::Const(..), .. }) |
map::NodeImplItem(&ImplItem { node: ImplItemKind::Const(..), .. }) => {
MirSource::Const(id)
}
map::NodeItem(&Item { node: ItemStatic(_, m, _), .. }) => {
MirSource::Static(id, m)
}
// Default to function if it's not a constant or static.
_ => MirSource::Fn(id)
}
}
pub fn item_id(&self) -> NodeId {
match *self {
MirSource::Fn(id) |
MirSource::Const(id) |
MirSource::Static(id, _) |
MirSource::Promoted(id, _) => id
}
}
}

View file

@ -18,7 +18,6 @@ use rustc::ty::maps::Providers;
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue, Local};
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
use rustc::mir::transform::MirSource;
use transform::nll;
use rustc_data_structures::indexed_set::{self, IdxSetBuf};
@ -50,8 +49,7 @@ pub fn provide(providers: &mut Providers) {
fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
let input_mir = tcx.mir_validated(def_id);
let src = MirSource::from_local_def_id(tcx, def_id);
debug!("run query mir_borrowck: {}", tcx.node_path_str(src.item_id()));
debug!("run query mir_borrowck: {}", tcx.item_path_str(def_id));
if {
!tcx.has_attr(def_id, "rustc_mir_borrowck") &&
@ -63,21 +61,20 @@ fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
tcx.infer_ctxt().enter(|infcx| {
let input_mir: &Mir = &input_mir.borrow();
do_mir_borrowck(&infcx, input_mir, def_id, src);
do_mir_borrowck(&infcx, input_mir, def_id);
});
debug!("mir_borrowck done");
}
fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
input_mir: &Mir<'gcx>,
def_id: DefId,
src: MirSource)
def_id: DefId)
{
let tcx = infcx.tcx;
let attributes = tcx.get_attrs(def_id);
let param_env = tcx.param_env(def_id);
let id = src.item_id();
let id = tcx.hir.as_local_node_id(def_id)
.expect("do_mir_borrowck: non-local DefId");
let move_data: MoveData<'tcx> = match MoveData::gather_moves(input_mir, tcx, param_env) {
Ok(move_data) => move_data,
@ -117,7 +114,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
let opt_regioncx = if !tcx.sess.opts.debugging_opts.nll {
None
} else {
Some(nll::compute_regions(infcx, src, mir))
Some(nll::compute_regions(infcx, def_id, mir))
};
let mdpe = MoveDataParamEnv { move_data: move_data, param_env: param_env };

View file

@ -16,7 +16,6 @@ use rustc::hir;
use rustc::hir::def_id::DefId;
use rustc::middle::region;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::mir::visit::{MutVisitor, TyContext};
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::Substs;
@ -30,6 +29,7 @@ use syntax::abi::Abi;
use syntax::ast;
use syntax::symbol::keywords;
use syntax_pos::Span;
use transform::MirSource;
use util as mir_util;
/// Construct the MIR for a given def-id.
@ -83,12 +83,11 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
_ => unsupported(),
};
let src = MirSource::from_node(tcx, id);
tcx.infer_ctxt().enter(|infcx| {
let cx = Cx::new(&infcx, src);
let cx = Cx::new(&infcx, id);
let mut mir = if cx.tables().tainted_by_errors {
build::construct_error(cx, body_id)
} else if let MirSource::Fn(id) = src {
} else if let hir::BodyOwnerKind::Fn = cx.body_owner_kind {
// fetch the fully liberated fn signature (that is, all bound
// types/lifetimes replaced)
let fn_hir_id = tcx.hir.node_to_hir_id(id);
@ -150,7 +149,8 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
mem::transmute::<Mir, Mir<'tcx>>(mir)
};
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir, |_, _| Ok(()) );
mir_util::dump_mir(tcx, None, "mir_map", &0,
MirSource::item(def_id), &mir, |_, _| Ok(()) );
mir
})
@ -214,8 +214,7 @@ fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let span = tcx.hir.span(ctor_id);
if let hir::VariantData::Tuple(ref fields, ctor_id) = *v {
tcx.infer_ctxt().enter(|infcx| {
let (mut mir, src) =
shim::build_adt_ctor(&infcx, ctor_id, fields, span);
let mut mir = shim::build_adt_ctor(&infcx, ctor_id, fields, span);
// Convert the Mir to global types.
let tcx = infcx.tcx.global_tcx();
@ -228,7 +227,9 @@ fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
mem::transmute::<Mir, Mir<'tcx>>(mir)
};
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir, |_, _| Ok(()) );
mir_util::dump_mir(tcx, None, "mir_map", &0,
MirSource::item(tcx.hir.local_def_id(ctor_id)),
&mir, |_, _| Ok(()) );
mir
})

View file

@ -91,9 +91,9 @@ use build::{BlockAnd, BlockAndExtension, Builder, CFG};
use hair::LintLevel;
use rustc::middle::region;
use rustc::ty::{Ty, TyCtxt};
use rustc::hir;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use syntax_pos::{Span};
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::fx::FxHashMap;
@ -596,15 +596,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// When building statics/constants, returns `None` since
/// intermediate values do not have to be dropped in that case.
pub fn local_scope(&self) -> Option<region::Scope> {
match self.hir.src {
MirSource::Const(_) |
MirSource::Static(..) =>
match self.hir.body_owner_kind {
hir::BodyOwnerKind::Const |
hir::BodyOwnerKind::Static(_) =>
// No need to free storage in this context.
None,
MirSource::Fn(_) =>
hir::BodyOwnerKind::Fn =>
Some(self.topmost_scope()),
MirSource::Promoted(..) =>
bug!(),
}
}

View file

@ -15,7 +15,6 @@
//!
use hair::*;
use rustc::mir::transform::MirSource;
use rustc::middle::const_val::{ConstEvalErr, ConstVal};
use rustc_const_eval::ConstContext;
@ -51,8 +50,8 @@ pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
/// `const`, or the body of a `const fn`.
constness: hir::Constness,
/// What are we compiling?
pub src: MirSource,
/// What kind of body is being compiled.
pub body_owner_kind: hir::BodyOwnerKind,
/// True if this constant/function needs overflow checks.
check_overflow: bool,
@ -60,21 +59,20 @@ pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
src: MirSource) -> Cx<'a, 'gcx, 'tcx> {
let constness = match src {
MirSource::Const(_) |
MirSource::Static(..) => hir::Constness::Const,
MirSource::Fn(id) => {
let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(id));
src_id: ast::NodeId) -> Cx<'a, 'gcx, 'tcx> {
let tcx = infcx.tcx;
let src_def_id = tcx.hir.local_def_id(src_id);
let body_owner_kind = tcx.hir.body_owner_kind(src_id);
let constness = match body_owner_kind {
hir::BodyOwnerKind::Const |
hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
hir::BodyOwnerKind::Fn => {
let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(src_id));
fn_like.map_or(hir::Constness::NotConst, |f| f.constness())
}
MirSource::Promoted(..) => bug!(),
};
let tcx = infcx.tcx;
let src_id = src.item_id();
let src_def_id = tcx.hir.local_def_id(src_id);
let attrs = tcx.hir.attrs(src_id);
// Some functions always have overflow checks enabled,
@ -99,7 +97,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
region_scope_tree: tcx.region_scope_tree(src_def_id),
tables: tcx.typeck_tables_of(src_def_id),
constness,
src,
body_owner_kind,
check_overflow,
}
}

View file

@ -13,7 +13,6 @@ use rustc::hir::def_id::DefId;
use rustc::infer;
use rustc::middle::const_val::ConstVal;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::{Kind, Subst, Substs};
use rustc::ty::maps::Providers;
@ -826,7 +825,7 @@ pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
ctor_id: ast::NodeId,
fields: &[hir::StructField],
span: Span)
-> (Mir<'tcx>, MirSource)
-> Mir<'tcx>
{
let tcx = infcx.tcx;
let def_id = tcx.hir.local_def_id(ctor_id);
@ -875,7 +874,7 @@ pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
is_cleanup: false
};
let mir = Mir::new(
Mir::new(
IndexVec::from_elem_n(start_block, 1),
IndexVec::from_elem_n(
VisibilityScopeData { span: span, parent_scope: None }, 1
@ -888,6 +887,5 @@ pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
sig.inputs().len(),
vec![],
span
);
(mir, MirSource::Fn(ctor_id))
)
}

View file

@ -10,9 +10,8 @@
use rustc::ty::TyCtxt;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use transform::MirPass;
use transform::{MirPass, MirSource};
#[derive(PartialEq)]
pub enum AddCallGuards {

View file

@ -17,9 +17,8 @@
use rustc::ty::{self, TyCtxt, RegionKind};
use rustc::hir;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::middle::region;
use transform::MirPass;
use transform::{MirPass, MirSource};
pub struct AddValidation;
@ -107,8 +106,9 @@ fn fn_contains_unsafe<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource) ->
}
}
let fn_like = match src {
MirSource::Fn(node_id) => {
let node_id = tcx.hir.as_local_node_id(src.def_id).unwrap();
let fn_like = match tcx.hir.body_owner_kind(node_id) {
hir::BodyOwnerKind::Fn => {
match FnLikeNode::from_node(tcx.hir.get(node_id)) {
Some(fn_like) => fn_like,
None => return false, // e.g. struct ctor shims -- such auto-generated code cannot

View file

@ -22,11 +22,10 @@
use rustc_data_structures::fx::FxHashSet;
use rustc::middle::region;
use rustc::mir::transform::MirSource;
use rustc::mir::{BasicBlock, Location, Mir, Rvalue, Statement, StatementKind};
use rustc::mir::visit::{MutVisitor, Visitor, TyContext};
use rustc::ty::{Ty, RegionKind, TyCtxt};
use transform::MirPass;
use transform::{MirPass, MirSource};
pub struct CleanEndRegions;

View file

@ -29,11 +29,11 @@
//! (non-mutating) use of `SRC`. These restrictions are conservative and may be relaxed in the
//! future.
use rustc::hir;
use rustc::mir::{Constant, Local, LocalKind, Location, Lvalue, Mir, Operand, Rvalue, StatementKind};
use rustc::mir::transform::MirSource;
use rustc::mir::visit::MutVisitor;
use rustc::ty::TyCtxt;
use transform::MirPass;
use transform::{MirPass, MirSource};
use util::def_use::DefUseAnalysis;
pub struct CopyPropagation;
@ -43,19 +43,17 @@ impl MirPass for CopyPropagation {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
mir: &mut Mir<'tcx>) {
match source {
MirSource::Const(_) => {
// Don't run on constants, because constant qualification might reject the
// optimized IR.
return
}
MirSource::Static(..) | MirSource::Promoted(..) => {
// Don't run on statics and promoted statics, because trans might not be able to
// evaluate the optimized IR.
return
}
MirSource::Fn(function_node_id) => {
if tcx.is_const_fn(tcx.hir.local_def_id(function_node_id)) {
// Don't run on constant MIR, because trans might not be able to
// evaluate the modified MIR.
// FIXME(eddyb) Remove check after miri is merged.
let id = tcx.hir.as_local_node_id(source.def_id).unwrap();
match (tcx.hir.body_owner_kind(id), source.promoted) {
(_, Some(_)) |
(hir::BodyOwnerKind::Const, _) |
(hir::BodyOwnerKind::Static(_), _) => return,
(hir::BodyOwnerKind::Fn, _) => {
if tcx.is_const_fn(source.def_id) {
// Don't run on const functions, as, again, trans might not be able to evaluate
// the optimized IR.
return

View file

@ -8,11 +8,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use rustc::hir;
use rustc::ty::TyCtxt;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc_data_structures::indexed_vec::Idx;
use transform::MirPass;
use transform::{MirPass, MirSource};
pub struct Deaggregator;
@ -21,16 +21,21 @@ impl MirPass for Deaggregator {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
mir: &mut Mir<'tcx>) {
let node_id = source.item_id();
let node_path = tcx.item_path_str(tcx.hir.local_def_id(node_id));
let node_path = tcx.item_path_str(source.def_id);
debug!("running on: {:?}", node_path);
// we only run when mir_opt_level > 2
if tcx.sess.opts.debugging_opts.mir_opt_level <= 2 {
return;
}
// Do not trigger on constants. Could be revised in future
if let MirSource::Fn(_) = source {} else { return; }
// Don't run on constant MIR, because trans might not be able to
// evaluate the modified MIR.
// FIXME(eddyb) Remove check after miri is merged.
let id = tcx.hir.as_local_node_id(source.def_id).unwrap();
match (tcx.hir.body_owner_kind(id), source.promoted) {
(hir::BodyOwnerKind::Fn, None) => {},
_ => return
}
// In fact, we might not want to trigger in other cases.
// Ex: when we could use SROA. See issue #35259

View file

@ -16,10 +16,9 @@ use std::fs::File;
use std::io;
use rustc::mir::Mir;
use rustc::mir::transform::MirSource;
use rustc::session::config::{OutputFilenames, OutputType};
use rustc::ty::TyCtxt;
use transform::MirPass;
use transform::{MirPass, MirSource};
use util as mir_util;
pub struct Marker(pub &'static str);

View file

@ -15,14 +15,14 @@ use dataflow::{on_all_children_bits, on_all_drop_children_bits};
use dataflow::{drop_flag_effects_for_location, on_lookup_result_bits};
use dataflow::MoveDataParamEnv;
use dataflow;
use rustc::hir;
use rustc::ty::{self, TyCtxt};
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::middle::const_val::ConstVal;
use rustc::util::nodemap::FxHashMap;
use rustc_data_structures::indexed_set::IdxSetBuf;
use rustc_data_structures::indexed_vec::Idx;
use transform::MirPass;
use transform::{MirPass, MirSource};
use util::patch::MirPatch;
use util::elaborate_drops::{DropFlagState, Unwind, elaborate_drop};
use util::elaborate_drops::{DropElaborator, DropStyle, DropFlagMode};
@ -40,12 +40,16 @@ impl MirPass for ElaborateDrops {
mir: &mut Mir<'tcx>)
{
debug!("elaborate_drops({:?} @ {:?})", src, mir.span);
match src {
MirSource::Fn(..) => {},
// Don't run on constant MIR, because trans might not be able to
// evaluate the modified MIR.
// FIXME(eddyb) Remove check after miri is merged.
let id = tcx.hir.as_local_node_id(src.def_id).unwrap();
match (tcx.hir.body_owner_kind(id), src.promoted) {
(hir::BodyOwnerKind::Fn, None) => {},
_ => return
}
let id = src.item_id();
let param_env = tcx.param_env(tcx.hir.local_def_id(id));
let param_env = tcx.param_env(src.def_id);
let move_data = MoveData::gather_moves(mir, tcx, param_env).unwrap();
let elaborate_patch = {
let mir = &*mir;

View file

@ -18,8 +18,7 @@ use rustc::ty::subst::Substs;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::mir::*;
use rustc::mir::visit::{MutVisitor, TyContext};
use rustc::mir::transform::MirSource;
use transform::MirPass;
use transform::{MirPass, MirSource};
struct EraseRegionsVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,

View file

@ -63,7 +63,6 @@ use rustc::hir;
use rustc::hir::def_id::DefId;
use rustc::middle::const_val::ConstVal;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::mir::visit::{LvalueContext, Visitor, MutVisitor};
use rustc::ty::{self, TyCtxt, AdtDef, Ty, GeneratorInterior};
use rustc::ty::subst::{Kind, Substs};
@ -76,7 +75,7 @@ use std::collections::HashMap;
use std::borrow::Cow;
use std::iter::once;
use std::mem;
use transform::MirPass;
use transform::{MirPass, MirSource};
use transform::simplify;
use transform::no_landing_pads::no_landing_pads;
use dataflow::{self, MaybeStorageLive, state_for_location};
@ -339,7 +338,7 @@ fn locals_live_across_suspend_points<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
(liveness::LocalSet,
HashMap<BasicBlock, liveness::LocalSet>) {
let dead_unwinds = IdxSetBuf::new_empty(mir.basic_blocks().len());
let node_id = source.item_id();
let node_id = tcx.hir.as_local_node_id(source.def_id).unwrap();
let analysis = MaybeStorageLive::new(mir);
let storage_live =
dataflow::do_dataflow(tcx, mir, node_id, &[], &dead_unwinds, analysis,
@ -764,8 +763,8 @@ impl MirPass for StateTransform {
assert!(mir.generator_drop.is_none());
let node_id = source.item_id();
let def_id = tcx.hir.local_def_id(source.item_id());
let def_id = source.def_id;
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
let hir_id = tcx.hir.node_to_hir_id(node_id);
// Get the interior types which typeck computed

View file

@ -10,19 +10,19 @@
//! Inlining pass for MIR functions
use rustc::hir;
use rustc::hir::def_id::DefId;
use rustc_data_structures::bitvec::BitVector;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::mir::visit::*;
use rustc::ty::{self, Instance, Ty, TyCtxt, TypeFoldable};
use rustc::ty::subst::{Subst,Substs};
use std::collections::VecDeque;
use transform::MirPass;
use transform::{MirPass, MirSource};
use super::simplify::{remove_dead_blocks, CfgSimplifier};
use syntax::{attr};
@ -78,12 +78,12 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
let mut callsites = VecDeque::new();
let param_env;
let param_env = self.tcx.param_env(self.source.def_id);
// Only do inlining into fn bodies.
if let MirSource::Fn(caller_id) = self.source {
let caller_def_id = self.tcx.hir.local_def_id(caller_id);
param_env = self.tcx.param_env(caller_def_id);
let id = self.tcx.hir.as_local_node_id(self.source.def_id).unwrap();
let body_owner_kind = self.tcx.hir.body_owner_kind(id);
if let (hir::BodyOwnerKind::Fn, None) = (body_owner_kind, self.source.promoted) {
for (bb, bb_data) in caller_mir.basic_blocks().iter_enumerated() {
// Don't inline calls that are in cleanup blocks.
@ -252,8 +252,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
// FIXME: Give a bonus to functions with only a single caller
let def_id = tcx.hir.local_def_id(self.source.item_id());
let param_env = tcx.param_env(def_id);
let param_env = tcx.param_env(self.source.def_id);
let mut first_block = true;
let mut cost = 0;

View file

@ -11,13 +11,12 @@
//! Performs various peephole optimizations.
use rustc::mir::{Location, Lvalue, Mir, Operand, ProjectionElem, Rvalue, Local};
use rustc::mir::transform::MirSource;
use rustc::mir::visit::{MutVisitor, Visitor};
use rustc::ty::TyCtxt;
use rustc::util::nodemap::FxHashSet;
use rustc_data_structures::indexed_vec::Idx;
use std::mem;
use transform::MirPass;
use transform::{MirPass, MirSource};
pub struct InstCombine;

View file

@ -10,8 +10,7 @@
use build;
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc::mir::Mir;
use rustc::mir::transform::MirSource;
use rustc::mir::{Mir, Promoted};
use rustc::ty::TyCtxt;
use rustc::ty::maps::Providers;
use rustc::ty::steal::Steal;
@ -108,6 +107,24 @@ fn mir_built<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Stea
tcx.alloc_steal_mir(mir)
}
/// Where a specific Mir comes from.
#[derive(Debug, Copy, Clone)]
pub struct MirSource {
pub def_id: DefId,
/// If `Some`, this is a promoted rvalue within the parent function.
pub promoted: Option<Promoted>,
}
impl MirSource {
pub fn item(def_id: DefId) -> Self {
MirSource {
def_id,
promoted: None
}
}
}
/// Generates a default name for the pass based on the name of the
/// type `T`.
pub fn default_name<T: ?Sized>() -> Cow<'static, str> {
@ -133,9 +150,13 @@ pub trait MirPass {
mir: &mut Mir<'tcx>);
}
pub macro run_passes($tcx:ident, $mir:ident, $source:ident, $suite_index:expr; $($pass:expr,)*) {{
pub macro run_passes($tcx:ident, $mir:ident, $def_id:ident, $suite_index:expr; $($pass:expr,)*) {{
let suite_index: usize = $suite_index;
let run_passes = |mir: &mut _, source| {
let run_passes = |mir: &mut _, promoted| {
let source = MirSource {
def_id: $def_id,
promoted
};
let mut index = 0;
let mut run_pass = |pass: &MirPass| {
let run_hooks = |mir: &_, index, is_after| {
@ -150,10 +171,11 @@ pub macro run_passes($tcx:ident, $mir:ident, $source:ident, $suite_index:expr; $
};
$(run_pass(&$pass);)*
};
run_passes(&mut $mir, $source);
run_passes(&mut $mir, None);
for (index, promoted_mir) in $mir.promoted.iter_enumerated_mut() {
run_passes(promoted_mir, MirSource::Promoted($source.item_id(), index));
run_passes(promoted_mir, Some(index));
// Let's make sure we don't miss any nested instances
assert!(promoted_mir.promoted.is_empty());
@ -165,8 +187,7 @@ fn mir_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Stea
let _ = tcx.unsafety_check_result(def_id);
let mut mir = tcx.mir_built(def_id).steal();
let source = MirSource::from_local_def_id(tcx, def_id);
run_passes![tcx, mir, source, 0;
run_passes![tcx, mir, def_id, 0;
// Remove all `EndRegion` statements that are not involved in borrows.
clean_end_regions::CleanEndRegions,
@ -179,15 +200,15 @@ fn mir_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Stea
}
fn mir_validated<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
let source = MirSource::from_local_def_id(tcx, def_id);
if let MirSource::Const(_) = source {
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
if let hir::BodyOwnerKind::Const = tcx.hir.body_owner_kind(node_id) {
// Ensure that we compute the `mir_const_qualif` for constants at
// this point, before we steal the mir-const result.
let _ = tcx.mir_const_qualif(def_id);
}
let mut mir = tcx.mir_const(def_id).steal();
run_passes![tcx, mir, source, 1;
run_passes![tcx, mir, def_id, 1;
// What we need to run borrowck etc.
qualify_consts::QualifyAndPromoteConstants,
simplify::SimplifyCfg::new("qualify-consts"),
@ -202,8 +223,7 @@ fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
let _ = tcx.borrowck(def_id);
let mut mir = tcx.mir_validated(def_id).steal();
let source = MirSource::from_local_def_id(tcx, def_id);
run_passes![tcx, mir, source, 2;
run_passes![tcx, mir, def_id, 2;
no_landing_pads::NoLandingPads,
simplify_branches::SimplifyBranches::new("initial"),

View file

@ -10,7 +10,6 @@
use rustc::hir;
use rustc::mir::{BasicBlock, BorrowKind, Location, Lvalue, Mir, Rvalue, Statement, StatementKind};
use rustc::mir::transform::MirSource;
use rustc::mir::visit::Visitor;
use rustc::mir::Lvalue::Projection;
use rustc::mir::{LvalueProjection, ProjectionElem};
@ -31,7 +30,7 @@ pub(super) fn generate_constraints<'a, 'gcx, 'tcx>(
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
regioncx: &mut RegionInferenceContext<'tcx>,
mir: &Mir<'tcx>,
mir_source: MirSource,
param_env: ty::ParamEnv<'tcx>,
liveness: &LivenessResults,
) {
ConstraintGeneration {
@ -39,7 +38,7 @@ pub(super) fn generate_constraints<'a, 'gcx, 'tcx>(
regioncx,
mir,
liveness,
mir_source,
param_env,
}.add_constraints();
}
@ -48,7 +47,7 @@ struct ConstraintGeneration<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
regioncx: &'cx mut RegionInferenceContext<'tcx>,
mir: &'cx Mir<'tcx>,
liveness: &'cx LivenessResults,
mir_source: MirSource,
param_env: ty::ParamEnv<'tcx>,
}
impl<'cx, 'gcx, 'tcx> ConstraintGeneration<'cx, 'gcx, 'tcx> {
@ -153,13 +152,11 @@ impl<'cx, 'gcx, 'tcx> ConstraintGeneration<'cx, 'gcx, 'tcx> {
// `dtorck_constraint_for_ty` could not resolve (e.g.,
// associated types and parameters). We need to normalize
// associated types here and possibly recursively process.
let def_id = tcx.hir.local_def_id(self.mir_source.item_id());
let param_env = self.infcx.tcx.param_env(def_id);
for ty in dtorck_types {
// FIXME -- I think that this may disregard some region obligations
// or something. Do we care? -nmatsakis
let cause = ObligationCause::dummy();
match traits::fully_normalize(self.infcx, cause, param_env, &ty) {
match traits::fully_normalize(self.infcx, cause, self.param_env, &ty) {
Ok(ty) => match ty.sty {
ty::TyParam(..) | ty::TyProjection(..) | ty::TyAnon(..) => {
self.add_regular_live_constraint(ty, location);

View file

@ -22,9 +22,9 @@
//! The code in this file doesn't *do anything* with those results; it
//! just returns them for other code to use.
use rustc::hir::def_id::DefId;
use rustc::infer::InferCtxt;
use rustc::middle::free_region::FreeRegionMap;
use rustc::mir::transform::MirSource;
use rustc::ty;
use rustc::ty::subst::Substs;
use rustc::util::nodemap::FxHashMap;
@ -43,12 +43,9 @@ pub struct FreeRegions<'tcx> {
pub fn free_regions<'a, 'gcx, 'tcx>(
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
source: MirSource,
item_def_id: DefId,
) -> FreeRegions<'tcx> {
debug!("free_regions(source={:?})", source);
let item_id = source.item_id();
let item_def_id = infcx.tcx.hir.local_def_id(item_id);
debug!("free_regions(item_def_id={:?})", item_def_id);
let mut indices = FxHashMap();
@ -63,6 +60,7 @@ pub fn free_regions<'a, 'gcx, 'tcx>(
// Extract the late-bound regions. Use the liberated fn sigs,
// where the late-bound regions will have been converted into free
// regions, and add them to the map.
let item_id = infcx.tcx.hir.as_local_node_id(item_def_id).unwrap();
let fn_hir_id = infcx.tcx.hir.node_to_hir_id(item_id);
let tables = infcx.tcx.typeck_tables_of(item_def_id);
let fn_sig = tables.liberated_fn_sigs()[fn_hir_id].clone();

View file

@ -8,13 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use rustc::ty::{self, RegionKind};
use rustc::hir::def_id::DefId;
use rustc::mir::Mir;
use rustc::mir::transform::MirSource;
use rustc::infer::InferCtxt;
use rustc::ty::{self, RegionKind};
use rustc::util::nodemap::FxHashMap;
use rustc_data_structures::indexed_vec::Idx;
use std::collections::BTreeSet;
use transform::MirSource;
use util::liveness::{self, LivenessMode, LivenessResult, LocalSet};
use util as mir_util;
@ -34,11 +35,11 @@ mod renumber;
/// This may result in errors being reported.
pub fn compute_regions<'a, 'gcx, 'tcx>(
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
source: MirSource,
def_id: DefId,
mir: &mut Mir<'tcx>,
) -> RegionInferenceContext<'tcx> {
// Compute named region information.
let free_regions = &free_regions::free_regions(infcx, source);
let free_regions = &free_regions::free_regions(infcx, def_id);
// Replace all regions with fresh inference variables.
let num_region_variables = renumber::renumber_mir(infcx, free_regions, mir);
@ -65,12 +66,13 @@ pub fn compute_regions<'a, 'gcx, 'tcx>(
// Create the region inference context, generate the constraints,
// and then solve them.
let mut regioncx = RegionInferenceContext::new(free_regions, num_region_variables, mir);
constraint_generation::generate_constraints(infcx, &mut regioncx, &mir, source, liveness);
let param_env = infcx.tcx.param_env(def_id);
constraint_generation::generate_constraints(infcx, &mut regioncx, &mir, param_env, liveness);
regioncx.solve(infcx, &mir);
// Dump MIR results into a file, if that is enabled. This let us
// write unit-tests.
dump_mir_results(infcx, liveness, source, &mir, &regioncx);
dump_mir_results(infcx, liveness, MirSource::item(def_id), &mir, &regioncx);
regioncx
}

View file

@ -14,8 +14,7 @@
use rustc::ty::TyCtxt;
use rustc::mir::*;
use rustc::mir::visit::MutVisitor;
use rustc::mir::transform::MirSource;
use transform::MirPass;
use transform::{MirPass, MirSource};
pub struct NoLandingPads;

View file

@ -26,7 +26,6 @@ use rustc::ty::cast::CastTy;
use rustc::ty::maps::Providers;
use rustc::mir::*;
use rustc::mir::traversal::ReversePostorder;
use rustc::mir::transform::MirSource;
use rustc::mir::visit::{LvalueContext, Visitor};
use rustc::middle::lang_items;
use syntax::abi::Abi;
@ -38,7 +37,7 @@ use std::fmt;
use std::rc::Rc;
use std::usize;
use transform::MirPass;
use transform::{MirPass, MirSource};
use super::promote_consts::{self, Candidate, TempState};
bitflags! {
@ -962,24 +961,27 @@ impl MirPass for QualifyAndPromoteConstants {
return;
}
let id = src.item_id();
let def_id = tcx.hir.local_def_id(id);
if src.promoted.is_some() {
return;
}
let def_id = src.def_id;
let id = tcx.hir.as_local_node_id(def_id).unwrap();
let mut const_promoted_temps = None;
let mode = match src {
MirSource::Fn(_) => {
let mode = match tcx.hir.body_owner_kind(id) {
hir::BodyOwnerKind::Fn => {
if tcx.is_const_fn(def_id) {
Mode::ConstFn
} else {
Mode::Fn
}
}
MirSource::Const(_) => {
hir::BodyOwnerKind::Const => {
const_promoted_temps = Some(tcx.mir_const_qualif(def_id).1);
Mode::Const
}
MirSource::Static(_, hir::MutImmutable) => Mode::Static,
MirSource::Static(_, hir::MutMutable) => Mode::StaticMut,
MirSource::Promoted(..) => return
hir::BodyOwnerKind::Static(hir::MutImmutable) => Mode::Static,
hir::BodyOwnerKind::Static(hir::MutMutable) => Mode::StaticMut,
};
if mode == Mode::Fn || mode == Mode::ConstFn {

View file

@ -14,10 +14,9 @@ use syntax_pos::Span;
use rustc::ty::{self, TyCtxt};
use rustc::mir::{self, Mir, Location};
use rustc::mir::transform::MirSource;
use rustc_data_structures::indexed_set::IdxSetBuf;
use rustc_data_structures::indexed_vec::Idx;
use transform::MirPass;
use transform::{MirPass, MirSource};
use dataflow::do_dataflow;
use dataflow::MoveDataParamEnv;
@ -35,8 +34,8 @@ pub struct SanityCheck;
impl MirPass for SanityCheck {
fn run_pass<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
src: MirSource, mir: &mut Mir<'tcx>) {
let id = src.item_id();
let def_id = tcx.hir.local_def_id(id);
let def_id = src.def_id;
let id = tcx.hir.as_local_node_id(def_id).unwrap();
if !tcx.has_attr(def_id, "rustc_mir_borrowck") {
debug!("skipping rustc_peek::SanityCheck on {}", tcx.item_path_str(def_id));
return;

View file

@ -41,10 +41,9 @@ use rustc_data_structures::bitvec::BitVector;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc::ty::TyCtxt;
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::mir::visit::{MutVisitor, Visitor, LvalueContext};
use std::borrow::Cow;
use transform::MirPass;
use transform::{MirPass, MirSource};
pub struct SimplifyCfg { label: String }

View file

@ -12,9 +12,8 @@
use rustc::ty::{self, TyCtxt};
use rustc::middle::const_val::ConstVal;
use rustc::mir::transform::MirSource;
use rustc::mir::*;
use transform::MirPass;
use transform::{MirPass, MirSource};
use std::borrow::Cow;

View file

@ -18,12 +18,11 @@ use rustc::ty::{self, Ty, TyCtxt, TypeVariants};
use rustc::middle::const_val::ConstVal;
use rustc::mir::*;
use rustc::mir::tcx::LvalueTy;
use rustc::mir::transform::MirSource;
use rustc::mir::visit::Visitor;
use std::fmt;
use syntax::ast;
use syntax_pos::{Span, DUMMY_SP};
use transform::MirPass;
use transform::{MirPass, MirSource};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::indexed_vec::Idx;
@ -795,8 +794,8 @@ impl MirPass for TypeckMir {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
src: MirSource,
mir: &mut Mir<'tcx>) {
let item_id = src.item_id();
let def_id = tcx.hir.local_def_id(item_id);
let def_id = src.def_id;
let id = tcx.hir.as_local_node_id(def_id).unwrap();
debug!("run_pass: {:?}", def_id);
if tcx.sess.err_count() > 0 {
@ -806,7 +805,7 @@ impl MirPass for TypeckMir {
}
let param_env = tcx.param_env(def_id);
tcx.infer_ctxt().enter(|infcx| {
let mut checker = TypeChecker::new(&infcx, item_id, param_env);
let mut checker = TypeChecker::new(&infcx, id, param_env);
{
let mut verifier = TypeVerifier::new(&mut checker, mir);
verifier.visit_mir(mir);

View file

@ -14,7 +14,6 @@ use rustc::mir::*;
use rustc::ty::TyCtxt;
use std::fmt::Debug;
use std::io::{self, Write};
use syntax::ast::NodeId;
use rustc_data_structures::indexed_vec::Idx;
@ -28,21 +27,20 @@ pub fn write_mir_graphviz<'tcx, W>(tcx: TyCtxt<'_, '_, 'tcx>,
where W: Write
{
for def_id in dump_mir_def_ids(tcx, single) {
let nodeid = tcx.hir.as_local_node_id(def_id).unwrap();
let mir = &tcx.optimized_mir(def_id);
write_mir_fn_graphviz(tcx, nodeid, mir, w)?;
write_mir_fn_graphviz(tcx, def_id, mir, w)?;
}
Ok(())
}
/// Write a graphviz DOT graph of the MIR.
pub fn write_mir_fn_graphviz<'tcx, W>(tcx: TyCtxt<'_, '_, 'tcx>,
nodeid: NodeId,
def_id: DefId,
mir: &Mir,
w: &mut W) -> io::Result<()>
where W: Write
{
writeln!(w, "digraph Mir_{} {{", nodeid)?;
writeln!(w, "digraph Mir_{} {{", tcx.hir.as_local_node_id(def_id).unwrap())?;
// Global graph properties
writeln!(w, r#" graph [fontname="monospace"];"#)?;
@ -50,7 +48,7 @@ pub fn write_mir_fn_graphviz<'tcx, W>(tcx: TyCtxt<'_, '_, 'tcx>,
writeln!(w, r#" edge [fontname="monospace"];"#)?;
// Graph label
write_graph_label(tcx, nodeid, mir, w)?;
write_graph_label(tcx, def_id, mir, w)?;
// Nodes
for (block, _) in mir.basic_blocks().iter_enumerated() {
@ -138,11 +136,11 @@ fn write_edges<W: Write>(source: BasicBlock, mir: &Mir, w: &mut W) -> io::Result
/// will appear below the graph, showing the type of the `fn` this MIR represents and the types of
/// all the variables and temporaries.
fn write_graph_label<'a, 'gcx, 'tcx, W: Write>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
nid: NodeId,
def_id: DefId,
mir: &Mir,
w: &mut W)
-> io::Result<()> {
write!(w, " label=<fn {}(", dot::escape_html(&tcx.node_path_str(nid)))?;
write!(w, " label=<fn {}(", dot::escape_html(&tcx.item_path_str(def_id)))?;
// fn argument types.
for (i, arg) in mir.args_iter().enumerate() {

View file

@ -38,12 +38,12 @@ use rustc::mir::visit::{LvalueContext, Visitor};
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc_data_structures::indexed_set::IdxSetBuf;
use util::pretty::{dump_enabled, write_basic_block, write_mir_intro};
use rustc::mir::transform::MirSource;
use rustc::ty::item_path;
use std::path::{Path, PathBuf};
use std::fs;
use rustc::ty::TyCtxt;
use std::io::{self, Write};
use transform::MirSource;
pub type LocalSet = IdxSetBuf<Local>;
@ -357,7 +357,7 @@ pub fn dump_mir<'a, 'tcx>(
}
let node_path = item_path::with_forced_impl_filename_line(|| {
// see notes on #41697 below
tcx.item_path_str(tcx.hir.local_def_id(source.item_id()))
tcx.item_path_str(source.def_id)
});
dump_matched_mir_node(tcx, pass_name, &node_path, source, mir, result);
}
@ -375,7 +375,8 @@ fn dump_matched_mir_node<'a, 'tcx>(
let p = Path::new(file_dir);
file_path.push(p);
};
let file_name = format!("rustc.node{}{}-liveness.mir", source.item_id(), pass_name);
let item_id = tcx.hir.as_local_node_id(source.def_id).unwrap();
let file_name = format!("rustc.node{}{}-liveness.mir", item_id, pass_name);
file_path.push(&file_name);
let _ = fs::File::create(&file_path).and_then(|mut file| {
writeln!(file, "// MIR local liveness analysis for `{}`", node_path)?;

View file

@ -11,7 +11,6 @@
use rustc::hir;
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::mir::*;
use rustc::mir::transform::MirSource;
use rustc::ty::TyCtxt;
use rustc::ty::item_path;
use rustc_data_structures::fx::FxHashMap;
@ -21,6 +20,7 @@ use std::fs;
use std::io::{self, Write};
use std::path::{PathBuf, Path};
use super::graphviz::write_mir_fn_graphviz;
use transform::MirSource;
const INDENT: &'static str = " ";
/// Alignment for lining up comments following MIR statements
@ -71,7 +71,7 @@ where
}
let node_path = item_path::with_forced_impl_filename_line(|| { // see notes on #41697 below
tcx.item_path_str(tcx.hir.local_def_id(source.item_id()))
tcx.item_path_str(source.def_id)
});
dump_matched_mir_node(tcx, pass_num, pass_name, &node_path,
disambiguator, source, mir, extra_data);
@ -85,9 +85,8 @@ pub fn dump_enabled<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
None => return false,
Some(ref filters) => filters,
};
let node_id = source.item_id();
let node_path = item_path::with_forced_impl_filename_line(|| { // see notes on #41697 below
tcx.item_path_str(tcx.hir.local_def_id(node_id))
tcx.item_path_str(source.def_id)
});
filters.split("&")
.any(|filter| {
@ -112,9 +111,9 @@ fn dump_matched_mir_node<'a, 'gcx, 'tcx, F>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
where
F: FnMut(PassWhere, &mut Write) -> io::Result<()>
{
let promotion_id = match source {
MirSource::Promoted(_, id) => format!("-{:?}", id),
_ => String::new()
let promotion_id = match source.promoted {
Some(id) => format!("-{:?}", id),
None => String::new()
};
let pass_num = if tcx.sess.opts.debugging_opts.dump_mir_exclude_pass_number {
@ -133,11 +132,9 @@ where
};
let _ = fs::create_dir_all(&file_path);
let function_name = tcx.hir.def_path_from_id(source.item_id())
.map(|d| d.to_filename_friendly_no_crate())
.unwrap_or(format!("node{}", source.item_id()));
let item_name = tcx.hir.def_path(source.def_id).to_filename_friendly_no_crate();
let file_name = format!("rustc.{}{}{}.{}.{}.mir",
function_name, promotion_id, pass_num, pass_name, disambiguator);
item_name, promotion_id, pass_num, pass_name, disambiguator);
file_path.push(&file_name);
let _ = fs::File::create(&file_path).and_then(|mut file| {
writeln!(file, "// MIR for `{}`", node_path)?;
@ -157,7 +154,7 @@ where
if tcx.sess.opts.debugging_opts.dump_mir_graphviz {
file_path.set_extension("dot");
let _ = fs::File::create(&file_path).and_then(|mut file| {
write_mir_fn_graphviz(tcx, source.item_id(), mir, &mut file)?;
write_mir_fn_graphviz(tcx, source.def_id, mir, &mut file)?;
Ok(())
});
}
@ -183,13 +180,15 @@ pub fn write_mir_pretty<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
writeln!(w, "")?;
}
let id = tcx.hir.as_local_node_id(def_id).unwrap();
let src = MirSource::from_node(tcx, id);
write_mir_fn(tcx, src, mir, &mut |_, _| Ok(()), w)?;
write_mir_fn(tcx, MirSource::item(def_id), mir, &mut |_, _| Ok(()), w)?;
for (i, mir) in mir.promoted.iter_enumerated() {
writeln!(w, "")?;
write_mir_fn(tcx, MirSource::Promoted(id, i), mir, &mut |_, _| Ok(()), w)?;
let src = MirSource {
def_id,
promoted: Some(i)
};
write_mir_fn(tcx, src, mir, &mut |_, _| Ok(()), w)?;
}
}
Ok(())
@ -367,20 +366,22 @@ pub fn write_mir_intro<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
fn write_mir_sig(tcx: TyCtxt, src: MirSource, mir: &Mir, w: &mut Write)
-> io::Result<()>
{
match src {
MirSource::Fn(_) => write!(w, "fn")?,
MirSource::Const(_) => write!(w, "const")?,
MirSource::Static(_, hir::MutImmutable) => write!(w, "static")?,
MirSource::Static(_, hir::MutMutable) => write!(w, "static mut")?,
MirSource::Promoted(_, i) => write!(w, "{:?} in", i)?,
let id = tcx.hir.as_local_node_id(src.def_id).unwrap();
let body_owner_kind = tcx.hir.body_owner_kind(id);
match (body_owner_kind, src.promoted) {
(_, Some(i)) => write!(w, "{:?} in", i)?,
(hir::BodyOwnerKind::Fn, _) => write!(w, "fn")?,
(hir::BodyOwnerKind::Const, _) => write!(w, "const")?,
(hir::BodyOwnerKind::Static(hir::MutImmutable), _) => write!(w, "static")?,
(hir::BodyOwnerKind::Static(hir::MutMutable), _) => write!(w, "static mut")?,
}
item_path::with_forced_impl_filename_line(|| { // see notes on #41697 elsewhere
write!(w, " {}", tcx.node_path_str(src.item_id()))
write!(w, " {}", tcx.item_path_str(src.def_id))
})?;
match src {
MirSource::Fn(_) => {
match (body_owner_kind, src.promoted) {
(hir::BodyOwnerKind::Fn, None) => {
write!(w, "(")?;
// fn argument types.
@ -393,9 +394,9 @@ fn write_mir_sig(tcx: TyCtxt, src: MirSource, mir: &Mir, w: &mut Write)
write!(w, ") -> {}", mir.return_ty)
}
MirSource::Const(..) |
MirSource::Static(..) |
MirSource::Promoted(..) => {
(hir::BodyOwnerKind::Const, _) |
(hir::BodyOwnerKind::Static(_), _) |
(_, Some(_)) => {
assert_eq!(mir.arg_count, 0);
write!(w, ": {} =", mir.return_ty)
}

View file

@ -37,7 +37,6 @@ use rustc::hir::map::blocks::FnLikeNode;
use rustc::middle::expr_use_visitor as euv;
use rustc::middle::mem_categorization as mc;
use rustc::middle::mem_categorization::Categorization;
use rustc::mir::transform::MirSource;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::maps::{queries, Providers};
use rustc::ty::subst::Substs;
@ -184,9 +183,9 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckCrateVisitor<'a, 'tcx> {
self.in_fn = false;
self.in_static = false;
match MirSource::from_node(self.tcx, item_id) {
MirSource::Fn(_) => self.in_fn = true,
MirSource::Static(_, _) => self.in_static = true,
match self.tcx.hir.body_owner_kind(item_id) {
hir::BodyOwnerKind::Fn => self.in_fn = true,
hir::BodyOwnerKind::Static(_) => self.in_static = true,
_ => {}
};