Skip to content

Commit 8214ab1

Browse files
committed
move Scope behind an enum
1 parent b6bce56 commit 8214ab1

File tree

6 files changed

+118
-37
lines changed

6 files changed

+118
-37
lines changed

Diff for: src/librustc/ich/impls_ty.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,17 @@ impl_stable_hash_for!(enum ty::cast::CastKind {
514514
FnPtrAddrCast
515515
});
516516

517-
impl_stable_hash_for!(enum ::middle::region::Scope {
517+
impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { idx });
518+
519+
impl<'gcx> HashStable<StableHashingContext<'gcx>> for ::middle::region::Scope {
520+
fn hash_stable<W: StableHasherResult>(&self,
521+
hcx: &mut StableHashingContext<'gcx>,
522+
hasher: &mut StableHasher<W>) {
523+
self.data().hash_stable(hcx, hasher)
524+
}
525+
}
526+
527+
impl_stable_hash_for!(enum ::middle::region::ScopeData {
518528
Node(local_id),
519529
Destruction(local_id),
520530
CallSite(local_id),

Diff for: src/librustc/infer/error_reporting/mod.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ use syntax::ast::DUMMY_NODE_ID;
7272
use syntax_pos::{Pos, Span};
7373
use errors::{DiagnosticBuilder, DiagnosticStyledString};
7474

75+
use rustc_data_structures::indexed_vec::Idx;
76+
7577
mod note;
7678

7779
mod need_type_info;
@@ -152,21 +154,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
152154
return;
153155
}
154156
};
155-
let scope_decorated_tag = match scope {
156-
region::Scope::Node(_) => tag,
157-
region::Scope::CallSite(_) => {
157+
let scope_decorated_tag = match scope.data() {
158+
region::ScopeData::Node(_) => tag,
159+
region::ScopeData::CallSite(_) => {
158160
"scope of call-site for function"
159161
}
160-
region::Scope::Arguments(_) => {
162+
region::ScopeData::Arguments(_) => {
161163
"scope of function body"
162164
}
163-
region::Scope::Destruction(_) => {
165+
region::ScopeData::Destruction(_) => {
164166
new_string = format!("destruction scope surrounding {}", tag);
165167
&new_string[..]
166168
}
167-
region::Scope::Remainder(r) => {
169+
region::ScopeData::Remainder(r) => {
168170
new_string = format!("block suffix following statement {}",
169-
r.first_statement_index);
171+
r.first_statement_index.index());
170172
&new_string[..]
171173
}
172174
};

Diff for: src/librustc/middle/region.rs

+85-19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use hir::def_id::DefId;
3131
use hir::intravisit::{self, Visitor, NestedVisitorMap};
3232
use hir::{Block, Arm, Pat, PatKind, Stmt, Expr, Local};
3333
use mir::transform::MirSource;
34+
use rustc_data_structures::indexed_vec::Idx;
3435
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
3536
StableHasherResult};
3637

@@ -96,7 +97,12 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
9697
/// actually attach a more meaningful ordering to scopes than the one
9798
/// generated via deriving here.
9899
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, RustcEncodable, RustcDecodable)]
99-
pub enum Scope {
100+
pub struct Scope {
101+
pub scope_data: ScopeData
102+
}
103+
104+
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, RustcEncodable, RustcDecodable)]
105+
pub enum ScopeData {
100106
Node(hir::ItemLocalId),
101107

102108
// Scope of the call-site for a function or closure
@@ -135,7 +141,64 @@ pub enum Scope {
135141
RustcDecodable, Debug, Copy)]
136142
pub struct BlockRemainder {
137143
pub block: hir::ItemLocalId,
138-
pub first_statement_index: u32,
144+
pub first_statement_index: FirstStatementIndex,
145+
}
146+
147+
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, RustcEncodable,
148+
RustcDecodable, Debug, Copy)]
149+
pub struct FirstStatementIndex { pub idx: u32 }
150+
151+
pub const FIRST_STATEMENT_MAX: usize = !0u32 as usize;
152+
153+
impl Idx for FirstStatementIndex {
154+
fn new(idx: usize) -> Self {
155+
assert!(idx <= FIRST_STATEMENT_MAX);
156+
FirstStatementIndex { idx: idx as u32 }
157+
}
158+
159+
fn index(self) -> usize {
160+
self.idx as usize
161+
}
162+
}
163+
164+
impl From<ScopeData> for Scope {
165+
#[inline]
166+
fn from(scope_data: ScopeData) -> Self {
167+
Self { scope_data }
168+
}
169+
}
170+
171+
#[allow(non_snake_case)]
172+
impl Scope {
173+
#[inline]
174+
pub fn data(self) -> ScopeData {
175+
self.scope_data
176+
}
177+
178+
#[inline]
179+
pub fn Node(id: hir::ItemLocalId) -> Self {
180+
Self::from(ScopeData::Node(id))
181+
}
182+
183+
#[inline]
184+
pub fn CallSite(id: hir::ItemLocalId) -> Self {
185+
Self::from(ScopeData::CallSite(id))
186+
}
187+
188+
#[inline]
189+
pub fn Arguments(id: hir::ItemLocalId) -> Self {
190+
Self::from(ScopeData::Arguments(id))
191+
}
192+
193+
#[inline]
194+
pub fn Destruction(id: hir::ItemLocalId) -> Self {
195+
Self::from(ScopeData::Destruction(id))
196+
}
197+
198+
#[inline]
199+
pub fn Remainder(r: BlockRemainder) -> Self {
200+
Self::from(ScopeData::Remainder(r))
201+
}
139202
}
140203

