Add SourceInfo::outermost.

This commit is contained in:
Nicholas Nethercote 2020-05-06 10:30:11 +10:00
parent a0c61a9044
commit a17234ca54
11 changed files with 45 additions and 53 deletions

View File

@ -474,6 +474,13 @@ pub struct SourceInfo {
pub scope: SourceScope, pub scope: SourceScope,
} }
impl SourceInfo {
#[inline]
pub fn outermost(span: Span) -> Self {
SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }
}
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// Borrow kinds // Borrow kinds
@ -944,7 +951,7 @@ impl<'tcx> LocalDecl<'tcx> {
mutability, mutability,
ty, ty,
user_ty: UserTypeProjections::none(), user_ty: UserTypeProjections::none(),
source_info: SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }, source_info: SourceInfo::outermost(span),
internal, internal,
local_info: LocalInfo::Other, local_info: LocalInfo::Other,
is_block_tail: None, is_block_tail: None,
@ -960,7 +967,7 @@ impl<'tcx> LocalDecl<'tcx> {
mutability: Mutability::Mut, mutability: Mutability::Mut,
ty: return_ty, ty: return_ty,
user_ty: UserTypeProjections::none(), user_ty: UserTypeProjections::none(),
source_info: SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }, source_info: SourceInfo::outermost(span),
internal: false, internal: false,
is_block_tail: None, is_block_tail: None,
local_info: LocalInfo::Other, local_info: LocalInfo::Other,
@ -1406,10 +1413,7 @@ impl<'tcx> BasicBlockData<'tcx> {
let mut gap = self.statements.len()..self.statements.len() + extra_stmts; let mut gap = self.statements.len()..self.statements.len() + extra_stmts;
self.statements.resize( self.statements.resize(
gap.end, gap.end,
Statement { Statement { source_info: SourceInfo::outermost(DUMMY_SP), kind: StatementKind::Nop },
source_info: SourceInfo { span: DUMMY_SP, scope: OUTERMOST_SOURCE_SCOPE },
kind: StatementKind::Nop,
},
); );
for (splice_start, new_stmts) in splices.into_iter().rev() { for (splice_start, new_stmts) in splices.into_iter().rev() {
let splice_end = splice_start + new_stmts.size_hint().0; let splice_end = splice_start + new_stmts.size_hint().0;

View File

@ -242,10 +242,10 @@ macro_rules! make_mir_visitor {
) { ) {
let span = body.span; let span = body.span;
if let Some(yield_ty) = &$($mutability)? body.yield_ty { if let Some(yield_ty) = &$($mutability)? body.yield_ty {
self.visit_ty(yield_ty, TyContext::YieldTy(SourceInfo { self.visit_ty(
span, yield_ty,
scope: OUTERMOST_SOURCE_SCOPE, TyContext::YieldTy(SourceInfo::outermost(span))
})); );
} }
// for best performance, we want to use an iterator rather // for best performance, we want to use an iterator rather
@ -263,10 +263,10 @@ macro_rules! make_mir_visitor {
self.visit_source_scope_data(scope); self.visit_source_scope_data(scope);
} }
self.visit_ty(&$($mutability)? body.return_ty(), TyContext::ReturnTy(SourceInfo { self.visit_ty(
span: body.span, &$($mutability)? body.return_ty(),
scope: OUTERMOST_SOURCE_SCOPE, TyContext::ReturnTy(SourceInfo::outermost(body.span))
})); );
for local in body.local_decls.indices() { for local in body.local_decls.indices() {
self.visit_local_decl(local, & $($mutability)? body.local_decls[local]); self.visit_local_decl(local, & $($mutability)? body.local_decls[local]);

View File

@ -16,7 +16,7 @@ use crate::dataflow::BottomValue;
/// This is the `Body` that will be used by the `MockAnalysis` below. The shape of its CFG is not /// This is the `Body` that will be used by the `MockAnalysis` below. The shape of its CFG is not
/// important. /// important.
fn mock_body() -> mir::Body<'static> { fn mock_body() -> mir::Body<'static> {
let source_info = mir::SourceInfo { scope: mir::OUTERMOST_SOURCE_SCOPE, span: DUMMY_SP }; let source_info = mir::SourceInfo::outermost(DUMMY_SP);
let mut blocks = IndexVec::new(); let mut blocks = IndexVec::new();
let mut block = |n, kind| { let mut block = |n, kind| {

View File

@ -146,12 +146,11 @@ enum CallKind {
} }
fn temp_decl(mutability: Mutability, ty: Ty<'_>, span: Span) -> LocalDecl<'_> { fn temp_decl(mutability: Mutability, ty: Ty<'_>, span: Span) -> LocalDecl<'_> {
let source_info = SourceInfo { scope: OUTERMOST_SOURCE_SCOPE, span };
LocalDecl { LocalDecl {
mutability, mutability,
ty, ty,
user_ty: UserTypeProjections::none(), user_ty: UserTypeProjections::none(),
source_info, source_info: SourceInfo::outermost(span),
internal: false, internal: false,
local_info: LocalInfo::Other, local_info: LocalInfo::Other,
is_block_tail: None, is_block_tail: None,
@ -185,7 +184,7 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
let sig = tcx.erase_late_bound_regions(&sig); let sig = tcx.erase_late_bound_regions(&sig);
let span = tcx.def_span(def_id); let span = tcx.def_span(def_id);
let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }; let source_info = SourceInfo::outermost(span);
let return_block = BasicBlock::new(1); let return_block = BasicBlock::new(1);
let mut blocks = IndexVec::with_capacity(2); let mut blocks = IndexVec::with_capacity(2);
@ -374,7 +373,7 @@ impl CloneShimBuilder<'tcx> {
} }
fn source_info(&self) -> SourceInfo { fn source_info(&self) -> SourceInfo {
SourceInfo { span: self.span, scope: OUTERMOST_SOURCE_SCOPE } SourceInfo::outermost(self.span)
} }
fn block( fn block(
@ -687,7 +686,7 @@ fn build_call_shim<'tcx>(
debug!("build_call_shim: sig={:?}", sig); debug!("build_call_shim: sig={:?}", sig);
let mut local_decls = local_decls_for_sig(&sig, span); let mut local_decls = local_decls_for_sig(&sig, span);
let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }; let source_info = SourceInfo::outermost(span);
let rcvr_place = || { let rcvr_place = || {
assert!(rcvr_adjustment.is_some()); assert!(rcvr_adjustment.is_some());
@ -849,7 +848,7 @@ pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
let local_decls = local_decls_for_sig(&sig, span); let local_decls = local_decls_for_sig(&sig, span);
let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }; let source_info = SourceInfo::outermost(span);
let variant_index = if adt_def.is_enum() { let variant_index = if adt_def.is_enum() {
adt_def.variant_index_with_ctor_id(ctor_id) adt_def.variant_index_with_ctor_id(ctor_id)

View File

@ -77,11 +77,9 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
// PART 1 // PART 1
// Retag arguments at the beginning of the start block. // Retag arguments at the beginning of the start block.
{ {
let source_info = SourceInfo { // FIXME: Consider using just the span covering the function
scope: OUTERMOST_SOURCE_SCOPE, // argument declaration.
span, // FIXME: Consider using just the span covering the function let source_info = SourceInfo::outermost(span);
// argument declaration.
};
// Gather all arguments, skip return value. // Gather all arguments, skip return value.
let places = local_decls let places = local_decls
.iter_enumerated() .iter_enumerated()

View File

@ -50,7 +50,7 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
const_context, const_context,
min_const_fn, min_const_fn,
violations: vec![], violations: vec![],
source_info: SourceInfo { span: body.span, scope: OUTERMOST_SOURCE_SCOPE }, source_info: SourceInfo::outermost(body.span),
tcx, tcx,
param_env, param_env,
used_unsafe: Default::default(), used_unsafe: Default::default(),

View File

@ -261,7 +261,7 @@ impl TransformVisitor<'tcx> {
let self_place = Place::from(SELF_ARG); let self_place = Place::from(SELF_ARG);
let assign = Statement { let assign = Statement {
source_info: source_info(body), source_info: SourceInfo::outermost(body.span),
kind: StatementKind::Assign(box (temp, Rvalue::Discriminant(self_place))), kind: StatementKind::Assign(box (temp, Rvalue::Discriminant(self_place))),
}; };
(assign, temp) (assign, temp)
@ -395,7 +395,7 @@ fn replace_local<'tcx>(
body: &mut Body<'tcx>, body: &mut Body<'tcx>,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
) -> Local { ) -> Local {
let source_info = source_info(body); let source_info = SourceInfo::outermost(body.span);
let new_decl = LocalDecl { let new_decl = LocalDecl {
mutability: Mutability::Mut, mutability: Mutability::Mut,
ty, ty,
@ -784,7 +784,7 @@ fn insert_switch<'tcx>(
targets: cases.iter().map(|&(_, d)| d).chain(iter::once(default_block)).collect(), targets: cases.iter().map(|&(_, d)| d).chain(iter::once(default_block)).collect(),
}; };
let source_info = source_info(body); let source_info = SourceInfo::outermost(body.span);
body.basic_blocks_mut().raw.insert( body.basic_blocks_mut().raw.insert(
0, 0,
BasicBlockData { BasicBlockData {
@ -858,7 +858,7 @@ fn create_generator_drop_shim<'tcx>(
let mut body = body.clone(); let mut body = body.clone();
body.arg_count = 1; // make sure the resume argument is not included here body.arg_count = 1; // make sure the resume argument is not included here
let source_info = source_info(&body); let source_info = SourceInfo::outermost(body.span);
let mut cases = create_cases(&mut body, transform, Operation::Drop); let mut cases = create_cases(&mut body, transform, Operation::Drop);
@ -922,7 +922,7 @@ fn create_generator_drop_shim<'tcx>(
} }
fn insert_term_block<'tcx>(body: &mut Body<'tcx>, kind: TerminatorKind<'tcx>) -> BasicBlock { fn insert_term_block<'tcx>(body: &mut Body<'tcx>, kind: TerminatorKind<'tcx>) -> BasicBlock {
let source_info = source_info(body); let source_info = SourceInfo::outermost(body.span);
body.basic_blocks_mut().push(BasicBlockData { body.basic_blocks_mut().push(BasicBlockData {
statements: Vec::new(), statements: Vec::new(),
terminator: Some(Terminator { source_info, kind }), terminator: Some(Terminator { source_info, kind }),
@ -948,7 +948,7 @@ fn insert_panic_block<'tcx>(
cleanup: None, cleanup: None,
}; };
let source_info = source_info(body); let source_info = SourceInfo::outermost(body.span);
body.basic_blocks_mut().push(BasicBlockData { body.basic_blocks_mut().push(BasicBlockData {
statements: Vec::new(), statements: Vec::new(),
terminator: Some(Terminator { source_info, kind: term }), terminator: Some(Terminator { source_info, kind: term }),
@ -1025,7 +1025,7 @@ fn create_generator_resume_function<'tcx>(
// Poison the generator when it unwinds // Poison the generator when it unwinds
if can_unwind { if can_unwind {
let source_info = source_info(body); let source_info = SourceInfo::outermost(body.span);
let poison_block = body.basic_blocks_mut().push(BasicBlockData { let poison_block = body.basic_blocks_mut().push(BasicBlockData {
statements: vec![transform.set_discr(VariantIdx::new(POISONED), source_info)], statements: vec![transform.set_discr(VariantIdx::new(POISONED), source_info)],
terminator: Some(Terminator { source_info, kind: TerminatorKind::Resume }), terminator: Some(Terminator { source_info, kind: TerminatorKind::Resume }),
@ -1092,10 +1092,6 @@ fn create_generator_resume_function<'tcx>(
dump_mir(tcx, None, "generator_resume", &0, source, body, |_, _| Ok(())); dump_mir(tcx, None, "generator_resume", &0, source, body, |_, _| Ok(()));
} }
fn source_info(body: &Body<'_>) -> SourceInfo {
SourceInfo { span: body.span, scope: OUTERMOST_SOURCE_SCOPE }
}
fn insert_clean_drop(body: &mut Body<'_>) -> BasicBlock { fn insert_clean_drop(body: &mut Body<'_>) -> BasicBlock {
let return_block = insert_term_block(body, TerminatorKind::Return); let return_block = insert_term_block(body, TerminatorKind::Return);
@ -1104,7 +1100,7 @@ fn insert_clean_drop(body: &mut Body<'_>) -> BasicBlock {
target: return_block, target: return_block,
unwind: None, unwind: None,
}; };
let source_info = source_info(body); let source_info = SourceInfo::outermost(body.span);
// Create a block to destroy an unresumed generators. This can only destroy upvars. // Create a block to destroy an unresumed generators. This can only destroy upvars.
body.basic_blocks_mut().push(BasicBlockData { body.basic_blocks_mut().push(BasicBlockData {
@ -1135,7 +1131,7 @@ fn create_cases<'tcx>(
transform: &TransformVisitor<'tcx>, transform: &TransformVisitor<'tcx>,
operation: Operation, operation: Operation,
) -> Vec<(usize, BasicBlock)> { ) -> Vec<(usize, BasicBlock)> {
let source_info = source_info(body); let source_info = SourceInfo::outermost(body.span);
transform transform
.suspension_points .suspension_points
@ -1241,7 +1237,7 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
replace_local(resume_local, body.local_decls[resume_local].ty, body, tcx); replace_local(resume_local, body.local_decls[resume_local].ty, body, tcx);
// When first entering the generator, move the resume argument into its new local. // When first entering the generator, move the resume argument into its new local.
let source_info = source_info(body); let source_info = SourceInfo::outermost(body.span);
let stmts = &mut body.basic_blocks_mut()[BasicBlock::new(0)].statements; let stmts = &mut body.basic_blocks_mut()[BasicBlock::new(0)].statements;
stmts.insert( stmts.insert(
0, 0,

View File

@ -775,7 +775,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
self.promoted.basic_blocks_mut().push(BasicBlockData { self.promoted.basic_blocks_mut().push(BasicBlockData {
statements: vec![], statements: vec![],
terminator: Some(Terminator { terminator: Some(Terminator {
source_info: SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }, source_info: SourceInfo::outermost(span),
kind: TerminatorKind::Return, kind: TerminatorKind::Return,
}), }),
is_cleanup: false, is_cleanup: false,
@ -786,7 +786,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
let last = self.promoted.basic_blocks().last().unwrap(); let last = self.promoted.basic_blocks().last().unwrap();
let data = &mut self.promoted[last]; let data = &mut self.promoted[last];
data.statements.push(Statement { data.statements.push(Statement {
source_info: SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE }, source_info: SourceInfo::outermost(span),
kind: StatementKind::Assign(box (Place::from(dest), rvalue)), kind: StatementKind::Assign(box (Place::from(dest), rvalue)),
}); });
} }

View File

@ -50,7 +50,7 @@ impl<'tcx> MirPatch<'tcx> {
result.new_block(BasicBlockData { result.new_block(BasicBlockData {
statements: vec![], statements: vec![],
terminator: Some(Terminator { terminator: Some(Terminator {
source_info: SourceInfo { span: body.span, scope: OUTERMOST_SOURCE_SCOPE }, source_info: SourceInfo::outermost(body.span),
kind: TerminatorKind::Resume, kind: TerminatorKind::Resume,
}), }),
is_cleanup: true, is_cleanup: true,

View File

@ -804,10 +804,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
) -> BlockAnd<()> { ) -> BlockAnd<()> {
// Allocate locals for the function arguments // Allocate locals for the function arguments
for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() { for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
let source_info = SourceInfo { let source_info =
scope: OUTERMOST_SOURCE_SCOPE, SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span));
span: arg_opt.map_or(self.fn_span, |arg| arg.pat.span),
};
let arg_local = self.local_decls.push(LocalDecl { let arg_local = self.local_decls.push(LocalDecl {
mutability: Mutability::Mut, mutability: Mutability::Mut,
ty, ty,
@ -885,10 +883,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
self.var_debug_info.push(VarDebugInfo { self.var_debug_info.push(VarDebugInfo {
name, name,
source_info: SourceInfo { source_info: SourceInfo::outermost(tcx_hir.span(var_id)),
scope: OUTERMOST_SOURCE_SCOPE,
span: tcx_hir.span(var_id),
},
place: Place { place: Place {
local: closure_env_arg, local: closure_env_arg,
projection: tcx.intern_place_elems(&projs), projection: tcx.intern_place_elems(&projs),

View File

@ -989,7 +989,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let resumeblk = self.cfg.start_new_cleanup_block(); let resumeblk = self.cfg.start_new_cleanup_block();
self.cfg.terminate( self.cfg.terminate(
resumeblk, resumeblk,
SourceInfo { scope: OUTERMOST_SOURCE_SCOPE, span: self.fn_span }, SourceInfo::outermost(self.fn_span),
TerminatorKind::Resume, TerminatorKind::Resume,
); );
self.cached_resume_block = Some(resumeblk); self.cached_resume_block = Some(resumeblk);