Skip to content

Commit

Permalink
rustc: move mir::SourceScopeLocalData to a field of SourceScopeData.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Nov 29, 2019
1 parent 78d85fc commit a9976d8
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 44 deletions.
10 changes: 4 additions & 6 deletions src/librustc/mir/mod.rs
Expand Up @@ -104,10 +104,6 @@ pub struct Body<'tcx> {
/// and used for debuginfo. Indexed by a `SourceScope`. /// and used for debuginfo. Indexed by a `SourceScope`.
pub source_scopes: IndexVec<SourceScope, SourceScopeData>, pub source_scopes: IndexVec<SourceScope, SourceScopeData>,


/// Crate-local information for each source scope, that can't (and
/// needn't) be tracked across crates.
pub source_scope_local_data: IndexVec<SourceScope, ClearCrossCrate<SourceScopeLocalData>>,

/// The yield type of the function, if it is a generator. /// The yield type of the function, if it is a generator.
pub yield_ty: Option<Ty<'tcx>>, pub yield_ty: Option<Ty<'tcx>>,


Expand Down Expand Up @@ -167,7 +163,6 @@ impl<'tcx> Body<'tcx> {
pub fn new( pub fn new(
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>, basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
source_scopes: IndexVec<SourceScope, SourceScopeData>, source_scopes: IndexVec<SourceScope, SourceScopeData>,
source_scope_local_data: IndexVec<SourceScope, ClearCrossCrate<SourceScopeLocalData>>,
local_decls: LocalDecls<'tcx>, local_decls: LocalDecls<'tcx>,
user_type_annotations: CanonicalUserTypeAnnotations<'tcx>, user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
arg_count: usize, arg_count: usize,
Expand All @@ -188,7 +183,6 @@ impl<'tcx> Body<'tcx> {
phase: MirPhase::Build, phase: MirPhase::Build,
basic_blocks, basic_blocks,
source_scopes, source_scopes,
source_scope_local_data,
yield_ty: None, yield_ty: None,
generator_drop: None, generator_drop: None,
generator_layout: None, generator_layout: None,
Expand Down Expand Up @@ -2034,6 +2028,10 @@ rustc_index::newtype_index! {
pub struct SourceScopeData { pub struct SourceScopeData {
pub span: Span, pub span: Span,
pub parent_scope: Option<SourceScope>, pub parent_scope: Option<SourceScope>,

/// Crate-local information for this source scope, that can't (and
/// needn't) be tracked across crates.
pub local_data: ClearCrossCrate<SourceScopeLocalData>,
} }


#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)] #[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
Expand Down
1 change: 1 addition & 0 deletions src/librustc/mir/visit.rs
Expand Up @@ -317,6 +317,7 @@ macro_rules! make_mir_visitor {
let SourceScopeData { let SourceScopeData {
span, span,
parent_scope, parent_scope,
local_data: _,
} = scope_data; } = scope_data;


self.visit_span(span); self.visit_span(span);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/mod.rs
Expand Up @@ -301,7 +301,7 @@ fn do_mir_borrowck<'a, 'tcx>(
mbcx.report_conflicting_borrow(location, (&place, span), bk, &borrow); mbcx.report_conflicting_borrow(location, (&place, span), bk, &borrow);


let scope = mbcx.body.source_info(location).scope; let scope = mbcx.body.source_info(location).scope;
let lint_root = match &mbcx.body.source_scope_local_data[scope] { let lint_root = match &mbcx.body.source_scopes[scope].local_data {
ClearCrossCrate::Set(data) => data.lint_root, ClearCrossCrate::Set(data) => data.lint_root,
_ => id, _ => id,
}; };
Expand Down Expand Up @@ -338,7 +338,7 @@ fn do_mir_borrowck<'a, 'tcx>(
let used_mut = mbcx.used_mut; let used_mut = mbcx.used_mut;
for local in mbcx.body.mut_vars_and_args_iter().filter(|local| !used_mut.contains(local)) { for local in mbcx.body.mut_vars_and_args_iter().filter(|local| !used_mut.contains(local)) {
let local_decl = &mbcx.body.local_decls[local]; let local_decl = &mbcx.body.local_decls[local];
let lint_root = match &mbcx.body.source_scope_local_data[local_decl.source_info.scope] { let lint_root = match &mbcx.body.source_scopes[local_decl.source_info.scope].local_data {
ClearCrossCrate::Set(data) => data.lint_root, ClearCrossCrate::Set(data) => data.lint_root,
_ => continue, _ => continue,
}; };
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_mir/build/mod.rs
Expand Up @@ -309,7 +309,6 @@ struct Builder<'a, 'tcx> {
/// The vector of all scopes that we have created thus far; /// The vector of all scopes that we have created thus far;
/// we track this for debuginfo later. /// we track this for debuginfo later.
source_scopes: IndexVec<SourceScope, SourceScopeData>, source_scopes: IndexVec<SourceScope, SourceScopeData>,
source_scope_local_data: IndexVec<SourceScope, ClearCrossCrate<SourceScopeLocalData>>,
source_scope: SourceScope, source_scope: SourceScope,


/// The guard-context: each time we build the guard expression for /// The guard-context: each time we build the guard expression for
Expand Down Expand Up @@ -704,7 +703,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block_context: BlockContext::new(), block_context: BlockContext::new(),
source_scopes: IndexVec::new(), source_scopes: IndexVec::new(),
source_scope: OUTERMOST_SOURCE_SCOPE, source_scope: OUTERMOST_SOURCE_SCOPE,
source_scope_local_data: IndexVec::new(),
guard_context: vec![], guard_context: vec![],
push_unsafe_count: 0, push_unsafe_count: 0,
unpushed_unsafe: safety, unpushed_unsafe: safety,
Expand Down Expand Up @@ -741,7 +739,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Body::new( Body::new(
self.cfg.basic_blocks, self.cfg.basic_blocks,
self.source_scopes, self.source_scopes,
self.source_scope_local_data,
self.local_decls, self.local_decls,
self.canonical_user_type_annotations, self.canonical_user_type_annotations,
self.arg_count, self.arg_count,
Expand Down Expand Up @@ -942,7 +939,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
self.hir.root_lint_level self.hir.root_lint_level
); );
let parent_root = tcx.maybe_lint_level_root_bounded( let parent_root = tcx.maybe_lint_level_root_bounded(
self.source_scope_local_data[original_source_scope] self.source_scopes[original_source_scope]
.local_data
.as_ref() .as_ref()
.assert_crate_local() .assert_crate_local()
.lint_root, .lint_root,
Expand Down
20 changes: 10 additions & 10 deletions src/librustc_mir/build/scope.rs
Expand Up @@ -436,7 +436,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// We estimate the true lint roots here to avoid creating a lot of source scopes. // We estimate the true lint roots here to avoid creating a lot of source scopes.


let parent_root = tcx.maybe_lint_level_root_bounded( let parent_root = tcx.maybe_lint_level_root_bounded(
self.source_scope_local_data[source_scope] self.source_scopes[source_scope]
.local_data
.as_ref() .as_ref()
.assert_crate_local() .assert_crate_local()
.lint_root, .lint_root,
Expand Down Expand Up @@ -657,23 +658,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let parent = self.source_scope; let parent = self.source_scope;
debug!("new_source_scope({:?}, {:?}, {:?}) - parent({:?})={:?}", debug!("new_source_scope({:?}, {:?}, {:?}) - parent({:?})={:?}",
span, lint_level, safety, span, lint_level, safety,
parent, self.source_scope_local_data.get(parent)); parent, self.source_scopes.get(parent));
let scope = self.source_scopes.push(SourceScopeData {
span,
parent_scope: Some(parent),
});
let scope_local_data = SourceScopeLocalData { let scope_local_data = SourceScopeLocalData {
lint_root: if let LintLevel::Explicit(lint_root) = lint_level { lint_root: if let LintLevel::Explicit(lint_root) = lint_level {
lint_root lint_root
} else { } else {
self.source_scope_local_data[parent].as_ref().assert_crate_local().lint_root self.source_scopes[parent].local_data.as_ref().assert_crate_local().lint_root
}, },
safety: safety.unwrap_or_else(|| { safety: safety.unwrap_or_else(|| {
self.source_scope_local_data[parent].as_ref().assert_crate_local().safety self.source_scopes[parent].local_data.as_ref().assert_crate_local().safety
}) })
}; };
self.source_scope_local_data.push(ClearCrossCrate::Set(scope_local_data)); self.source_scopes.push(SourceScopeData {
scope span,
parent_scope: Some(parent),
local_data: ClearCrossCrate::Set(scope_local_data),
})
} }


/// Given a span and the current source scope, make a SourceInfo. /// Given a span and the current source scope, make a SourceInfo.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/eval_context.rs
Expand Up @@ -849,7 +849,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
} else { } else {
block.terminator().source_info block.terminator().source_info
}; };
match &body.source_scope_local_data[source_info.scope] { match &body.source_scopes[source_info.scope].local_data {
mir::ClearCrossCrate::Set(data) => Some(data.lint_root), mir::ClearCrossCrate::Set(data) => Some(data.lint_root),
mir::ClearCrossCrate::Clear => None, mir::ClearCrossCrate::Clear => None,
} }
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_mir/shim.rs
Expand Up @@ -248,10 +248,8 @@ fn new_body<'tcx>(
Body::new( Body::new(
basic_blocks, basic_blocks,
IndexVec::from_elem_n( IndexVec::from_elem_n(
SourceScopeData { span, parent_scope: None }, 1 SourceScopeData { span, parent_scope: None, local_data: ClearCrossCrate::Clear },
), 1,
IndexVec::from_elem_n(
ClearCrossCrate::Clear, 1
), ),
local_decls, local_decls,
IndexVec::new(), IndexVec::new(),
Expand Down
6 changes: 4 additions & 2 deletions src/librustc_mir/transform/check_unsafety.rs
Expand Up @@ -214,7 +214,8 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
if context.is_borrow() { if context.is_borrow() {
if util::is_disaligned(self.tcx, self.body, self.param_env, place) { if util::is_disaligned(self.tcx, self.body, self.param_env, place) {
let source_info = self.source_info; let source_info = self.source_info;
let lint_root = self.body.source_scope_local_data[source_info.scope] let lint_root = self.body.source_scopes[source_info.scope]
.local_data
.as_ref() .as_ref()
.assert_crate_local() .assert_crate_local()
.lint_root; .lint_root;
Expand Down Expand Up @@ -343,7 +344,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
fn register_violations(&mut self, fn register_violations(&mut self,
violations: &[UnsafetyViolation], violations: &[UnsafetyViolation],
unsafe_blocks: &[(hir::HirId, bool)]) { unsafe_blocks: &[(hir::HirId, bool)]) {
let safety = self.body.source_scope_local_data[self.source_info.scope] let safety = self.body.source_scopes[self.source_info.scope]
.local_data
.as_ref() .as_ref()
.assert_crate_local() .assert_crate_local()
.safety; .safety;
Expand Down
17 changes: 8 additions & 9 deletions src/librustc_mir/transform/const_prop.rs
Expand Up @@ -9,7 +9,7 @@ use rustc::hir::def_id::DefId;
use rustc::mir::{ use rustc::mir::{
AggregateKind, Constant, Location, Place, PlaceBase, Body, Operand, Rvalue, Local, UnOp, AggregateKind, Constant, Location, Place, PlaceBase, Body, Operand, Rvalue, Local, UnOp,
StatementKind, Statement, LocalKind, TerminatorKind, Terminator, ClearCrossCrate, SourceInfo, StatementKind, Statement, LocalKind, TerminatorKind, Terminator, ClearCrossCrate, SourceInfo,
BinOp, SourceScope, SourceScopeLocalData, LocalDecl, BasicBlock, RETURN_PLACE, BinOp, SourceScope, SourceScopeData, LocalDecl, BasicBlock, RETURN_PLACE,
}; };
use rustc::mir::visit::{ use rustc::mir::visit::{
Visitor, PlaceContext, MutatingUseContext, MutVisitor, NonMutatingUseContext, Visitor, PlaceContext, MutatingUseContext, MutVisitor, NonMutatingUseContext,
Expand Down Expand Up @@ -77,8 +77,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
let dummy_body = let dummy_body =
&Body::new( &Body::new(
body.basic_blocks().clone(), body.basic_blocks().clone(),
Default::default(), body.source_scopes.clone(),
body.source_scope_local_data.clone(),
body.local_decls.clone(), body.local_decls.clone(),
Default::default(), Default::default(),
body.arg_count, body.arg_count,
Expand Down Expand Up @@ -254,7 +253,7 @@ struct ConstPropagator<'mir, 'tcx> {
param_env: ParamEnv<'tcx>, param_env: ParamEnv<'tcx>,
// FIXME(eddyb) avoid cloning these two fields more than once, // FIXME(eddyb) avoid cloning these two fields more than once,
// by accessing them through `ecx` instead. // by accessing them through `ecx` instead.
source_scope_local_data: IndexVec<SourceScope, ClearCrossCrate<SourceScopeLocalData>>, source_scopes: IndexVec<SourceScope, SourceScopeData>,
local_decls: IndexVec<Local, LocalDecl<'tcx>>, local_decls: IndexVec<Local, LocalDecl<'tcx>>,
ret: Option<OpTy<'tcx, ()>>, ret: Option<OpTy<'tcx, ()>>,
} }
Expand Down Expand Up @@ -325,7 +324,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
can_const_prop, can_const_prop,
// FIXME(eddyb) avoid cloning these two fields more than once, // FIXME(eddyb) avoid cloning these two fields more than once,
// by accessing them through `ecx` instead. // by accessing them through `ecx` instead.
source_scope_local_data: body.source_scope_local_data.clone(), source_scopes: body.source_scopes.clone(),
//FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it //FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
local_decls: body.local_decls.clone(), local_decls: body.local_decls.clone(),
ret: ret.map(Into::into), ret: ret.map(Into::into),
Expand Down Expand Up @@ -362,9 +361,9 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
{ {
self.ecx.tcx.span = source_info.span; self.ecx.tcx.span = source_info.span;
// FIXME(eddyb) move this to the `Panic(_)` error case, so that // FIXME(eddyb) move this to the `Panic(_)` error case, so that
// `f(self)` is always called, and that the only difference when // `f(self)` is always called, and that the only difference when the
// `source_scope_local_data` is missing, is that the lint isn't emitted. // scope's `local_data` is missing, is that the lint isn't emitted.
let lint_root = match &self.source_scope_local_data[source_info.scope] { let lint_root = match &self.source_scopes[source_info.scope].local_data {
ClearCrossCrate::Set(data) => data.lint_root, ClearCrossCrate::Set(data) => data.lint_root,
ClearCrossCrate::Clear => return None, ClearCrossCrate::Clear => return None,
}; };
Expand Down Expand Up @@ -489,7 +488,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
let right_size = r.layout.size; let right_size = r.layout.size;
let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size)); let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
if r_bits.ok().map_or(false, |b| b >= left_bits as u128) { if r_bits.ok().map_or(false, |b| b >= left_bits as u128) {
let lint_root = match &self.source_scope_local_data[source_info.scope] { let lint_root = match &self.source_scopes[source_info.scope].local_data {
ClearCrossCrate::Set(data) => data.lint_root, ClearCrossCrate::Set(data) => data.lint_root,
ClearCrossCrate::Clear => return None, ClearCrossCrate::Clear => return None,
}; };
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/transform/inline.rs
Expand Up @@ -388,8 +388,7 @@ impl Inliner<'tcx> {
let mut local_map = IndexVec::with_capacity(callee_body.local_decls.len()); let mut local_map = IndexVec::with_capacity(callee_body.local_decls.len());
let mut scope_map = IndexVec::with_capacity(callee_body.source_scopes.len()); let mut scope_map = IndexVec::with_capacity(callee_body.source_scopes.len());


for (callee_idx, scope) in callee_body.source_scopes.iter_enumerated() { for mut scope in callee_body.source_scopes.iter().cloned() {
let mut scope = scope.clone();
if scope.parent_scope.is_none() { if scope.parent_scope.is_none() {
scope.parent_scope = Some(callsite.location.scope); scope.parent_scope = Some(callsite.location.scope);
// FIXME(eddyb) is this really needed? // FIXME(eddyb) is this really needed?
Expand All @@ -404,9 +403,6 @@ impl Inliner<'tcx> {


let idx = caller_body.source_scopes.push(scope); let idx = caller_body.source_scopes.push(scope);
scope_map.push(idx); scope_map.push(idx);

let local_data = callee_body.source_scope_local_data[callee_idx].clone();
assert_eq!(idx, caller_body.source_scope_local_data.push(local_data));
} }


for loc in callee_body.vars_and_temps_iter() { for loc in callee_body.vars_and_temps_iter() {
Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/transform/promote_consts.rs
Expand Up @@ -1081,7 +1081,6 @@ pub fn promote_candidates<'tcx>(
// FIXME: maybe try to filter this to avoid blowing up // FIXME: maybe try to filter this to avoid blowing up
// memory usage? // memory usage?
body.source_scopes.clone(), body.source_scopes.clone(),
body.source_scope_local_data.clone(),
initial_locals, initial_locals,
IndexVec::new(), IndexVec::new(),
0, 0,
Expand Down

0 comments on commit a9976d8

Please sign in to comment.