Skip to content

Commit 719a591

Browse files
committed
mir: distinguish between variable visibility scopes and SEME scopes.
1 parent f352550 commit 719a591

File tree

19 files changed

+259
-257
lines changed

19 files changed

+259
-257
lines changed

src/librustc/mir/repr.rs

+20-17
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
@@ -173,7 +173,7 @@ pub struct VarDecl<'tcx> {
173173
pub ty: Ty<'tcx>,
174174

175175
/// scope in which variable was declared
176-
pub scope: ScopeId,
176+
pub scope: VisibilityScope,
177177

178178
/// span where variable was declared
179179
pub span: Span,
@@ -276,7 +276,7 @@ pub struct BasicBlockData<'tcx> {
276276
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
277277
pub struct Terminator<'tcx> {
278278
pub span: Span,
279-
pub scope: ScopeId,
279+
pub scope: VisibilityScope,
280280
pub kind: TerminatorKind<'tcx>
281281
}
282282

@@ -588,7 +588,7 @@ pub enum AssertMessage<'tcx> {
588588
#[derive(Clone, RustcEncodable, RustcDecodable)]
589589
pub struct Statement<'tcx> {
590590
pub span: Span,
591-
pub scope: ScopeId,
591+
pub scope: VisibilityScope,
592592
pub kind: StatementKind<'tcx>,
593593
}
594594