141204
impl Scope {
@@ -144,15 +207,16 @@ impl Scope {
144207
/// NB: likely to be replaced as API is refined; e.g. pnkfelix
145208
/// anticipates `fn entry_node_id` and `fn each_exit_node_id`.
146209
pub fn item_local_id(&self) -> hir::ItemLocalId {
147-
match *self {
148-
Scope::Node(id) => id,
210+
// TODO: killme
211+
match self.data() {
212+
ScopeData::Node(id) => id,
149213

150214
// These cases all return rough approximations to the
151215
// precise scope denoted by `self`.
152-
Scope::Remainder(br) => br.block,
153-
Scope::Destruction(id) |
154-
Scope::CallSite(id) |
155-
Scope::Arguments(id) => id,
216+
ScopeData::Remainder(br) => br.block,
217+
ScopeData::Destruction(id) |
218+
ScopeData::CallSite(id) |
219+
ScopeData::Arguments(id) => id,
156220
}
157221
}
158222

@@ -177,7 +241,7 @@ impl Scope {
177241
return DUMMY_SP;
178242
}
179243
let span = tcx.hir.span(node_id);
180-
if let Scope::Remainder(r) = *self {
244+
if let ScopeData::Remainder(r) = self.data() {
181245
if let hir::map::NodeBlock(ref blk) = tcx.hir.get(node_id) {
182246
// Want span for scope starting after the
183247
// indexed statement and ending at end of
@@ -187,7 +251,7 @@ impl Scope {
187251
// (This is the special case aluded to in the
188252
// doc-comment for this method)
189253

190-
let stmt_span = blk.stmts[r.first_statement_index as usize].span;
254+
let stmt_span = blk.stmts[r.first_statement_index.index()].span;
191255

192256
// To avoid issues with macro-generated spans, the span
193257
// of the statement must be nested in that of the block.
@@ -387,7 +451,7 @@ impl<'tcx> ScopeTree {
387451
}
388452

389453
// record the destruction scopes for later so we can query them
390-
if let Scope::Destruction(n) = child {
454+
if let ScopeData::Destruction(n) = child.data() {
391455
self.destruction_scopes.insert(n, child);
392456
}
393457
}
@@ -482,8 +546,8 @@ impl<'tcx> ScopeTree {
482546
let mut id = Scope::Node(expr_id);
483547

484548
while let Some(&p) = self.parent_map.get(&id) {
485-
match p {
486-
Scope::Destruction(..) => {
549+
match p.data() {
550+
ScopeData::Destruction(..) => {
487551
debug!("temporary_scope({:?}) = {:?} [enclosing]",
488552
expr_id, id);
489553
return Some(id);
@@ -573,9 +637,9 @@ impl<'tcx> ScopeTree {
573637
// infer::region_inference for more details.
574638
let a_root_scope = a_ancestors[a_index];
575639
let b_root_scope = a_ancestors[a_index];
576-
return match (a_root_scope, b_root_scope) {
577-
(Scope::Destruction(a_root_id),
578-
Scope::Destruction(b_root_id)) => {
640+
return match (a_root_scope.data(), b_root_scope.data()) {
641+
(ScopeData::Destruction(a_root_id),
642+
ScopeData::Destruction(b_root_id)) => {
579643
if self.closure_is_enclosed_by(a_root_id, b_root_id) {
580644
// `a` is enclosed by `b`, hence `b` is the ancestor of everything in `a`
581645
scope_b
@@ -764,7 +828,7 @@ fn resolve_block<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, blk:
764828
visitor.enter_scope(
765829
Scope::Remainder(BlockRemainder {
766830
block: blk.hir_id.local_id,
767-
first_statement_index: i as u32
831+
first_statement_index: FirstStatementIndex::new(i)
768832
})
769833
);
770834
visitor.cx.var_parent = visitor.cx.parent;
@@ -915,8 +979,10 @@ fn resolve_expr<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, expr:
915979
// Keep traversing up while we can.
916980
match visitor.scope_tree.parent_map.get(&scope) {
917981
// Don't cross from closure bodies to their parent.
918-
Some(&Scope::CallSite(_)) => break,
919-
Some(&superscope) => scope = superscope,
982+
Some(&superscope) => match superscope.data() {
983+
ScopeData::CallSite(_) => break,
984+
_ => scope = superscope
985+
},
920986
None => break
921987
}
922988
}

Diff for: src/librustc/util/ppaux.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::fmt;
2626
use std::usize;
2727

2828
use rustc_const_math::ConstInt;
29+
use rustc_data_structures::indexed_vec::Idx;
2930
use syntax::abi::Abi;
3031
use syntax::ast::CRATE_NODE_ID;
3132
use syntax::symbol::Symbol;
@@ -530,17 +531,17 @@ impl fmt::Display for ty::RegionKind {
530531
write!(f, "{}", br)
531532
}
532533
ty::ReScope(scope) if identify_regions() => {
533-
match scope {
534-
region::Scope::Node(id) =>
534+
match scope.data() {
535+
region::ScopeData::Node(id) =>
535536
write!(f, "'{}s", id.as_usize()),
536-
region::Scope::CallSite(id) =>
537+
region::ScopeData::CallSite(id) =>
537538
write!(f, "'{}cs", id.as_usize()),
538-
region::Scope::Arguments(id) =>
539+
region::ScopeData::Arguments(id) =>
539540
write!(f, "'{}as", id.as_usize()),
540-
region::Scope::Destruction(id) =>
541+
region::ScopeData::Destruction(id) =>
541542
write!(f, "'{}ds", id.as_usize()),
542-
region::Scope::Remainder(BlockRemainder { block, first_statement_index }) =>
543-
write!(f, "'{}_{}rs", block.as_usize(), first_statement_index),
543+
region::ScopeData::Remainder(BlockRemainder { block, first_statement_index }) =>
544+
write!(f, "'{}_{}rs", block.as_usize(), first_statement_index.index()),
544545
}
545546
}
546547
ty::ReVar(region_vid) if identify_regions() => {

Diff for: src/librustc_mir/build/scope.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
514514
// The outermost scope (`scopes[0]`) will be the `CallSiteScope`.
515515
// We want `scopes[1]`, which is the `ParameterScope`.
516516
assert!(self.scopes.len() >= 2);
517-
assert!(match self.scopes[1].region_scope {
518-
region::Scope::Arguments(_) => true,
517+
assert!(match self.scopes[1].region_scope.data() {
518+
region::ScopeData::Arguments(_) => true,
519519
_ => false,
520520
});
521521
self.scopes[1].region_scope

Diff for: src/librustc_mir/hair/cx/block.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use hair::cx::to_ref::ToRef;
1414
use rustc::middle::region::{self, BlockRemainder};
1515
use rustc::hir;
1616

17+
use rustc_data_structures::indexed_vec::Idx;
18+
1719
impl<'tcx> Mirror<'tcx> for &'tcx hir::Block {
1820
type Output = Block<'tcx>;
1921

@@ -61,7 +63,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
6163
hir::DeclLocal(ref local) => {
6264
let remainder_scope = region::Scope::Remainder(BlockRemainder {
6365
block: block_id,
64-
first_statement_index: index as u32,
66+
first_statement_index: region::FirstStatementIndex::new(index),
6567
});
6668

6769
let pattern = cx.pattern_from_hir(&local.pat);

0 commit comments

Comments
 (0)