Skip to content

Commit d6aa56f

Browse files
committed
rustc: split off BodyOwnerKind from MirSource.
1 parent c79e8f4 commit d6aa56f

35 files changed

+238
-275
lines changed

src/librustc/hir/map/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,28 @@ impl<'hir> Map<'hir> {
448448
})
449449
}
450450

451+
pub fn body_owner_kind(&self, id: NodeId) -> BodyOwnerKind {
452+
// Handle constants in enum discriminants, types, and repeat expressions.
453+
let def_id = self.local_def_id(id);
454+
let def_key = self.def_key(def_id);
455+
if def_key.disambiguated_data.data == DefPathData::Initializer {
456+
return BodyOwnerKind::Const;
457+
}
458+
459+
match self.get(id) {
460+
NodeItem(&Item { node: ItemConst(..), .. }) |
461+
NodeTraitItem(&TraitItem { node: TraitItemKind::Const(..), .. }) |
462+
NodeImplItem(&ImplItem { node: ImplItemKind::Const(..), .. }) => {
463+
BodyOwnerKind::Const
464+
}
465+
NodeItem(&Item { node: ItemStatic(_, m, _), .. }) => {
466+
BodyOwnerKind::Static(m)
467+
}
468+
// Default to function if it's not a constant or static.
469+
_ => BodyOwnerKind::Fn
470+
}
471+
}
472+
451473
pub fn ty_param_owner(&self, id: NodeId) -> NodeId {
452474
match self.get(id) {
453475
NodeItem(&Item { node: ItemTrait(..), .. }) => id,

src/librustc/hir/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,18 @@ impl Body {
10281028
}
10291029
}
10301030

1031+
#[derive(Copy, Clone, Debug)]
1032+
pub enum BodyOwnerKind {
1033+
/// Functions and methods.
1034+
Fn,
1035+
1036+
/// Constants and associated constants.
1037+
Const,
1038+
1039+
/// Initializer of a `static` item.
1040+
Static(Mutability),
1041+
}
1042+
10311043
/// An expression
10321044
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
10331045
pub struct Expr {

src/librustc/middle/region.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use hir;
3131
use hir::def_id::DefId;
3232
use hir::intravisit::{self, Visitor, NestedVisitorMap};
3333
use hir::{Block, Arm, Pat, PatKind, Stmt, Expr, Local};
34-
use mir::transform::MirSource;
3534
use rustc_data_structures::indexed_vec::Idx;
3635
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
3736
StableHasherResult};
@@ -1298,7 +1297,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionResolutionVisitor<'a, 'tcx> {
12981297

12991298
// The body of the every fn is a root scope.
13001299
self.cx.parent = self.cx.var_parent;
1301-
if let MirSource::Fn(_) = MirSource::from_node(self.tcx, owner_id) {
1300+
if let hir::BodyOwnerKind::Fn = self.tcx.hir.body_owner_kind(owner_id) {
13021301
self.visit_expr(&body.value);
13031302
} else {
13041303
// Only functions have an outer terminating (drop) scope, while

src/librustc/mir/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use syntax_pos::Span;
4141
mod cache;
4242
pub mod tcx;
4343
pub mod visit;
44-
pub mod transform;
4544
pub mod traversal;
4645

4746
/// Types for locals

src/librustc/mir/transform.rs

-74
This file was deleted.

src/librustc_mir/borrow_check.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc::ty::maps::Providers;
1818
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue, Local};
1919
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
2020
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
21-
use rustc::mir::transform::MirSource;
2221
use transform::nll;
2322

2423
use rustc_data_structures::indexed_set::{self, IdxSetBuf};
@@ -50,8 +49,7 @@ pub fn provide(providers: &mut Providers) {
5049

5150
fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
5251
let input_mir = tcx.mir_validated(def_id);
53-
let src = MirSource::from_local_def_id(tcx, def_id);
54-
debug!("run query mir_borrowck: {}", tcx.node_path_str(src.item_id()));
52+
debug!("run query mir_borrowck: {}", tcx.item_path_str(def_id));
5553

5654
if {
5755
!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) {
6361

6462
tcx.infer_ctxt().enter(|infcx| {
6563
let input_mir: &Mir = &input_mir.borrow();
66-
do_mir_borrowck(&infcx, input_mir, def_id, src);
64+
do_mir_borrowck(&infcx, input_mir, def_id);
6765
});
6866
debug!("mir_borrowck done");
6967
}
7068

7169
fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
7270
input_mir: &Mir<'gcx>,
73-
def_id: DefId,
74-
src: MirSource)
71+
def_id: DefId)
7572
{
7673
let tcx = infcx.tcx;
7774
let attributes = tcx.get_attrs(def_id);
7875
let param_env = tcx.param_env(def_id);
79-
80-
let id = src.item_id();
76+
let id = tcx.hir.as_local_node_id(def_id)
77+
.expect("do_mir_borrowck: non-local DefId");
8178

8279
let move_data: MoveData<'tcx> = match MoveData::gather_moves(input_mir, tcx, param_env) {
8380
Ok(move_data) => move_data,
@@ -117,7 +114,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
117114
let opt_regioncx = if !tcx.sess.opts.debugging_opts.nll {
118115
None
119116
} else {
120-
Some(nll::compute_regions(infcx, src, mir))
117+
Some(nll::compute_regions(infcx, def_id, mir))
121118
};
122119

123120
let mdpe = MoveDataParamEnv { move_data: move_data, param_env: param_env };

src/librustc_mir/build/mod.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc::hir;
1616
use rustc::hir::def_id::DefId;
1717
use rustc::middle::region;
1818
use rustc::mir::*;
19-
use rustc::mir::transform::MirSource;
2019
use rustc::mir::visit::{MutVisitor, TyContext};
2120
use rustc::ty::{self, Ty, TyCtxt};
2221
use rustc::ty::subst::Substs;
@@ -30,6 +29,7 @@ use syntax::abi::Abi;
3029
use syntax::ast;
3130
use syntax::symbol::keywords;
3231
use syntax_pos::Span;
32+
use transform::MirSource;
3333
use util as mir_util;
3434

3535
/// 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
8383
_ => unsupported(),
8484
};
8585

86-
let src = MirSource::from_node(tcx, id);
8786
tcx.infer_ctxt().enter(|infcx| {
88-
let cx = Cx::new(&infcx, src);
87+
let cx = Cx::new(&infcx, id);
8988
let mut mir = if cx.tables().tainted_by_errors {
9089
build::construct_error(cx, body_id)
91-
} else if let MirSource::Fn(id) = src {
90+
} else if let hir::BodyOwnerKind::Fn = cx.body_owner_kind {
9291
// fetch the fully liberated fn signature (that is, all bound
9392
// types/lifetimes replaced)
9493
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
150149
mem::transmute::<Mir, Mir<'tcx>>(mir)
151150
};
152151

153-
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir, |_, _| Ok(()) );
152+
mir_util::dump_mir(tcx, None, "mir_map", &0,
153+
MirSource::item(def_id), &mir, |_, _| Ok(()) );
154154

155155
mir
156156
})
@@ -214,8 +214,7 @@ fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
214214
let span = tcx.hir.span(ctor_id);
215215
if let hir::VariantData::Tuple(ref fields, ctor_id) = *v {
216216
tcx.infer_ctxt().enter(|infcx| {
217-
let (mut mir, src) =
218-
shim::build_adt_ctor(&infcx, ctor_id, fields, span);
217+
let mut mir = shim::build_adt_ctor(&infcx, ctor_id, fields, span);
219218

220219
// Convert the Mir to global types.
221220
let tcx = infcx.tcx.global_tcx();
@@ -228,7 +227,9 @@ fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
228227
mem::transmute::<Mir, Mir<'tcx>>(mir)
229228
};
230229

231-
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir, |_, _| Ok(()) );
230+
mir_util::dump_mir(tcx, None, "mir_map", &0,
231+
MirSource::item(tcx.hir.local_def_id(ctor_id)),
232+
&mir, |_, _| Ok(()) );
232233

233234
mir
234235
})