@@ -754,29 +754,32 @@ impl<'tcx> Debug for Lvalue<'tcx> {
754754
///////////////////////////////////////////////////////////////////////////
755755
// Scopes
756756

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

760760
#[inline]
761-
fn index(&self, index: ScopeId) -> &ScopeData {
761+
fn index(&self, index: VisibilityScope) -> &VisibilityScopeData {
762762
&self[index.index()]
763763
}
764764
}
765765

766-
impl IndexMut<ScopeId> for Vec<ScopeData> {
766+
impl IndexMut<VisibilityScope> for Vec<VisibilityScopeData> {
767767
#[inline]
768-
fn index_mut(&mut self, index: ScopeId) -> &mut ScopeData {
768+
fn index_mut(&mut self, index: VisibilityScope) -> &mut VisibilityScopeData {
769769
&mut self[index.index()]
770770
}
771771
}
772772

773773
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
774-
pub struct ScopeId(u32);
774+
pub struct VisibilityScope(u32);
775775

776-
impl ScopeId {
777-
pub fn new(index: usize) -> ScopeId {
776+
/// The visibility scope all arguments go into.
777+
pub const ARGUMENT_VISIBILITY_SCOPE: VisibilityScope = VisibilityScope(0);
778+
779+
impl VisibilityScope {
780+
pub fn new(index: usize) -> VisibilityScope {
778781
assert!(index < (u32::MAX as usize));
779-
ScopeId(index as u32)
782+
VisibilityScope(index as u32)
780783
}
781784

782785
pub fn index(self) -> usize {
@@ -785,9 +788,9 @@ impl ScopeId {
785788
}
786789

787790
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
788-
pub struct ScopeData {
791+
pub struct VisibilityScopeData {
789792
pub span: Span,
790-
pub parent_scope: Option<ScopeId>,
793+
pub parent_scope: Option<VisibilityScope>,
791794
}
792795

793796
///////////////////////////////////////////////////////////////////////////

src/librustc/mir/visit.rs

+18-18
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,
@@ -236,9 +236,9 @@ macro_rules! make_mir_visitor {
236236
self.super_arg_decl(arg_decl);
237237
}
238238

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

244244
// The `super_xxx` methods comprise the default behavior and are
@@ -248,7 +248,7 @@ macro_rules! make_mir_visitor {
248248
mir: & $($mutability)* Mir<'tcx>) {
249249
let Mir {
250250
ref $($mutability)* basic_blocks,
251-
ref $($mutability)* scopes,
251+
ref $($mutability)* visibility_scopes,
252252
promoted: _, // Visited by passes separately.
253253
ref $($mutability)* return_ty,
254254
ref $($mutability)* var_decls,
@@ -263,8 +263,8 @@ macro_rules! make_mir_visitor {
263263
self.visit_basic_block_data(block, data);
264264
}
265265

266-
for scope in scopes {
267-
self.visit_scope_data(scope);
266+
for scope in visibility_scopes {
267+
self.visit_visibility_scope_data(scope);
268268
}
269269

270270
self.visit_fn_output(return_ty);
@@ -302,16 +302,16 @@ macro_rules! make_mir_visitor {
302302
}
303303
}
304304

305-
fn super_scope_data(&mut self,
306-
scope_data: & $($mutability)* ScopeData) {
307-
let ScopeData {
305+
fn super_visibility_scope_data(&mut self,
306+
scope_data: & $($mutability)* VisibilityScopeData) {
307+
let VisibilityScopeData {
308308
ref $($mutability)* span,
309309
ref $($mutability)* parent_scope,
310310
} = *scope_data;
311311

312312
self.visit_span(span);
313313
if let Some(ref $($mutability)* parent_scope) = *parent_scope {
314-
self.visit_scope_id(parent_scope);
314+
self.visit_visibility_scope(parent_scope);
315315
}
316316
}
317317

@@ -325,7 +325,7 @@ macro_rules! make_mir_visitor {
325325
} = *statement;
326326

327327
self.visit_span(span);
328-
self.visit_scope_id(scope);
328+
self.visit_visibility_scope(scope);
329329
match *kind {
330330
StatementKind::Assign(ref $($mutability)* lvalue,
331331
ref $($mutability)* rvalue) => {
@@ -352,7 +352,7 @@ macro_rules! make_mir_visitor {
352352
} = *terminator;
353353

354354
self.visit_span(span);
355-
self.visit_scope_id(scope);
355+
self.visit_visibility_scope(scope);
356356
self.visit_terminator_kind(block, kind);
357357
}
358358

@@ -627,7 +627,7 @@ macro_rules! make_mir_visitor {
627627
} = *var_decl;
628628

629629
self.visit_ty(ty);
630-
self.visit_scope_id(scope);
630+
self.visit_visibility_scope(scope);
631631
self.visit_span(span);
632632
}
633633

@@ -651,8 +651,8 @@ macro_rules! make_mir_visitor {
651651
self.visit_ty(ty);
652652
}
653653

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

658658
fn super_branch(&mut self,

src/librustc_mir/build/block.rs

+27-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2222
ast_block: &'tcx hir::Block)
2323
-> BlockAnd<()> {
2424
let Block { extent, span, stmts, expr } = self.hir.mirror(ast_block);
25-
self.in_scope(extent, block, move |this, _| {
25+
self.in_scope(extent, block, move |this| {
2626
// This convoluted structure is to avoid using recursion as we walk down a list
2727
// of statements. Basically, the structure we get back is something like:
2828
//
@@ -40,27 +40,40 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
4040
//
4141
// First we build all the statements in the block.
4242
let mut let_extent_stack = Vec::with_capacity(8);
43+
let outer_visibility_scope = this.visibility_scope;
4344
for stmt in stmts {
4445
let Stmt { span: _, kind } = this.hir.mirror(stmt);
4546
match kind {
4647
StmtKind::Expr { scope, expr } => {
47-
unpack!(block = this.in_scope(scope, block, |this, _| {
48+
unpack!(block = this.in_scope(scope, block, |this| {
4849
let expr = this.hir.mirror(expr);
4950
this.stmt_expr(block, expr)
5051
}));
5152
}
5253
StmtKind::Let { remainder_scope, init_scope, pattern, initializer } => {
53-
let remainder_scope_id = this.push_scope(remainder_scope, block);
54+
let tcx = this.hir.tcx();
55+
56+
// Enter the remainder scope, i.e. the bindings' destruction scope.
57+
this.push_scope(remainder_scope, block);
5458
let_extent_stack.push(remainder_scope);
55-
unpack!(block = this.in_scope(init_scope, block, move |this, _| {
56-
// FIXME #30046 ^~~~
57-
if let Some(init) = initializer {
58-
this.expr_into_pattern(block, remainder_scope_id, pattern, init)
59-
} else {
60-
this.declare_bindings(remainder_scope_id, &pattern);
61-
block.unit()
62-
}
63-
}));
59+
60+
// Declare the bindings, which may create a visibility scope.
61+
let remainder_span = remainder_scope.span(&tcx.region_maps, &tcx.map);
62+
let remainder_span = remainder_span.unwrap_or(span);
63+
let scope = this.declare_bindings(None, remainder_span, &pattern);
64+
65+
// Evaluate the initializer, if present.
66+
if let Some(init) = initializer {
67+
unpack!(block = this.in_scope(init_scope, block, move |this| {
68+
// FIXME #30046 ^~~~
69+
this.expr_into_pattern(block, pattern, init)
70+
}));
71+
}
72+
73+
// Enter the visibility scope, after evaluating the initializer.
74+
if let Some(visibility_scope) = scope {
75+
this.visibility_scope = visibility_scope;
76+
}
6477
}
6578
}
6679
}
@@ -78,6 +91,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7891
for extent in let_extent_stack.into_iter().rev() {
7992
unpack!(block = this.pop_scope(extent, block));
8093
}
94+
// Restore the original visibility scope.
95+
this.visibility_scope = outer_visibility_scope;
8196
block.unit()
8297
})
8398
}

src/librustc_mir/build/cfg.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'tcx> CFG<'tcx> {
5050

5151
pub fn push_assign(&mut self,
5252
block: BasicBlock,
53-
scope: ScopeId,
53+
scope: VisibilityScope,
5454
span: Span,
5555
lvalue: &Lvalue<'tcx>,
5656
rvalue: Rvalue<'tcx>) {
@@ -63,7 +63,7 @@ impl<'tcx> CFG<'tcx> {
6363

6464
pub fn push_assign_constant(&mut self,
6565
block: BasicBlock,
66-
scope: ScopeId,
66+
scope: VisibilityScope,
6767
span: Span,
6868
temp: &Lvalue<'tcx>,
6969
constant: Constant<'tcx>) {
@@ -73,7 +73,7 @@ impl<'tcx> CFG<'tcx> {
7373

7474
pub fn push_assign_unit(&mut self,
7575
block: BasicBlock,
76-
scope: ScopeId,
76+
scope: VisibilityScope,
7777
span: Span,
7878
lvalue: &Lvalue<'tcx>) {
7979
self.push_assign(block, scope, span, lvalue, Rvalue::Aggregate(
@@ -83,7 +83,7 @@ impl<'tcx> CFG<'tcx> {
8383

8484
pub fn terminate(&mut self,
8585
block: BasicBlock,
86-
scope: ScopeId,
86+
scope: VisibilityScope,
8787
span: Span,
8888
kind: TerminatorKind<'tcx>) {
8989
debug_assert!(self.block_data(block).terminator.is_none(),

src/librustc_mir/build/expr/as_lvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3838
let expr_span = expr.span;
3939
match expr.kind {
4040
ExprKind::Scope { extent, value } => {
41-
this.in_scope(extent, block, |this, _| this.as_lvalue(block, value))
41+
this.in_scope(extent, block, |this| this.as_lvalue(block, value))
4242
}
4343
ExprKind::Field { lhs, name } => {
4444
let lvalue = unpack!(block = this.as_lvalue(block, lhs));

src/librustc_mir/build/expr/as_operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3535
let this = self;
3636

3737
if let ExprKind::Scope { extent, value } = expr.kind {
38-
return this.in_scope(extent, block, |this, _| this.as_operand(block, value));
38+
return this.in_scope(extent, block, |this| this.as_operand(block, value));
3939
}
4040

4141
let category = Category::of(&expr.kind).unwrap();

src/librustc_mir/build/expr/as_rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
4646

4747
match expr.kind {
4848
ExprKind::Scope { extent, value } => {
49-
this.in_scope(extent, block, |this, _| this.as_rvalue(block, value))
49+
this.in_scope(extent, block, |this| this.as_rvalue(block, value))
5050
}
5151
ExprKind::InlineAsm { asm, outputs, inputs } => {
5252
let outputs = outputs.into_iter().map(|output| {
@@ -100,7 +100,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
100100
let result = this.temp(expr.ty);
101101
// to start, malloc some memory of suitable type (thus far, uninitialized):
102102
this.cfg.push_assign(block, scope_id, expr_span, &result, Rvalue::Box(value.ty));
103-
this.in_scope(value_extents, block, |this, _| {
103+
this.in_scope(value_extents, block, |this| {
104104
// schedule a shallow free of that memory, lest we unwind:
105105
this.schedule_box_free(expr_span, value_extents, &result, value.ty);
106106
// initialize the box contents:

src/librustc_mir/build/expr/as_temp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3030
let this = self;
3131

3232
if let ExprKind::Scope { extent, value } = expr.kind {
33-
return this.in_scope(extent, block, |this, _| this.as_temp(block, value));
33+
return this.in_scope(extent, block, |this| this.as_temp(block, value));
3434
}
3535

3636
let expr_ty = expr.ty.clone();

src/librustc_mir/build/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3737

3838
match expr.kind {
3939
ExprKind::Scope { extent, value } => {
40-
this.in_scope(extent, block, |this, _| this.into(destination, block, value))
40+
this.in_scope(extent, block, |this| this.into(destination, block, value))
4141
}
4242
ExprKind::Block { body: ast_block } => {
4343
this.ast_block(destination, expr.ty.is_nil(), block, ast_block)

src/librustc_mir/build/expr/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2626
match expr.kind {
2727
ExprKind::Scope { extent, value } => {
2828
let value = this.hir.mirror(value);
29-
this.in_scope(extent, block, |this, _| this.stmt_expr(block, value))
29+
this.in_scope(extent, block, |this| this.stmt_expr(block, value))
3030
}
3131
ExprKind::Assign { lhs, rhs } => {
3232
let lhs = this.hir.mirror(lhs);

0 commit comments

Comments
 (0)