Skip to content

Commit 0d531bf

Browse files
committed
Auto merge of #33989 - eddyb:mir-viz, r=nikomatsakis
[MIR] Make scopes debuginfo-specific (visibility scopes). Fixes #32949 by having MIR (visibility) scopes mimic the lexical structure. Unlike #33235, this PR also removes all scopes without variable bindings. Printing of scopes also changed, e.g. for: ```rust fn foo(x: i32, y: i32) { let a = 0; let b = 0; let c = 0; } ``` Before my changes: ```rust fn foo(arg0: i32, arg1: i32) -> () { let var0: i32; // "x" in scope 1 at <anon>:1:8: 1:9 let var1: i32; // "y" in scope 1 at <anon>:1:16: 1:17 let var2: i32; // "a" in scope 3 at <anon>:1:30: 1:31 let var3: i32; // "b" in scope 6 at <anon>:1:41: 1:42 let var4: i32; // "c" in scope 9 at <anon>:1:52: 1:53 ... scope tree: 0 1 2 3 { 4 5 6 { 7 8 9 10 11 } } } ``` After my changes: ```rust fn foo(arg0: i32, arg1: i32) -> () { scope 1 { let var0: i32; // "x" in scope 1 at <anon>:1:8: 1:9 let var1: i32; // "y" in scope 1 at <anon>:1:16: 1:17 scope 2 { let var2: i32; // "a" in scope 2 at <anon>:1:30: 1:31 scope 3 { let var3: i32; // "b" in scope 3 at <anon>:1:41: 1:42 scope 4 { let var4: i32; // "c" in scope 4 at <anon>:1:52: 1:53 } } } } ... }
2 parents 4b240fe + 0c5930e commit 0d531bf

30 files changed

+478
-493
lines changed

src/librustc/mir/repr.rs

+33-23
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ pub struct Mir<'tcx> {
3232
/// that indexes into this vector.
3333
pub basic_blocks: Vec<BasicBlockData<'tcx>>,
3434

35-
/// List of lexical scopes; these are referenced by statements and
36-
/// used (eventually) for debuginfo. Indexed by a `ScopeId`.
37-
pub scopes: Vec<ScopeData>,
35+
/// List of visibility (lexical) scopes; these are referenced by statements
36+
/// and used (eventually) for debuginfo. Indexed by a `VisibilityScope`.
37+
pub visibility_scopes: Vec<VisibilityScopeData>,
3838

3939
/// Rvalues promoted from this function, such as borrows of constants.
4040
/// Each of them is the Mir of a constant with the fn's type parameters
@@ -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: ScopeId,
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: ScopeId,
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: ScopeId,
598+
pub source_info: SourceInfo,
592599
pub kind: StatementKind<'tcx>,
593600
}
594601

