Skip to content

Make rustc::middle::region::Scope's fields public #54260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/librustc/cfg/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,10 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
target_scope: region::Scope,
to_index: CFGIndex) {
let mut data = CFGEdgeData { exiting_scopes: vec![] };
let mut scope = region::Scope::Node(from_expr.hir_id.local_id);
let mut scope = region::Scope {
id: from_expr.hir_id.local_id,
data: region::ScopeData::Node
};
let region_scope_tree = self.tcx.region_scope_tree(self.owner_def_id);
while scope != target_scope {
data.exiting_scopes.push(scope.item_local_id());
Expand Down Expand Up @@ -586,17 +589,23 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
Ok(loop_id) => {
for b in &self.breakable_block_scopes {
if b.block_expr_id == self.tcx.hir.node_to_hir_id(loop_id).local_id {
let scope_id = self.tcx.hir.node_to_hir_id(loop_id).local_id;
return (region::Scope::Node(scope_id), match scope_cf_kind {
let scope = region::Scope {
id: self.tcx.hir.node_to_hir_id(loop_id).local_id,
data: region::ScopeData::Node
};
return (scope, match scope_cf_kind {
ScopeCfKind::Break => b.break_index,
ScopeCfKind::Continue => bug!("can't continue to block"),
});
}
}
for l in &self.loop_scopes {
if l.loop_id == self.tcx.hir.node_to_hir_id(loop_id).local_id {
let scope_id = self.tcx.hir.node_to_hir_id(loop_id).local_id;
return (region::Scope::Node(scope_id), match scope_cf_kind {
let scope = region::Scope {
id: self.tcx.hir.node_to_hir_id(loop_id).local_id,
data: region::ScopeData::Node
};
return (scope, match scope_cf_kind {
ScopeCfKind::Break => l.break_index,
ScopeCfKind::Continue => l.continue_index,
});
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
return;
}
};
let scope_decorated_tag = match scope.data() {
let scope_decorated_tag = match scope.data {
region::ScopeData::Node => tag,
region::ScopeData::CallSite => "scope of call-site for function",
region::ScopeData::Arguments => "scope of function body",
Expand Down
16 changes: 13 additions & 3 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
debug!("consume_body: arg_ty = {:?}", arg_ty);

let fn_body_scope_r =
self.tcx().mk_region(ty::ReScope(region::Scope::Node(body.value.hir_id.local_id)));
self.tcx().mk_region(ty::ReScope(
region::Scope {
id: body.value.hir_id.local_id,
data: region::ScopeData::Node
}));
let arg_cmt = Rc::new(self.mc.cat_rvalue(
arg.hir_id,
arg.pat.span,
Expand Down Expand Up @@ -558,7 +562,10 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
_ => {
if let Some(def) = self.mc.tables.type_dependent_defs().get(call.hir_id) {
let def_id = def.def_id();
let call_scope = region::Scope::Node(call.hir_id.local_id);
let call_scope = region::Scope {
id: call.hir_id.local_id,
data: region::ScopeData::Node
};
match OverloadedCallType::from_method_id(self.tcx(), def_id) {
FnMutOverloadedCall => {
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
Expand Down Expand Up @@ -766,7 +773,10 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
// treated as borrowing it for the enclosing temporary
// scope.
let r = self.tcx().mk_region(ty::ReScope(
region::Scope::Node(expr.hir_id.local_id)));
region::Scope {
id: expr.hir_id.local_id,
data: region::ScopeData::Node
}));

self.delegate.borrow(expr.id,
expr.span,
Expand Down
79 changes: 20 additions & 59 deletions src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
/// generated via deriving here.
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Copy, RustcEncodable, RustcDecodable)]
pub struct Scope {
pub(crate) id: hir::ItemLocalId,
pub(crate) data: ScopeData,
pub id: hir::ItemLocalId,
pub data: ScopeData,
}

impl fmt::Debug for Scope {
Expand Down Expand Up @@ -172,48 +172,6 @@ impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { private });
#[cfg(not(stage0))]
static ASSERT: () = [()][!(mem::size_of::<ScopeData>() == 4) as usize];

#[allow(non_snake_case)]
impl Scope {
#[inline]
pub fn data(self) -> ScopeData {
self.data
}

#[inline]
pub fn new(id: hir::ItemLocalId, data: ScopeData) -> Self {
Scope { id, data }
}

#[inline]
pub fn Node(id: hir::ItemLocalId) -> Self {
Self::new(id, ScopeData::Node)
}

#[inline]
pub fn CallSite(id: hir::ItemLocalId) -> Self {
Self::new(id, ScopeData::CallSite)
}

#[inline]
pub fn Arguments(id: hir::ItemLocalId) -> Self {
Self::new(id, ScopeData::Arguments)
}

#[inline]
pub fn Destruction(id: hir::ItemLocalId) -> Self {
Self::new(id, ScopeData::Destruction)
}

#[inline]
pub fn Remainder(
id: hir::ItemLocalId,
first: FirstStatementIndex,
) -> Self {
Self::new(id, ScopeData::Remainder(first))
}
}


impl Scope {
/// Returns a item-local id associated with this scope.
///
Expand Down Expand Up @@ -244,7 +202,7 @@ impl Scope {
return DUMMY_SP;
}
let span = tcx.hir.span(node_id);
if let ScopeData::Remainder(first_statement_index) = self.data() {
if let ScopeData::Remainder(first_statement_index) = self.data {
if let Node::Block(ref blk) = tcx.hir.get(node_id) {
// Want span for scope starting after the
// indexed statement and ending at end of
Expand Down Expand Up @@ -498,7 +456,7 @@ impl<'tcx> ScopeTree {
}

// record the destruction scopes for later so we can query them
if let ScopeData::Destruction = child.data() {
if let ScopeData::Destruction = child.data {
self.destruction_scopes.insert(child.item_local_id(), child);
}
}
Expand Down Expand Up @@ -578,10 +536,10 @@ impl<'tcx> ScopeTree {
// if there's one. Static items, for instance, won't
// have an enclosing scope, hence no scope will be
// returned.
let mut id = Scope::Node(expr_id);
let mut id = Scope { id: expr_id, data: ScopeData::Node };

while let Some(&(p, _)) = self.parent_map.get(&id) {
match p.data() {
match p.data {
ScopeData::Destruction => {
debug!("temporary_scope({:?}) = {:?} [enclosing]",
expr_id, id);
Expand Down Expand Up @@ -637,7 +595,7 @@ impl<'tcx> ScopeTree {
/// Returns the id of the innermost containing body
pub fn containing_body(&self, mut scope: Scope)-> Option<hir::ItemLocalId> {
loop {
if let ScopeData::CallSite = scope.data() {
if let ScopeData::CallSite = scope.data {
return Some(scope.item_local_id());
}

Expand Down Expand Up @@ -730,7 +688,7 @@ impl<'tcx> ScopeTree {
self.root_body.unwrap().local_id
});

Scope::CallSite(scope)
Scope { id: scope, data: ScopeData::CallSite }
}

/// Assuming that the provided region was defined within this `ScopeTree`,
Expand All @@ -750,7 +708,7 @@ impl<'tcx> ScopeTree {

let param_owner_id = tcx.hir.as_local_node_id(param_owner).unwrap();
let body_id = tcx.hir.body_owned_by(param_owner_id);
Scope::CallSite(tcx.hir.body(body_id).value.hir_id.local_id)
Scope { id: tcx.hir.body(body_id).value.hir_id.local_id, data: ScopeData::CallSite }
}

/// Checks whether the given scope contains a `yield`. If so,
Expand Down Expand Up @@ -854,7 +812,10 @@ fn resolve_block<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, blk:
// except for the first such subscope, which has the
// block itself as a parent.
visitor.enter_scope(
Scope::Remainder(blk.hir_id.local_id, FirstStatementIndex::new(i))
Scope {
id: blk.hir_id.local_id,
data: ScopeData::Remainder(FirstStatementIndex::new(i))
}
);
visitor.cx.var_parent = visitor.cx.parent;
}
Expand All @@ -879,7 +840,7 @@ fn resolve_arm<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, arm: &
}

fn resolve_pat<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, pat: &'tcx hir::Pat) {
visitor.record_child_scope(Scope::Node(pat.hir_id.local_id));
visitor.record_child_scope(Scope { id: pat.hir_id.local_id, data: ScopeData::Node });

// If this is a binding then record the lifetime of that binding.
if let PatKind::Binding(..) = pat.node {
Expand Down Expand Up @@ -1008,15 +969,15 @@ fn resolve_expr<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, expr:

if let hir::ExprKind::Yield(..) = expr.node {
// Mark this expr's scope and all parent scopes as containing `yield`.
let mut scope = Scope::Node(expr.hir_id.local_id);
let mut scope = Scope { id: expr.hir_id.local_id, data: ScopeData::Node };
loop {
visitor.scope_tree.yield_in_scope.insert(scope,
(expr.span, visitor.expr_and_pat_count));

// Keep traversing up while we can.
match visitor.scope_tree.parent_map.get(&scope) {
// Don't cross from closure bodies to their parent.
Some(&(superscope, _)) => match superscope.data() {
Some(&(superscope, _)) => match superscope.data {
ScopeData::CallSite => break,
_ => scope = superscope
},
Expand Down Expand Up @@ -1280,9 +1241,9 @@ impl<'a, 'tcx> RegionResolutionVisitor<'a, 'tcx> {
// account for the destruction scope representing the scope of
// the destructors that run immediately after it completes.
if self.terminating_scopes.contains(&id) {
self.enter_scope(Scope::Destruction(id));
self.enter_scope(Scope { id, data: ScopeData::Destruction });
}
self.enter_scope(Scope::Node(id));
self.enter_scope(Scope { id, data: ScopeData::Node });
}
}

Expand Down Expand Up @@ -1315,8 +1276,8 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionResolutionVisitor<'a, 'tcx> {
}
self.cx.root_id = Some(body.value.hir_id.local_id);

self.enter_scope(Scope::CallSite(body.value.hir_id.local_id));
self.enter_scope(Scope::Arguments(body.value.hir_id.local_id));
self.enter_scope(Scope { id: body.value.hir_id.local_id, data: ScopeData::CallSite });
self.enter_scope(Scope { id: body.value.hir_id.local_id, data: ScopeData::Arguments });

// The arguments and `self` are parented to the fn.
self.cx.var_parent = self.cx.parent.take();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ define_print! {
write!(f, "{}", br)
}
ty::ReScope(scope) if cx.identify_regions => {
match scope.data() {
match scope.data {
region::ScopeData::Node =>
write!(f, "'{}s", scope.item_local_id().as_usize()),
region::ScopeData::CallSite =>
Expand Down
11 changes: 9 additions & 2 deletions src/librustc_borrowck/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,12 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {

let mut ret = UseOk;

let scope = region::Scope {
id: expr_id,
data: region::ScopeData::Node
};
self.each_in_scope_loan_affecting_path(
region::Scope::Node(expr_id), use_path, |loan| {
scope, use_path, |loan| {
if !compatible_borrow_kinds(loan.kind, borrow_kind) {
ret = UseWhileBorrowed(loan.loan_path.clone(), loan.span);
false
Expand Down Expand Up @@ -886,7 +890,10 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {

// Check that we don't invalidate any outstanding loans
if let Some(loan_path) = opt_loan_path(assignee_cmt) {
let scope = region::Scope::Node(assignment_id);
let scope = region::Scope {
id: assignment_id,
data: region::ScopeData::Node
};
self.each_in_scope_loan_affecting_path(scope, &loan_path, |loan| {
self.report_illegal_mutation(assignment_span, &loan_path, loan);
false
Expand Down
10 changes: 8 additions & 2 deletions src/librustc_borrowck/borrowck/gather_loans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ pub fn gather_loans_in_fn<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
let mut glcx = GatherLoanCtxt {
bccx,
all_loans: Vec::new(),
item_ub: region::Scope::Node(bccx.tcx.hir.body(body).value.hir_id.local_id),
item_ub: region::Scope {
id: bccx.tcx.hir.body(body).value.hir_id.local_id,
data: region::ScopeData::Node
},
move_data: MoveData::default(),
move_error_collector: move_error::MoveErrorCollector::new(),
};
Expand Down Expand Up @@ -375,7 +378,10 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
};
debug!("loan_scope = {:?}", loan_scope);

let borrow_scope = region::Scope::Node(borrow_id);
let borrow_scope = region::Scope {
id: borrow_id,
data: region::ScopeData::Node
};
let gen_scope = self.compute_gen_scope(borrow_scope, loan_scope);
debug!("gen_scope = {:?}", gen_scope);

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ impl<'a, 'tcx> LoanPath<'tcx> {
LpUpvar(upvar_id) => {
let block_id = closure_to_block(upvar_id.closure_expr_id, bccx.tcx);
let hir_id = bccx.tcx.hir.node_to_hir_id(block_id);
region::Scope::Node(hir_id.local_id)
region::Scope { id: hir_id.local_id, data: region::ScopeData::Node }
}
LpDowncast(ref base, _) |
LpExtend(ref base, ..) => base.kill_scope(bccx),
Expand Down
12 changes: 9 additions & 3 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {

pub fn create_region_hierarchy(&mut self, rh: &RH,
parent: (region::Scope, region::ScopeDepth)) {
let me = region::Scope::Node(rh.id);
let me = region::Scope { id: rh.id, data: region::ScopeData::Node };
self.region_scope_tree.record_scope_parent(me, Some(parent));
for child_rh in rh.sub {
self.create_region_hierarchy(child_rh, (me, parent.1 + 1));
Expand All @@ -209,7 +209,10 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
// creates a region hierarchy where 1 is root, 10 and 11 are
// children of 1, etc

let dscope = region::Scope::Destruction(hir::ItemLocalId(1));
let dscope = region::Scope {
id: hir::ItemLocalId(1),
data: region::ScopeData::Destruction
};
self.region_scope_tree.record_scope_parent(dscope, None);
self.create_region_hierarchy(&RH {
id: hir::ItemLocalId(1),
Expand Down Expand Up @@ -355,7 +358,10 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
}

pub fn t_rptr_scope(&self, id: u32) -> Ty<'tcx> {
let r = ty::ReScope(region::Scope::Node(hir::ItemLocalId(id)));
let r = ty::ReScope(region::Scope {
id: hir::ItemLocalId(id),
data: region::ScopeData::Node
});
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'tcx> CFG<'tcx> {
source_info: SourceInfo,
region_scope: region::Scope) {
if tcx.emit_end_regions() {
if let region::ScopeData::CallSite = region_scope.data() {
if let region::ScopeData::CallSite = region_scope.data {
// The CallSite scope (aka the root scope) is sort of weird, in that it is
// supposed to "separate" the "interior" and "exterior" of a closure. Being
// that, it is not really a part of the region hierarchy, but for some
Expand Down
10 changes: 8 additions & 2 deletions src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,14 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
upvar_decls);

let fn_def_id = tcx.hir.local_def_id(fn_id);
let call_site_scope = region::Scope::CallSite(body.value.hir_id.local_id);
let arg_scope = region::Scope::Arguments(body.value.hir_id.local_id);
let call_site_scope = region::Scope {
id: body.value.hir_id.local_id,
data: region::ScopeData::CallSite
};
let arg_scope = region::Scope {
id: body.value.hir_id.local_id,
data: region::ScopeData::Arguments
};
let mut block = START_BLOCK;
let source_info = builder.source_info(span);
let call_site_s = (call_site_scope, source_info);
Expand Down
Loading