src/librustc_mir/build/scope.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ use build::{BlockAnd, BlockAndExtension, Builder, CFG};
9191
use hair::LintLevel;
9292
use rustc::middle::region;
9393
use rustc::ty::{Ty, TyCtxt};
94+
use rustc::hir;
9495
use rustc::hir::def_id::LOCAL_CRATE;
9596
use rustc::mir::*;
96-
use rustc::mir::transform::MirSource;
9797
use syntax_pos::{Span};
9898
use rustc_data_structures::indexed_vec::Idx;
9999
use rustc_data_structures::fx::FxHashMap;
@@ -596,15 +596,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
596596
/// When building statics/constants, returns `None` since
597597
/// intermediate values do not have to be dropped in that case.
598598
pub fn local_scope(&self) -> Option<region::Scope> {
599-
match self.hir.src {
600-
MirSource::Const(_) |
601-
MirSource::Static(..) =>
599+
match self.hir.body_owner_kind {
600+
hir::BodyOwnerKind::Const |
601+
hir::BodyOwnerKind::Static(_) =>
602602
// No need to free storage in this context.
603603
None,
604-
MirSource::Fn(_) =>
604+
hir::BodyOwnerKind::Fn =>
605605
Some(self.topmost_scope()),
606-
MirSource::Promoted(..) =>
607-
bug!(),
608606
}
609607
}
610608

src/librustc_mir/hair/cx/mod.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//!
1616
1717
use hair::*;
18-
use rustc::mir::transform::MirSource;
1918

2019
use rustc::middle::const_val::{ConstEvalErr, ConstVal};
2120
use rustc_const_eval::ConstContext;
@@ -51,30 +50,29 @@ pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
5150
/// `const`, or the body of a `const fn`.
5251
constness: hir::Constness,
5352

54-
/// What are we compiling?
55-
pub src: MirSource,
53+
/// What kind of body is being compiled.
54+
pub body_owner_kind: hir::BodyOwnerKind,
5655

5756
/// True if this constant/function needs overflow checks.
5857
check_overflow: bool,
5958
}
6059