@@ -754,29 +761,32 @@ impl<'tcx> Debug for Lvalue<'tcx> {
754761
///////////////////////////////////////////////////////////////////////////
755762
// Scopes
756763

757-
impl Index<ScopeId> for Vec<ScopeData> {
758-
type Output = ScopeData;
764+
impl Index<VisibilityScope> for Vec<VisibilityScopeData> {
765+
type Output = VisibilityScopeData;
759766

760767
#[inline]
761-
fn index(&self, index: ScopeId) -> &ScopeData {
768+
fn index(&self, index: VisibilityScope) -> &VisibilityScopeData {
762769
&self[index.index()]
763770
}
764771
}
765772

766-
impl IndexMut<ScopeId> for Vec<ScopeData> {
773+
impl IndexMut<VisibilityScope> for Vec<VisibilityScopeData> {
767774
#[inline]
768-
fn index_mut(&mut self, index: ScopeId) -> &mut ScopeData {
775+
fn index_mut(&mut self, index: VisibilityScope) -> &mut VisibilityScopeData {
769776
&mut self[index.index()]
770777
}
771778
}
772779

773780
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
774-
pub struct ScopeId(u32);
781+
pub struct VisibilityScope(u32);
782+
783+
/// The visibility scope all arguments go into.
784+
pub const ARGUMENT_VISIBILITY_SCOPE: VisibilityScope = VisibilityScope(0);
775785

776-
impl ScopeId {
777-
pub fn new(index: usize) -> ScopeId {
786+
impl VisibilityScope {
787+
pub fn new(index: usize) -> VisibilityScope {
778788
assert!(index < (u32::MAX as usize));
779-
ScopeId(index as u32)
789+
VisibilityScope(index as u32)
780790
}
781791

782792
pub fn index(self) -> usize {
@@ -785,9 +795,9 @@ impl ScopeId {
785795
}
786796

787797
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
788-
pub struct ScopeData {
798+
pub struct VisibilityScopeData {
789799
pub span: Span,
790-
pub parent_scope: Option<ScopeId>,
800+
pub parent_scope: Option<VisibilityScope>,
791801
}
792802

793803
///////////////////////////////////////////////////////////////////////////

src/librustc/mir/visit.rs

+36-27
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ macro_rules! make_mir_visitor {
9797
self.super_basic_block_data(block, data);
9898
}
9999

100-
fn visit_scope_data(&mut self,
101-
scope_data: & $($mutability)* ScopeData) {
102-
self.super_scope_data(scope_data);
100+
fn visit_visibility_scope_data(&mut self,
101+
scope_data: & $($mutability)* VisibilityScopeData) {
102+
self.super_visibility_scope_data(scope_data);
103103
}
104104

105105
fn visit_statement(&mut self,
@@ -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);
@@ -236,9 +241,9 @@ macro_rules! make_mir_visitor {
236241
self.super_arg_decl(arg_decl);
237242
}
238243

239-
fn visit_scope_id(&mut self,
240-
scope_id: & $($mutability)* ScopeId) {
241-
self.super_scope_id(scope_id);
244+
fn visit_visibility_scope(&mut self,
245+
scope: & $($mutability)* VisibilityScope) {
246+
self.super_visibility_scope(scope);
242247
}
243248

244249
// The `super_xxx` methods comprise the default behavior and are
@@ -248,7 +253,7 @@ macro_rules! make_mir_visitor {
248253
mir: & $($mutability)* Mir<'tcx>) {
249254
let Mir {
250255
ref $($mutability)* basic_blocks,
251-
ref $($mutability)* scopes,
256+
ref $($mutability)* visibility_scopes,
252257
promoted: _, // Visited by passes separately.
253258
ref $($mutability)* return_ty,
254259
ref $($mutability)* var_decls,
@@ -263,8 +268,8 @@ macro_rules! make_mir_visitor {
263268
self.visit_basic_block_data(block, data);
264269
}
265270

266-
for scope in scopes {
267-
self.visit_scope_data(scope);
271+
for scope in visibility_scopes {
272+
self.visit_visibility_scope_data(scope);
268273
}
269274

270275
self.visit_fn_output(return_ty);
@@ -302,30 +307,28 @@ macro_rules! make_mir_visitor {
302307
}
303308
}
304309

305-
fn super_scope_data(&mut self,
306-
scope_data: & $($mutability)* ScopeData) {
307-
let ScopeData {
310+
fn super_visibility_scope_data(&mut self,
311+
scope_data: & $($mutability)* VisibilityScopeData) {
312+
let VisibilityScopeData {
308313
ref $($mutability)* span,
309314
ref $($mutability)* parent_scope,
310315
} = *scope_data;
311316

312317
self.visit_span(span);
313318
if let Some(ref $($mutability)* parent_scope) = *parent_scope {
314-
self.visit_scope_id(parent_scope);
319+
self.visit_visibility_scope(parent_scope);
315320
}
316321
}
317322

318323
fn super_statement(&mut self,
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_scope_id(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_scope_id(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_scope_id(scope);
631-
self.visit_span(span);
630+
self.visit_source_info(source_info);
632631
}
633632

634633
fn super_temp_decl(&mut self,
@@ -651,8 +650,8 @@ macro_rules! make_mir_visitor {
651650
self.visit_ty(ty);
652651
}
653652

654-
fn super_scope_id(&mut self,
655-
_scope_id: & $($mutability)* ScopeId) {
653+
fn super_visibility_scope(&mut self,
654+
_scope: & $($mutability)* VisibilityScope) {
656655
}
657656

658657
fn super_branch(&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
}

0 commit comments

Comments
 (0)