Skip to content

Commit 0c5930e

Browse files
committed
mir: group span + visibility scope under a new SourceInfo type.
1 parent 719a591 commit 0c5930e

28 files changed

+255
-272
lines changed

src/librustc/mir/repr.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
100100
}
101101
}
102102

103+
/// Grouped information about the source code origin of a MIR entity.
104+
/// Intended to be inspected by diagnostics and debuginfo.
105+
/// Most passes can work with it as a whole, within a single function.
106+
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
107+
pub struct SourceInfo {
108+
/// Source span for the AST pertaining to this MIR entity.
109+
pub span: Span,
110+
111+
/// The lexical visibility scope, i.e. which bindings can be seen.
112+
pub scope: VisibilityScope
113+
}
114+
103115
///////////////////////////////////////////////////////////////////////////
104116
// Mutability and borrow kinds
105117

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

175-
/// scope in which variable was declared
176-
pub scope: VisibilityScope,
177-
178-
/// span where variable was declared
179-
pub span: Span,
187+
/// source information (span, scope, etc.) for the declaration
188+
pub source_info: SourceInfo,
180189
}
181190

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

276285
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
277286
pub struct Terminator<'tcx> {
278-
pub span: Span,
279-
pub scope: VisibilityScope,
287+
pub source_info: SourceInfo,
280288
pub kind: TerminatorKind<'tcx>
281289
}
282290

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

588596
#[derive(Clone, RustcEncodable, RustcDecodable)]
589597
pub struct Statement<'tcx> {
590-
pub span: Span,
591-
pub scope: VisibilityScope,
598+
pub source_info: SourceInfo,
592599
pub kind: StatementKind<'tcx>,
593600
}
594601

src/librustc/mir/visit.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ macro_rules! make_mir_visitor {
186186
self.super_span(span);
187187
}
188188

