Skip to content

Commit 30cae58

Browse files
committed
Auto merge of #51182 - eddyb:not-just-visibility, r=nikomatsakis
[MIR] Change "scopes" from "visibility scopes" to "source scopes". These scopes are already used for source-oriented diagnostics, lint levels and unsafety checks. This PR generalizes the naming around scopes, making the type `SourceScope`, and flips (across several commits) the relationship/priority between `LocalDecl`'s "visibility" and "syntactic" scopes. r? @nikomatsakis
2 parents a47b2d0 + 06d88cd commit 30cae58

34 files changed

+275
-261
lines changed

src/librustc/ich/impls_mir.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ impl_stable_hash_for!(struct mir::LocalDecl<'tcx> {
2626
ty,
2727
name,
2828
source_info,
29+
visibility_scope,
2930
internal,
30-
syntactic_scope,
3131
is_user_variable
3232
});
3333
impl_stable_hash_for!(struct mir::UpvarDecl { debug_name, by_ref, mutability });
@@ -127,7 +127,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Field {
127127
}
128128

129129
impl<'a> HashStable<StableHashingContext<'a>>
130-
for mir::VisibilityScope {
130+
for mir::SourceScope {
131131
#[inline]
132132
fn hash_stable<W: StableHasherResult>(&self,
133133
hcx: &mut StableHashingContext<'a>,
@@ -363,8 +363,8 @@ for mir::ProjectionElem<'gcx, V, T>
363363
}
364364
}
365365

366-
impl_stable_hash_for!(struct mir::VisibilityScopeData { span, parent_scope });
367-
impl_stable_hash_for!(struct mir::VisibilityScopeInfo {
366+
impl_stable_hash_for!(struct mir::SourceScopeData { span, parent_scope });
367+
impl_stable_hash_for!(struct mir::SourceScopeLocalData {
368368
lint_root, safety
369369
});
370370

src/librustc/mir/mod.rs

+54-52
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ pub struct Mir<'tcx> {
7878
/// that indexes into this vector.
7979
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
8080

81-
/// List of visibility (lexical) scopes; these are referenced by statements
82-
/// and used (eventually) for debuginfo. Indexed by a `VisibilityScope`.
83-
pub visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
81+
/// List of source scopes; these are referenced by statements
82+
/// and used for debuginfo. Indexed by a `SourceScope`.
83+
pub source_scopes: IndexVec<SourceScope, SourceScopeData>,
8484

85-
/// Crate-local information for each visibility scope, that can't (and
85+
/// Crate-local information for each source scope, that can't (and
8686
/// needn't) be tracked across crates.
87-
pub visibility_scope_info: ClearCrossCrate<IndexVec<VisibilityScope, VisibilityScopeInfo>>,
87+
pub source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
8888

8989
/// Rvalues promoted from this function, such as borrows of constants.
9090
/// Each of them is the Mir of a constant with the fn's type parameters
@@ -137,9 +137,9 @@ pub const START_BLOCK: BasicBlock = BasicBlock(0);
137137

138138
impl<'tcx> Mir<'tcx> {
139139
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
140-
visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
141-
visibility_scope_info: ClearCrossCrate<IndexVec<VisibilityScope,
142-
VisibilityScopeInfo>>,
140+
source_scopes: IndexVec<SourceScope, SourceScopeData>,
141+
source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope,
142+
SourceScopeLocalData>>,
143143
promoted: IndexVec<Promoted, Mir<'tcx>>,
144144
yield_ty: Option<Ty<'tcx>>,
145145
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
@@ -153,8 +153,8 @@ impl<'tcx> Mir<'tcx> {
153153

154154
Mir {
155155
basic_blocks,
156-
visibility_scopes,
157-
visibility_scope_info,
156+
source_scopes,
157+
source_scope_local_data,
158158
promoted,
159159
yield_ty,
160160
generator_drop: None,
@@ -308,14 +308,6 @@ impl<'tcx> Mir<'tcx> {
308308
}
309309
}
310310

311-
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
312-
pub struct VisibilityScopeInfo {
313-
/// A NodeId with lint levels equivalent to this scope's lint levels.
314-
pub lint_root: ast::NodeId,
315-
/// The unsafe block that contains this node.
316-
pub safety: Safety,
317-
}
318-
319311
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
320312
pub enum Safety {
321313
Safe,
@@ -329,8 +321,8 @@ pub enum Safety {
329321

330322
impl_stable_hash_for!(struct Mir<'tcx> {
331323
basic_blocks,
332-
visibility_scopes,
333-
visibility_scope_info,
324+
source_scopes,
325+
source_scope_local_data,
334326
promoted,
335327
yield_ty,
336328
generator_drop,
@@ -376,8 +368,9 @@ pub struct SourceInfo {
376368
/// Source span for the AST pertaining to this MIR entity.
377369
pub span: Span,
378370

379-
/// The lexical visibility scope, i.e. which bindings can be seen.
380-
pub scope: VisibilityScope
371+
/// The source scope, keeping track of which bindings can be
372+
/// seen by debuginfo, active lint levels, `unsafe {...}`, etc.
373+
pub scope: SourceScope
381374
}
382375

383376
///////////////////////////////////////////////////////////////////////////
@@ -512,16 +505,13 @@ pub struct LocalDecl<'tcx> {
512505
/// to generate better debuginfo.
513506
pub name: Option<Name>,
514507

515-
/// Source info of the local.
516-
pub source_info: SourceInfo,
517-
518-
/// The *syntactic* visibility scope the local is defined
508+
/// The *syntactic* (i.e. not visibility) source scope the local is defined
519509
/// in. If the local was defined in a let-statement, this
520510
/// is *within* the let-statement, rather than outside
521511
/// of it.
522512
///
523-
/// This is needed because visibility scope of locals within a let-statement
524-
/// is weird.
513+
/// This is needed because the visibility source scope of locals within
514+
/// a let-statement is weird.
525515
///
526516
/// The reason is that we want the local to be *within* the let-statement
527517
/// for lint purposes, but we want the local to be *after* the let-statement
@@ -566,9 +556,9 @@ pub struct LocalDecl<'tcx> {
566556
/// `drop(x)`, we want it to refer to `x: u32`.
567557
///
568558
/// To allow both uses to work, we need to have more than a single scope
569-
/// for a local. We have the `syntactic_scope` represent the
559+
/// for a local. We have the `source_info.scope` represent the
570560
/// "syntactic" lint scope (with a variable being under its let
571-
/// block) while the source-info scope represents the "local variable"
561+
/// block) while the `visibility_scope` represents the "local variable"
572562
/// scope (where the "rest" of a block is under all prior let-statements).
573563
///
574564
/// The end result looks like this:
@@ -580,21 +570,25 @@ pub struct LocalDecl<'tcx> {
580570
/// │ │{ #[allow(unused_mut] } // this is actually split into 2 scopes
581571
/// │ │ // in practice because I'm lazy.
582572
/// │ │
583-
/// │ │← x.syntactic_scope
573+
/// │ │← x.source_info.scope
584574
/// │ │← `x.parse().unwrap()`
585575
/// │ │
586-
/// │ │ │← y.syntactic_scope
576+
/// │ │ │← y.source_info.scope
587577
/// │ │
588578
/// │ │ │{ let y: u32 }
589579
/// │ │ │
590-
/// │ │ │← y.source_info.scope
580+
/// │ │ │← y.visibility_scope
591581
/// │ │ │← `y + 2`
592582
/// │
593583
/// │ │{ let x: u32 }
594-
/// │ │← x.source_info.scope
584+
/// │ │← x.visibility_scope
595585
/// │ │← `drop(x)` // this accesses `x: u32`
596586
/// ```
597-
pub syntactic_scope: VisibilityScope,
587+
pub source_info: SourceInfo,
588+
589+
/// Source scope within which the local is visible (for debuginfo)
590+
/// (see `source_info` for more details).
591+
pub visibility_scope: SourceScope,
598592
}
599593

600594
impl<'tcx> LocalDecl<'tcx> {
@@ -607,9 +601,9 @@ impl<'tcx> LocalDecl<'tcx> {
607601
name: None,
608602
source_info: SourceInfo {
609603
span,
610-
scope: ARGUMENT_VISIBILITY_SCOPE
604+
scope: OUTERMOST_SOURCE_SCOPE
611605
},
612-
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
606+
visibility_scope: OUTERMOST_SOURCE_SCOPE,
613607
internal: false,
614608
is_user_variable: false
615609
}
@@ -624,9 +618,9 @@ impl<'tcx> LocalDecl<'tcx> {
624618
name: None,
625619
source_info: SourceInfo {
626620
span,
627-
scope: ARGUMENT_VISIBILITY_SCOPE
621+
scope: OUTERMOST_SOURCE_SCOPE
628622
},
629-
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
623+
visibility_scope: OUTERMOST_SOURCE_SCOPE,
630624
internal: true,
631625
is_user_variable: false
632626
}
@@ -642,9 +636,9 @@ impl<'tcx> LocalDecl<'tcx> {
642636
ty: return_ty,
643637
source_info: SourceInfo {
644638
span,
645-
scope: ARGUMENT_VISIBILITY_SCOPE
639+
scope: OUTERMOST_SOURCE_SCOPE
646640
},
647-
syntactic_scope: ARGUMENT_VISIBILITY_SCOPE,
641+
visibility_scope: OUTERMOST_SOURCE_SCOPE,
648642
internal: false,
649643
name: None, // FIXME maybe we do want some name here?
650644
is_user_variable: false
@@ -1047,7 +1041,7 @@ impl<'tcx> BasicBlockData<'tcx> {
10471041
self.statements.resize(gap.end, Statement {
10481042
source_info: SourceInfo {
10491043
span: DUMMY_SP,
1050-
scope: ARGUMENT_VISIBILITY_SCOPE
1044+
scope: OUTERMOST_SOURCE_SCOPE
10511045
},
10521046
kind: StatementKind::Nop
10531047
});
@@ -1501,16 +1495,24 @@ impl<'tcx> Debug for Place<'tcx> {
15011495
///////////////////////////////////////////////////////////////////////////
15021496
// Scopes
15031497

1504-
newtype_index!(VisibilityScope
1498+
newtype_index!(SourceScope
15051499
{
15061500
DEBUG_FORMAT = "scope[{}]",
1507-
const ARGUMENT_VISIBILITY_SCOPE = 0,
1501+
const OUTERMOST_SOURCE_SCOPE = 0,
15081502
});
15091503

15101504
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
1511-
pub struct VisibilityScopeData {
1505+
pub struct SourceScopeData {
15121506
pub span: Span,
1513-
pub parent_scope: Option<VisibilityScope>,
1507+
pub parent_scope: Option<SourceScope>,
1508+
}
1509+
1510+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
1511+
pub struct SourceScopeLocalData {
1512+
/// A NodeId with lint levels equivalent to this scope's lint levels.
1513+
pub lint_root: ast::NodeId,
1514+
/// The unsafe block that contains this node.
1515+
pub safety: Safety,
15141516
}
15151517

15161518
///////////////////////////////////////////////////////////////////////////
@@ -2153,16 +2155,16 @@ CloneTypeFoldableAndLiftImpls! {
21532155
SourceInfo,
21542156
UpvarDecl,
21552157
ValidationOp,
2156-
VisibilityScopeData,
2157-
VisibilityScope,
2158-
VisibilityScopeInfo,
2158+
SourceScope,
2159+
SourceScopeData,
2160+
SourceScopeLocalData,
21592161
}
21602162

21612163
BraceStructTypeFoldableImpl! {
21622164
impl<'tcx> TypeFoldable<'tcx> for Mir<'tcx> {
21632165
basic_blocks,
2164-
visibility_scopes,
2165-
visibility_scope_info,
2166+
source_scopes,
2167+
source_scope_local_data,
21662168
promoted,
21672169
yield_ty,
21682170
generator_drop,
@@ -2190,7 +2192,7 @@ BraceStructTypeFoldableImpl! {
21902192
ty,
21912193
name,
21922194
source_info,
2193-
syntactic_scope,
2195+
visibility_scope,
21942196
}
21952197
}
21962198

src/librustc/mir/visit.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ macro_rules! make_mir_visitor {
9292
self.super_basic_block_data(block, data);
9393
}
9494

95-
fn visit_visibility_scope_data(&mut self,
96-
scope_data: & $($mutability)* VisibilityScopeData) {
97-
self.super_visibility_scope_data(scope_data);
95+
fn visit_source_scope_data(&mut self,
96+
scope_data: & $($mutability)* SourceScopeData) {
97+
self.super_source_scope_data(scope_data);
9898
}
9999

100100
fn visit_statement(&mut self,
@@ -261,9 +261,9 @@ macro_rules! make_mir_visitor {
261261
_location: Location) {
262262
}
263263

264-
fn visit_visibility_scope(&mut self,
265-
scope: & $($mutability)* VisibilityScope) {
266-
self.super_visibility_scope(scope);
264+
fn visit_source_scope(&mut self,
265+
scope: & $($mutability)* SourceScope) {
266+
self.super_source_scope(scope);
267267
}
268268

269269
// The `super_xxx` methods comprise the default behavior and are
@@ -274,7 +274,7 @@ macro_rules! make_mir_visitor {
274274
if let Some(yield_ty) = &$($mutability)* mir.yield_ty {
275275
self.visit_ty(yield_ty, TyContext::YieldTy(SourceInfo {
276276
span: mir.span,
277-
scope: ARGUMENT_VISIBILITY_SCOPE,
277+
scope: OUTERMOST_SOURCE_SCOPE,
278278
}));
279279
}
280280

@@ -289,13 +289,13 @@ macro_rules! make_mir_visitor {
289289
self.visit_basic_block_data(bb, data);
290290
}
291291

292-
for scope in &$($mutability)* mir.visibility_scopes {
293-
self.visit_visibility_scope_data(scope);
292+
for scope in &$($mutability)* mir.source_scopes {
293+
self.visit_source_scope_data(scope);
294294
}
295295

296296
self.visit_ty(&$($mutability)* mir.return_ty(), TyContext::ReturnTy(SourceInfo {
297297
span: mir.span,
298-
scope: ARGUMENT_VISIBILITY_SCOPE,
298+
scope: OUTERMOST_SOURCE_SCOPE,
299299
}));
300300

301301
for local in mir.local_decls.indices() {
@@ -327,16 +327,16 @@ macro_rules! make_mir_visitor {
327327
}
328328
}
329329

330-
fn super_visibility_scope_data(&mut self,
331-
scope_data: & $($mutability)* VisibilityScopeData) {
332-
let VisibilityScopeData {
330+
fn super_source_scope_data(&mut self,
331+
scope_data: & $($mutability)* SourceScopeData) {
332+
let SourceScopeData {
333333
ref $($mutability)* span,
334334
ref $($mutability)* parent_scope,
335335
} = *scope_data;
336336

337337
self.visit_span(span);
338338
if let Some(ref $($mutability)* parent_scope) = *parent_scope {
339-
self.visit_visibility_scope(parent_scope);
339+
self.visit_source_scope(parent_scope);
340340
}
341341
}
342342

@@ -715,8 +715,8 @@ macro_rules! make_mir_visitor {
715715
ref $($mutability)* ty,
716716
name: _,
717717
ref $($mutability)* source_info,
718+
ref $($mutability)* visibility_scope,
718719
internal: _,
719-
ref $($mutability)* syntactic_scope,
720720
is_user_variable: _,
721721
} = *local_decl;
722722

@@ -725,11 +725,11 @@ macro_rules! make_mir_visitor {
725725
source_info: *source_info,
726726
});
727727
self.visit_source_info(source_info);
728-
self.visit_visibility_scope(syntactic_scope);
728+
self.visit_source_scope(visibility_scope);
729729
}
730730

731-
fn super_visibility_scope(&mut self,
732-
_scope: & $($mutability)* VisibilityScope) {
731+
fn super_source_scope(&mut self,
732+
_scope: & $($mutability)* SourceScope) {
733733
}
734734

735735
fn super_branch(&mut self,
@@ -775,7 +775,7 @@ macro_rules! make_mir_visitor {
775775
} = *source_info;
776776

777777
self.visit_span(span);
778-
self.visit_visibility_scope(scope);
778+
self.visit_source_scope(scope);
779779
}
780780

781781
fn super_ty(&mut self, _ty: & $($mutability)* Ty<'tcx>) {

0 commit comments

Comments
 (0)