6160
impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
6261
pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
63-
src: MirSource) -> Cx<'a, 'gcx, 'tcx> {
64-
let constness = match src {
65-
MirSource::Const(_) |
66-
MirSource::Static(..) => hir::Constness::Const,
67-
MirSource::Fn(id) => {
68-
let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(id));
62+
src_id: ast::NodeId) -> Cx<'a, 'gcx, 'tcx> {
63+
let tcx = infcx.tcx;
64+
let src_def_id = tcx.hir.local_def_id(src_id);
65+
let body_owner_kind = tcx.hir.body_owner_kind(src_id);
66+
67+
let constness = match body_owner_kind {
68+
hir::BodyOwnerKind::Const |
69+
hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
70+
hir::BodyOwnerKind::Fn => {
71+
let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(src_id));
6972
fn_like.map_or(hir::Constness::NotConst, |f| f.constness())
7073
}
71-
MirSource::Promoted(..) => bug!(),
7274
};
7375

74-
let tcx = infcx.tcx;
75-
let src_id = src.item_id();
76-
let src_def_id = tcx.hir.local_def_id(src_id);
77-
7876
let attrs = tcx.hir.attrs(src_id);
7977

8078
// Some functions always have overflow checks enabled,
@@ -99,7 +97,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
9997
region_scope_tree: tcx.region_scope_tree(src_def_id),
10098
tables: tcx.typeck_tables_of(src_def_id),
10199
constness,
102-
src,
100+
body_owner_kind,
103101
check_overflow,
104102
}
105103
}

src/librustc_mir/shim.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc::hir::def_id::DefId;
1313
use rustc::infer;
1414
use rustc::middle::const_val::ConstVal;
1515
use rustc::mir::*;
16-
use rustc::mir::transform::MirSource;
1716
use rustc::ty::{self, Ty, TyCtxt};
1817
use rustc::ty::subst::{Kind, Subst, Substs};
1918
use rustc::ty::maps::Providers;
@@ -826,7 +825,7 @@ pub fn build_adt_ctor<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a, 'gcx, 'tcx>,
826825
ctor_id: ast::NodeId,
827826
fields: &[hir::StructField],
828827
span: Span)
829-
-> (Mir<'tcx>, MirSource)
828+
-> Mir<'tcx>
830829
{
831830
let tcx = infcx.tcx;
832831
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>,
875874
is_cleanup: false
876875
};
877876

878-
let mir = Mir::new(
877+
Mir::new(
879878
IndexVec::from_elem_n(start_block, 1),
880879
IndexVec::from_elem_n(
881880
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>,
888887
sig.inputs().len(),
889888
vec![],
890889
span
891-
);
892-
(mir, MirSource::Fn(ctor_id))
890+
)
893891
}

src/librustc_mir/transform/add_call_guards.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
use rustc::ty::TyCtxt;
1212
use rustc::mir::*;
13-
use rustc::mir::transform::MirSource;
1413
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
15-
use transform::MirPass;
14+
use transform::{MirPass, MirSource};
1615

1716
#[derive(PartialEq)]
1817
pub enum AddCallGuards {

0 commit comments

Comments
 (0)