189+
fn visit_source_info(&mut self,
190+
source_info: & $($mutability)* SourceInfo) {
191+
self.super_source_info(source_info);
192+
}
193+
189194
fn visit_fn_output(&mut self,
190195
fn_output: & $($mutability)* FnOutput<'tcx>) {
191196
self.super_fn_output(fn_output);
@@ -319,13 +324,11 @@ macro_rules! make_mir_visitor {
319324
block: BasicBlock,
320325
statement: & $($mutability)* Statement<'tcx>) {
321326
let Statement {
322-
ref $($mutability)* span,
323-
ref $($mutability)* scope,
327+
ref $($mutability)* source_info,
324328
ref $($mutability)* kind,
325329
} = *statement;
326330

327-
self.visit_span(span);
328-
self.visit_visibility_scope(scope);
331+
self.visit_source_info(source_info);
329332
match *kind {
330333
StatementKind::Assign(ref $($mutability)* lvalue,
331334
ref $($mutability)* rvalue) => {
@@ -346,13 +349,11 @@ macro_rules! make_mir_visitor {
346349
block: BasicBlock,
347350
terminator: &$($mutability)* Terminator<'tcx>) {
348351
let Terminator {
349-
ref $($mutability)* span,
350-
ref $($mutability)* scope,
352+
ref $($mutability)* source_info,
351353
ref $($mutability)* kind,
352354
} = *terminator;
353355

354-
self.visit_span(span);
355-
self.visit_visibility_scope(scope);
356+
self.visit_source_info(source_info);
356357
self.visit_terminator_kind(block, kind);
357358
}
358359

@@ -622,13 +623,11 @@ macro_rules! make_mir_visitor {
622623
mutability: _,
623624
name: _,
624625
ref $($mutability)* ty,
625-
ref $($mutability)* scope,
626-
ref $($mutability)* span,
626+
ref $($mutability)* source_info,
627627
} = *var_decl;
628628

629629
self.visit_ty(ty);
630-
self.visit_visibility_scope(scope);
631-
self.visit_span(span);
630+
self.visit_source_info(source_info);
632631
}
633632

634633
fn super_temp_decl(&mut self,
@@ -707,6 +706,16 @@ macro_rules! make_mir_visitor {
707706
fn super_span(&mut self, _span: & $($mutability)* Span) {
708707
}
709708

709+
fn super_source_info(&mut self, source_info: & $($mutability)* SourceInfo) {
710+
let SourceInfo {
711+
ref $($mutability)* span,
712+
ref $($mutability)* scope,
713+
} = *source_info;
714+
715+
self.visit_span(span);
716+
self.visit_visibility_scope(scope);
717+
}
718+
710719
fn super_fn_output(&mut self, fn_output: & $($mutability)* FnOutput<'tcx>) {
711720
match *fn_output {
712721
FnOutput::FnConverging(ref $($mutability)* ty) => {

src/librustc_borrowck/borrowck/mir/dataflow/sanity_check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn each_block<'a, 'tcx, O>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
151151
fn is_rustc_peek<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
152152
terminator: &'a Option<repr::Terminator<'tcx>>)
153153
-> Option<(&'a [repr::Operand<'tcx>], Span)> {
154-
if let Some(repr::Terminator { ref kind, span, .. }) = *terminator {
154+
if let Some(repr::Terminator { ref kind, source_info, .. }) = *terminator {
155155
if let repr::TerminatorKind::Call { func: ref oper, ref args, .. } = *kind
156156
{
157157
if let repr::Operand::Constant(ref func) = *oper
@@ -161,7 +161,7 @@ fn is_rustc_peek<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
161161
let name = tcx.item_name(def_id);
162162
if abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic {
163163
if name.as_str() == "rustc_peek" {
164-
return Some((args, span));
164+
return Some((args, source_info.span));
165165
}
166166
}
167167
}

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ struct ElaborateDropsCtxt<'a, 'tcx: 'a> {
124124

125125
#[derive(Copy, Clone, Debug)]
126126
struct DropCtxt<'a, 'tcx: 'a> {
127-
span: Span,
128-
scope: ScopeId,
127+
source_info: SourceInfo,
129128
is_cleanup: bool,
130129

131130
init_data: &'a InitializationData,
@@ -273,8 +272,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
273272
let init_data = self.initialization_data_at(loc);
274273
let path = self.move_data().rev_lookup.find(location);
275274
self.elaborate_drop(&DropCtxt {
276-
span: terminator.span,
277-
scope: terminator.scope,
275+
source_info: terminator.source_info,
278276
is_cleanup: data.is_cleanup,
279277
init_data: &init_data,
280278
lvalue: location,
@@ -329,8 +327,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
329327

330328
let assign = Statement {
331329
kind: StatementKind::Assign(location.clone(), Rvalue::Use(value.clone())),
332-
span: terminator.span,
333-
scope: terminator.scope
330+
source_info: terminator.source_info
334331
};
335332

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

369366
self.elaborate_drop(&DropCtxt {
370-
span: terminator.span,
371-
scope: terminator.scope,
367+
source_info: terminator.source_info,
372368
is_cleanup: data.is_cleanup,
373369
init_data: &init_data,
374370
lvalue: location,
@@ -513,8 +509,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
513509
debug!("drop_ladder: for std field {} ({:?})", i, lv);
514510

515511
self.elaborated_drop_block(&DropCtxt {
516-
span: c.span,
517-
scope: c.scope,
512+
source_info: c.source_info,
518513
is_cleanup: is_cleanup,
519514
init_data: c.init_data,
520515
lvalue: lv,
@@ -527,8 +522,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
527522
debug!("drop_ladder: for rest field {} ({:?})", i, lv);
528523

529524
let blk = self.complete_drop(&DropCtxt {
530-
span: c.span,
531-
scope: c.scope,
525+
source_info: c.source_info,
532526
is_cleanup: is_cleanup,
533527
init_data: c.init_data,
534528
lvalue: lv,
@@ -785,7 +779,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
785779
self.patch.new_block(BasicBlockData {
786780
statements: vec![],
787781
terminator: Some(Terminator {
788-
scope: c.scope, span: c.span, kind: k
782+
source_info: c.source_info, kind: k
789783
}),
790784
is_cleanup: is_cleanup
791785
})
@@ -858,11 +852,10 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
858852
let mut statements = vec![];
859853
if let Some(&flag) = self.drop_flags.get(&c.path) {
860854
statements.push(Statement {
861-
span: c.span,
862-
scope: c.scope,
855+
source_info: c.source_info,
863856
kind: StatementKind::Assign(
864857
Lvalue::Temp(flag),
865-
self.constant_bool(c.span, false)
858+
self.constant_bool(c.source_info.span, false)
866859
)
867860
});
868861
}
@@ -880,9 +873,9 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
880873
self.patch.new_block(BasicBlockData {
881874
statements: statements,
882875
terminator: Some(Terminator {
883-
scope: c.scope, span: c.span, kind: TerminatorKind::Call {
876+
source_info: c.source_info, kind: TerminatorKind::Call {
884877
func: Operand::Constant(Constant {
885-
span: c.span,
878+
span: c.source_info.span,
886879
ty: fty,
887880
literal: Literal::Item {
888881
def_id: free_func,
@@ -910,7 +903,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
910903
ty::TyStruct(def, _) | ty::TyEnum(def, _) => {
911904
if def.has_dtor() {
912905
self.tcx.sess.span_warn(
913-
c.span,
906+
c.source_info.span,
914907
&format!("dataflow bug??? moving out of type with dtor {:?}",
915908
c));
916909
true
@@ -932,15 +925,15 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
932925

933926
fn set_drop_flag(&mut self, loc: Location, path: MovePathIndex, val: DropFlagState) {
934927
if let Some(&flag) = self.drop_flags.get(&path) {
935-
let span = self.patch.context_for_location(self.mir, loc).0;
928+
let span = self.patch.source_info_for_location(self.mir, loc).span;
936929
let val = self.constant_bool(span, val.value());
937930
self.patch.add_assign(loc, Lvalue::Temp(flag), val);
938931
}
939932
}
940933

941934
fn drop_flags_on_init(&mut self) {
942935
let loc = Location { block: START_BLOCK, index: 0 };
943-
let span = self.patch.context_for_location(self.mir, loc).0;
936+
let span = self.patch.source_info_for_location(self.mir, loc).span;
944937
let false_ = self.constant_bool(span, false);
945938
for flag in self.drop_flags.values() {
946939
self.patch.add_assign(loc, Lvalue::Temp(*flag), false_.clone());

src/librustc_borrowck/borrowck/mir/patch.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use super::gather_moves::Location;
1212
use rustc::ty::Ty;
1313
use rustc::mir::repr::*;
14-
use syntax::codemap::Span;
1514

1615
use std::iter;
1716
use std::u32;
@@ -62,8 +61,10 @@ impl<'tcx> MirPatch<'tcx> {
6261
result.new_block(BasicBlockData {
6362
statements: vec![],
6463
terminator: Some(Terminator {
65-
span: mir.span,
66-
scope: ScopeId::new(0),
64+
source_info: SourceInfo {
65+
span: mir.span,
66+
scope: ARGUMENT_VISIBILITY_SCOPE
67+
},
6768
kind: TerminatorKind::Resume
6869
}),
6970
is_cleanup: true
@@ -154,31 +155,30 @@ impl<'tcx> MirPatch<'tcx> {
154155
debug!("MirPatch: adding statement {:?} at loc {:?}+{}",
155156
stmt, loc, delta);
156157
loc.index += delta;
157-
let (span, scope) = Self::context_for_index(
158+
let source_info = Self::source_info_for_index(
158159
mir.basic_block_data(loc.block), loc
159160
);
160161
mir.basic_block_data_mut(loc.block).statements.insert(
161162
loc.index, Statement {
162-
span: span,
163-
scope: scope,
163+
source_info: source_info,
164164
kind: stmt
165165
});
166166
delta += 1;
167167
}
168168
}
169169

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

177-
pub fn context_for_location(&self, mir: &Mir, loc: Location) -> (Span, ScopeId) {
177+
pub fn source_info_for_location(&self, mir: &Mir, loc: Location) -> SourceInfo {
178178
let data = match loc.block.index().checked_sub(mir.basic_blocks.len()) {
179179
Some(new) => &self.new_blocks[new],
180180
None => mir.basic_block_data(loc.block)
181181
};
182-
Self::context_for_index(data, loc)
182+
Self::source_info_for_index(data, loc)
183183
}
184184
}

src/librustc_mir/build/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
8383
unpack!(block = this.into(destination, block, expr));
8484
} else if dest_is_unit {
8585
// FIXME(#31472)
86-
let scope_id = this.innermost_scope_id();
87-
this.cfg.push_assign_unit(block, scope_id, span, destination);
86+
let source_info = this.source_info(span);
87+
this.cfg.push_assign_unit(block, source_info, destination);
8888
}
8989
// Finally, we pop all the let scopes before exiting out from the scope of block
9090
// itself.

0 commit comments

Comments
 (0)