Skip to content

Commit

Permalink
mir: group span + visibility scope under a new SourceInfo type.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jun 7, 2016
1 parent 719a591 commit 0c5930e
Show file tree
Hide file tree
Showing 28 changed files with 255 additions and 272 deletions.
25 changes: 16 additions & 9 deletions src/librustc/mir/repr.rs
Expand Up @@ -100,6 +100,18 @@ impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
}
}

/// Grouped information about the source code origin of a MIR entity.
/// Intended to be inspected by diagnostics and debuginfo.
/// Most passes can work with it as a whole, within a single function.
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
pub struct SourceInfo {
/// Source span for the AST pertaining to this MIR entity.
pub span: Span,

/// The lexical visibility scope, i.e. which bindings can be seen.
pub scope: VisibilityScope
}

///////////////////////////////////////////////////////////////////////////
// Mutability and borrow kinds

Expand Down Expand Up @@ -172,11 +184,8 @@ pub struct VarDecl<'tcx> {
/// type inferred for this variable (`let x: ty = ...`)
pub ty: Ty<'tcx>,

/// scope in which variable was declared
pub scope: VisibilityScope,

/// span where variable was declared
pub span: Span,
/// source information (span, scope, etc.) for the declaration
pub source_info: SourceInfo,
}

/// A "temp" is a temporary that we place on the stack. They are
Expand Down Expand Up @@ -275,8 +284,7 @@ pub struct BasicBlockData<'tcx> {

#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct Terminator<'tcx> {
pub span: Span,
pub scope: VisibilityScope,
pub source_info: SourceInfo,
pub kind: TerminatorKind<'tcx>
}

Expand Down Expand Up @@ -587,8 +595,7 @@ pub enum AssertMessage<'tcx> {

#[derive(Clone, RustcEncodable, RustcDecodable)]
pub struct Statement<'tcx> {
pub span: Span,
pub scope: VisibilityScope,
pub source_info: SourceInfo,
pub kind: StatementKind<'tcx>,
}

Expand Down
33 changes: 21 additions & 12 deletions src/librustc/mir/visit.rs
Expand Up @@ -186,6 +186,11 @@ macro_rules! make_mir_visitor {
self.super_span(span);
}

fn visit_source_info(&mut self,
source_info: & $($mutability)* SourceInfo) {
self.super_source_info(source_info);
}

fn visit_fn_output(&mut self,
fn_output: & $($mutability)* FnOutput<'tcx>) {
self.super_fn_output(fn_output);
Expand Down Expand Up @@ -319,13 +324,11 @@ macro_rules! make_mir_visitor {
block: BasicBlock,
statement: & $($mutability)* Statement<'tcx>) {
let Statement {
ref $($mutability)* span,
ref $($mutability)* scope,
ref $($mutability)* source_info,
ref $($mutability)* kind,
} = *statement;

self.visit_span(span);
self.visit_visibility_scope(scope);
self.visit_source_info(source_info);
match *kind {
StatementKind::Assign(ref $($mutability)* lvalue,
ref $($mutability)* rvalue) => {
Expand All @@ -346,13 +349,11 @@ macro_rules! make_mir_visitor {
block: BasicBlock,
terminator: &$($mutability)* Terminator<'tcx>) {
let Terminator {
ref $($mutability)* span,
ref $($mutability)* scope,
ref $($mutability)* source_info,
ref $($mutability)* kind,
} = *terminator;

self.visit_span(span);
self.visit_visibility_scope(scope);
self.visit_source_info(source_info);
self.visit_terminator_kind(block, kind);
}

Expand Down Expand Up @@ -622,13 +623,11 @@ macro_rules! make_mir_visitor {
mutability: _,
name: _,
ref $($mutability)* ty,
ref $($mutability)* scope,
ref $($mutability)* span,
ref $($mutability)* source_info,
} = *var_decl;

self.visit_ty(ty);
self.visit_visibility_scope(scope);
self.visit_span(span);
self.visit_source_info(source_info);
}

fn super_temp_decl(&mut self,
Expand Down Expand Up @@ -707,6 +706,16 @@ macro_rules! make_mir_visitor {
fn super_span(&mut self, _span: & $($mutability)* Span) {
}

fn super_source_info(&mut self, source_info: & $($mutability)* SourceInfo) {
let SourceInfo {
ref $($mutability)* span,
ref $($mutability)* scope,
} = *source_info;

self.visit_span(span);
self.visit_visibility_scope(scope);
}

fn super_fn_output(&mut self, fn_output: & $($mutability)* FnOutput<'tcx>) {
match *fn_output {
FnOutput::FnConverging(ref $($mutability)* ty) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_borrowck/borrowck/mir/dataflow/sanity_check.rs
Expand Up @@ -151,7 +151,7 @@ fn each_block<'a, 'tcx, O>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
fn is_rustc_peek<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
terminator: &'a Option<repr::Terminator<'tcx>>)
-> Option<(&'a [repr::Operand<'tcx>], Span)> {
if let Some(repr::Terminator { ref kind, span, .. }) = *terminator {
if let Some(repr::Terminator { ref kind, source_info, .. }) = *terminator {
if let repr::TerminatorKind::Call { func: ref oper, ref args, .. } = *kind
{
if let repr::Operand::Constant(ref func) = *oper
Expand All @@ -161,7 +161,7 @@ fn is_rustc_peek<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let name = tcx.item_name(def_id);
if abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic {
if name.as_str() == "rustc_peek" {
return Some((args, span));
return Some((args, source_info.span));
}
}
}
Expand Down
35 changes: 14 additions & 21 deletions src/librustc_borrowck/borrowck/mir/elaborate_drops.rs
Expand Up @@ -124,8 +124,7 @@ struct ElaborateDropsCtxt<'a, 'tcx: 'a> {

#[derive(Copy, Clone, Debug)]
struct DropCtxt<'a, 'tcx: 'a> {
span: Span,
scope: ScopeId,
source_info: SourceInfo,
is_cleanup: bool,

init_data: &'a InitializationData,
Expand Down Expand Up @@ -273,8 +272,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
let init_data = self.initialization_data_at(loc);
let path = self.move_data().rev_lookup.find(location);
self.elaborate_drop(&DropCtxt {
span: terminator.span,
scope: terminator.scope,
source_info: terminator.source_info,
is_cleanup: data.is_cleanup,
init_data: &init_data,
lvalue: location,
Expand Down Expand Up @@ -329,8 +327,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {

let assign = Statement {
kind: StatementKind::Assign(location.clone(), Rvalue::Use(value.clone())),
span: terminator.span,
scope: terminator.scope
source_info: terminator.source_info
};

let unwind = unwind.unwrap_or(self.patch.resume_block());
Expand Down Expand Up @@ -367,8 +364,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
let path = self.move_data().rev_lookup.find(location);

self.elaborate_drop(&DropCtxt {
span: terminator.span,
scope: terminator.scope,
source_info: terminator.source_info,
is_cleanup: data.is_cleanup,
init_data: &init_data,
lvalue: location,
Expand Down Expand Up @@ -513,8 +509,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
debug!("drop_ladder: for std field {} ({:?})", i, lv);

self.elaborated_drop_block(&DropCtxt {
span: c.span,
scope: c.scope,
source_info: c.source_info,
is_cleanup: is_cleanup,
init_data: c.init_data,
lvalue: lv,
Expand All @@ -527,8 +522,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
debug!("drop_ladder: for rest field {} ({:?})", i, lv);

let blk = self.complete_drop(&DropCtxt {
span: c.span,
scope: c.scope,
source_info: c.source_info,
is_cleanup: is_cleanup,
init_data: c.init_data,
lvalue: lv,
Expand Down Expand Up @@ -785,7 +779,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
self.patch.new_block(BasicBlockData {
statements: vec![],
terminator: Some(Terminator {
scope: c.scope, span: c.span, kind: k
source_info: c.source_info, kind: k
}),
is_cleanup: is_cleanup
})
Expand Down Expand Up @@ -858,11 +852,10 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
let mut statements = vec![];
if let Some(&flag) = self.drop_flags.get(&c.path) {
statements.push(Statement {
span: c.span,
scope: c.scope,
source_info: c.source_info,
kind: StatementKind::Assign(
Lvalue::Temp(flag),
self.constant_bool(c.span, false)
self.constant_bool(c.source_info.span, false)
)
});
}
Expand All @@ -880,9 +873,9 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
self.patch.new_block(BasicBlockData {
statements: statements,
terminator: Some(Terminator {
scope: c.scope, span: c.span, kind: TerminatorKind::Call {
source_info: c.source_info, kind: TerminatorKind::Call {
func: Operand::Constant(Constant {
span: c.span,
span: c.source_info.span,
ty: fty,
literal: Literal::Item {
def_id: free_func,
Expand Down Expand Up @@ -910,7 +903,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
ty::TyStruct(def, _) | ty::TyEnum(def, _) => {
if def.has_dtor() {
self.tcx.sess.span_warn(
c.span,
c.source_info.span,
&format!("dataflow bug??? moving out of type with dtor {:?}",
c));
true
Expand All @@ -932,15 +925,15 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {

fn set_drop_flag(&mut self, loc: Location, path: MovePathIndex, val: DropFlagState) {
if let Some(&flag) = self.drop_flags.get(&path) {
let span = self.patch.context_for_location(self.mir, loc).0;
let span = self.patch.source_info_for_location(self.mir, loc).span;
let val = self.constant_bool(span, val.value());
self.patch.add_assign(loc, Lvalue::Temp(flag), val);
}
}

fn drop_flags_on_init(&mut self) {
let loc = Location { block: START_BLOCK, index: 0 };
let span = self.patch.context_for_location(self.mir, loc).0;
let span = self.patch.source_info_for_location(self.mir, loc).span;
let false_ = self.constant_bool(span, false);
for flag in self.drop_flags.values() {
self.patch.add_assign(loc, Lvalue::Temp(*flag), false_.clone());
Expand Down
22 changes: 11 additions & 11 deletions src/librustc_borrowck/borrowck/mir/patch.rs
Expand Up @@ -11,7 +11,6 @@
use super::gather_moves::Location;
use rustc::ty::Ty;
use rustc::mir::repr::*;
use syntax::codemap::Span;

use std::iter;
use std::u32;
Expand Down Expand Up @@ -62,8 +61,10 @@ impl<'tcx> MirPatch<'tcx> {
result.new_block(BasicBlockData {
statements: vec![],
terminator: Some(Terminator {
span: mir.span,
scope: ScopeId::new(0),
source_info: SourceInfo {
span: mir.span,
scope: ARGUMENT_VISIBILITY_SCOPE
},
kind: TerminatorKind::Resume
}),
is_cleanup: true
Expand Down Expand Up @@ -154,31 +155,30 @@ impl<'tcx> MirPatch<'tcx> {
debug!("MirPatch: adding statement {:?} at loc {:?}+{}",
stmt, loc, delta);
loc.index += delta;
let (span, scope) = Self::context_for_index(
let source_info = Self::source_info_for_index(
mir.basic_block_data(loc.block), loc
);
mir.basic_block_data_mut(loc.block).statements.insert(
loc.index, Statement {
span: span,
scope: scope,
source_info: source_info,
kind: stmt
});
delta += 1;
}
}

pub fn context_for_index(data: &BasicBlockData, loc: Location) -> (Span, ScopeId) {
pub fn source_info_for_index(data: &BasicBlockData, loc: Location) -> SourceInfo {
match data.statements.get(loc.index) {
Some(stmt) => (stmt.span, stmt.scope),
None => (data.terminator().span, data.terminator().scope)
Some(stmt) => stmt.source_info,
None => data.terminator().source_info
}
}

pub fn context_for_location(&self, mir: &Mir, loc: Location) -> (Span, ScopeId) {
pub fn source_info_for_location(&self, mir: &Mir, loc: Location) -> SourceInfo {
let data = match loc.block.index().checked_sub(mir.basic_blocks.len()) {
Some(new) => &self.new_blocks[new],
None => mir.basic_block_data(loc.block)
};
Self::context_for_index(data, loc)
Self::source_info_for_index(data, loc)
}
}
4 changes: 2 additions & 2 deletions src/librustc_mir/build/block.rs
Expand Up @@ -83,8 +83,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
unpack!(block = this.into(destination, block, expr));
} else if dest_is_unit {
// FIXME(#31472)
let scope_id = this.innermost_scope_id();
this.cfg.push_assign_unit(block, scope_id, span, destination);
let source_info = this.source_info(span);
this.cfg.push_assign_unit(block, source_info, destination);
}
// Finally, we pop all the let scopes before exiting out from the scope of block
// itself.
Expand Down

0 comments on commit 0c5930e

Please sign in to comment.