Skip to content

Commit ed94397

Browse files
committed
Auto merge of #54260 - maxdeviant:public-scope-fields, r=petrochenkov
Make rustc::middle::region::Scope's fields public This PR makes the following changes to `rustc::middle::region::Scope`: - [x] Makes `region::Scope`'s fields public - [x] Removes the `impl Scope` block with constructors (as per [this comment](#54032 (comment))) - [x] Updates call sites throughout the compiler Closes #54122.
2 parents 0b0d2ed + 2d7176f commit ed94397

File tree

17 files changed

+157
-101
lines changed

17 files changed

+157
-101
lines changed

src/librustc/cfg/construct.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,10 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
556556
target_scope: region::Scope,
557557
to_index: CFGIndex) {
558558
let mut data = CFGEdgeData { exiting_scopes: vec![] };
559-
let mut scope = region::Scope::Node(from_expr.hir_id.local_id);
559+
let mut scope = region::Scope {
560+
id: from_expr.hir_id.local_id,
561+
data: region::ScopeData::Node
562+
};
560563
let region_scope_tree = self.tcx.region_scope_tree(self.owner_def_id);
561564
while scope != target_scope {
562565
data.exiting_scopes.push(scope.item_local_id());
@@ -586,17 +589,23 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
586589
Ok(loop_id) => {
587590
for b in &self.breakable_block_scopes {
588591
if b.block_expr_id == self.tcx.hir.node_to_hir_id(loop_id).local_id {
589-
let scope_id = self.tcx.hir.node_to_hir_id(loop_id).local_id;
590-
return (region::Scope::Node(scope_id), match scope_cf_kind {
592+
let scope = region::Scope {
593+
id: self.tcx.hir.node_to_hir_id(loop_id).local_id,
594+
data: region::ScopeData::Node
595+
};
596+
return (scope, match scope_cf_kind {
591597
ScopeCfKind::Break => b.break_index,
592598
ScopeCfKind::Continue => bug!("can't continue to block"),
593599
});
594600
}
595601
}
596602
for l in &self.loop_scopes {
597603
if l.loop_id == self.tcx.hir.node_to_hir_id(loop_id).local_id {
598-
let scope_id = self.tcx.hir.node_to_hir_id(loop_id).local_id;
599-
return (region::Scope::Node(scope_id), match scope_cf_kind {
604+
let scope = region::Scope {
605+
id: self.tcx.hir.node_to_hir_id(loop_id).local_id,
606+
data: region::ScopeData::Node
607+
};
608+
return (scope, match scope_cf_kind {
600609
ScopeCfKind::Break => l.break_index,
601610
ScopeCfKind::Continue => l.continue_index,
602611
});

src/librustc/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
118118
return;
119119
}
120120
};
121-
let scope_decorated_tag = match scope.data() {
121+
let scope_decorated_tag = match scope.data {
122122
region::ScopeData::Node => tag,
123123
region::ScopeData::CallSite => "scope of call-site for function",
124124
region::ScopeData::Arguments => "scope of function body",

src/librustc/middle/expr_use_visitor.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
317317
debug!("consume_body: arg_ty = {:?}", arg_ty);
318318

319319
let fn_body_scope_r =
320-
self.tcx().mk_region(ty::ReScope(region::Scope::Node(body.value.hir_id.local_id)));
320+
self.tcx().mk_region(ty::ReScope(
321+
region::Scope {
322+
id: body.value.hir_id.local_id,
323+
data: region::ScopeData::Node
324+
}));
321325
let arg_cmt = Rc::new(self.mc.cat_rvalue(
322326
arg.hir_id,
323327
arg.pat.span,
@@ -558,7 +562,10 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
558562
_ => {
559563
if let Some(def) = self.mc.tables.type_dependent_defs().get(call.hir_id) {
560564
let def_id = def.def_id();
561-
let call_scope = region::Scope::Node(call.hir_id.local_id);
565+
let call_scope = region::Scope {
566+
id: call.hir_id.local_id,
567+
data: region::ScopeData::Node
568+
};
562569
match OverloadedCallType::from_method_id(self.tcx(), def_id) {
563570
FnMutOverloadedCall => {
564571
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
@@ -766,7 +773,10 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
766773
// treated as borrowing it for the enclosing temporary
767774
// scope.
768775
let r = self.tcx().mk_region(ty::ReScope(
769-
region::Scope::Node(expr.hir_id.local_id)));
776+
region::Scope {
777+
id: expr.hir_id.local_id,
778+
data: region::ScopeData::Node
779+
}));
770780

771781
self.delegate.borrow(expr.id,
772782
expr.span,

src/librustc/middle/region.rs

+20-59
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
102102
/// generated via deriving here.
103103
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Copy, RustcEncodable, RustcDecodable)]
104104
pub struct Scope {
105-
pub(crate) id: hir::ItemLocalId,
106-
pub(crate) data: ScopeData,
105+
pub id: hir::ItemLocalId,
106+
pub data: ScopeData,
107107
}
108108

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

175-
#[allow(non_snake_case)]
176-
impl Scope {
177-
#[inline]
178-
pub fn data(self) -> ScopeData {
179-
self.data
180-
}
181-
182-
#[inline]
183-
pub fn new(id: hir::ItemLocalId, data: ScopeData) -> Self {
184-
Scope { id, data }
185-
}
186-
187-
#[inline]
188-
pub fn Node(id: hir::ItemLocalId) -> Self {
189-
Self::new(id, ScopeData::Node)
190-
}
191-
192-
#[inline]
193-
pub fn CallSite(id: hir::ItemLocalId) -> Self {
194-
Self::new(id, ScopeData::CallSite)
195-
}
196-
197-
#[inline]
198-
pub fn Arguments(id: hir::ItemLocalId) -> Self {
199-
Self::new(id, ScopeData::Arguments)
200-
}
201-
202-
#[inline]
203-
pub fn Destruction(id: hir::ItemLocalId) -> Self {
204-
Self::new(id, ScopeData::Destruction)
205-
}
206-
207-
#[inline]
208-
pub fn Remainder(
209-
id: hir::ItemLocalId,
210-
first: FirstStatementIndex,
211-
) -> Self {
212-
Self::new(id, ScopeData::Remainder(first))
213-
}
214-
}
215-
216-
217175
impl Scope {
218176
/// Returns a item-local id associated with this scope.
219177
///
@@ -244,7 +202,7 @@ impl Scope {
244202
return DUMMY_SP;
245203
}
246204
let span = tcx.hir.span(node_id);
247-
if let ScopeData::Remainder(first_statement_index) = self.data() {
205+
if let ScopeData::Remainder(first_statement_index) = self.data {
248206
if let Node::Block(ref blk) = tcx.hir.get(node_id) {
249207
// Want span for scope starting after the
250208
// indexed statement and ending at end of
@@ -498,7 +456,7 @@ impl<'tcx> ScopeTree {
498456
}
499457

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

583541
while let Some(&(p, _)) = self.parent_map.get(&id) {
584-
match p.data() {
542+
match p.data {
585543
ScopeData::Destruction => {
586544
debug!("temporary_scope({:?}) = {:?} [enclosing]",
587545
expr_id, id);
@@ -637,7 +595,7 @@ impl<'tcx> ScopeTree {
637595
/// Returns the id of the innermost containing body
638596
pub fn containing_body(&self, mut scope: Scope)-> Option<hir::ItemLocalId> {
639597
loop {
640-
if let ScopeData::CallSite = scope.data() {
598+
if let ScopeData::CallSite = scope.data {
641599
return Some(scope.item_local_id());
642600
}
643601

@@ -730,7 +688,7 @@ impl<'tcx> ScopeTree {
730688
self.root_body.unwrap().local_id
731689
});
732690

733-
Scope::CallSite(scope)
691+
Scope { id: scope, data: ScopeData::CallSite }
734692
}
735693

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

751709
let param_owner_id = tcx.hir.as_local_node_id(param_owner).unwrap();
752710
let body_id = tcx.hir.body_owned_by(param_owner_id);
753-
Scope::CallSite(tcx.hir.body(body_id).value.hir_id.local_id)
711+
Scope { id: tcx.hir.body(body_id).value.hir_id.local_id, data: ScopeData::CallSite }
754712
}
755713

756714
/// Checks whether the given scope contains a `yield`. If so,
@@ -854,7 +812,10 @@ fn resolve_block<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, blk:
854812
// except for the first such subscope, which has the
855813
// block itself as a parent.
856814
visitor.enter_scope(
857-
Scope::Remainder(blk.hir_id.local_id, FirstStatementIndex::new(i))
815+
Scope {
816+
id: blk.hir_id.local_id,
817+
data: ScopeData::Remainder(FirstStatementIndex::new(i))
818+
}
858819
);
859820
visitor.cx.var_parent = visitor.cx.parent;
860821
}
@@ -879,7 +840,7 @@ fn resolve_arm<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, arm: &
879840
}
880841

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

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

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

1016977
// Keep traversing up while we can.
1017978
match visitor.scope_tree.parent_map.get(&scope) {
1018979
// Don't cross from closure bodies to their parent.
1019-
Some(&(superscope, _)) => match superscope.data() {
980+
Some(&(superscope, _)) => match superscope.data {
1020981
ScopeData::CallSite => break,
1021982
_ => scope = superscope
1022983
},
@@ -1280,9 +1241,9 @@ impl<'a, 'tcx> RegionResolutionVisitor<'a, 'tcx> {
12801241
// account for the destruction scope representing the scope of
12811242
// the destructors that run immediately after it completes.
12821243
if self.terminating_scopes.contains(&id) {
1283-
self.enter_scope(Scope::Destruction(id));
1244+
self.enter_scope(Scope { id, data: ScopeData::Destruction });
12841245
}
1285-
self.enter_scope(Scope::Node(id));
1246+
self.enter_scope(Scope { id, data: ScopeData::Node });
12861247
}
12871248
}
12881249

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

1318-
self.enter_scope(Scope::CallSite(body.value.hir_id.local_id));
1319-
self.enter_scope(Scope::Arguments(body.value.hir_id.local_id));
1279+
self.enter_scope(Scope { id: body.value.hir_id.local_id, data: ScopeData::CallSite });
1280+
self.enter_scope(Scope { id: body.value.hir_id.local_id, data: ScopeData::Arguments });
13201281

13211282
// The arguments and `self` are parented to the fn.
13221283
self.cx.var_parent = self.cx.parent.take();

src/librustc/util/ppaux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ define_print! {
769769
write!(f, "{}", br)
770770
}
771771
ty::ReScope(scope) if cx.identify_regions => {
772-
match scope.data() {
772+
match scope.data {
773773
region::ScopeData::Node =>
774774
write!(f, "'{}s", scope.item_local_id().as_usize()),
775775
region::ScopeData::CallSite =>

src/librustc_borrowck/borrowck/check_loans.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,12 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
767767

768768
let mut ret = UseOk;
769769

770+
let scope = region::Scope {
771+
id: expr_id,
772+
data: region::ScopeData::Node
773+
};
770774
self.each_in_scope_loan_affecting_path(
771-
region::Scope::Node(expr_id), use_path, |loan| {
775+
scope, use_path, |loan| {
772776
if !compatible_borrow_kinds(loan.kind, borrow_kind) {
773777
ret = UseWhileBorrowed(loan.loan_path.clone(), loan.span);
774778
false
@@ -886,7 +890,10 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
886890

887891
// Check that we don't invalidate any outstanding loans
888892
if let Some(loan_path) = opt_loan_path(assignee_cmt) {
889-
let scope = region::Scope::Node(assignment_id);
893+
let scope = region::Scope {
894+
id: assignment_id,
895+
data: region::ScopeData::Node
896+
};
890897
self.each_in_scope_loan_affecting_path(scope, &loan_path, |loan| {
891898
self.report_illegal_mutation(assignment_span, &loan_path, loan);
892899
false

src/librustc_borrowck/borrowck/gather_loans/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ pub fn gather_loans_in_fn<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
4343
let mut glcx = GatherLoanCtxt {
4444
bccx,
4545
all_loans: Vec::new(),
46-
item_ub: region::Scope::Node(bccx.tcx.hir.body(body).value.hir_id.local_id),
46+
item_ub: region::Scope {
47+
id: bccx.tcx.hir.body(body).value.hir_id.local_id,
48+
data: region::ScopeData::Node
49+
},
4750
move_data: MoveData::default(),
4851
move_error_collector: move_error::MoveErrorCollector::new(),
4952
};
@@ -375,7 +378,10 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
375378
};
376379
debug!("loan_scope = {:?}", loan_scope);
377380

378-
let borrow_scope = region::Scope::Node(borrow_id);
381+
let borrow_scope = region::Scope {
382+
id: borrow_id,
383+
data: region::ScopeData::Node
384+
};
379385
let gen_scope = self.compute_gen_scope(borrow_scope, loan_scope);
380386
debug!("gen_scope = {:?}", gen_scope);
381387

src/librustc_borrowck/borrowck/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ impl<'a, 'tcx> LoanPath<'tcx> {
441441
LpUpvar(upvar_id) => {
442442
let block_id = closure_to_block(upvar_id.closure_expr_id, bccx.tcx);
443443
let hir_id = bccx.tcx.hir.node_to_hir_id(block_id);
444-
region::Scope::Node(hir_id.local_id)
444+
region::Scope { id: hir_id.local_id, data: region::ScopeData::Node }
445445
}
446446
LpDowncast(ref base, _) |
447447
LpExtend(ref base, ..) => base.kill_scope(bccx),

src/librustc_driver/test.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
198198

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

212-
let dscope = region::Scope::Destruction(hir::ItemLocalId(1));
212+
let dscope = region::Scope {
213+
id: hir::ItemLocalId(1),
214+
data: region::ScopeData::Destruction
215+
};
213216
self.region_scope_tree.record_scope_parent(dscope, None);
214217
self.create_region_hierarchy(&RH {
215218
id: hir::ItemLocalId(1),
@@ -355,7 +358,10 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
355358
}
356359

357360
pub fn t_rptr_scope(&self, id: u32) -> Ty<'tcx> {
358-
let r = ty::ReScope(region::Scope::Node(hir::ItemLocalId(id)));
361+
let r = ty::ReScope(region::Scope {
362+
id: hir::ItemLocalId(id),
363+
data: region::ScopeData::Node
364+
});
359365
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(r), self.tcx().types.isize)
360366
}
361367

src/librustc_mir/build/cfg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'tcx> CFG<'tcx> {
5151
source_info: SourceInfo,
5252
region_scope: region::Scope) {
5353
if tcx.emit_end_regions() {
54-
if let region::ScopeData::CallSite = region_scope.data() {
54+
if let region::ScopeData::CallSite = region_scope.data {
5555
// The CallSite scope (aka the root scope) is sort of weird, in that it is
5656
// supposed to "separate" the "interior" and "exterior" of a closure. Being
5757
// that, it is not really a part of the region hierarchy, but for some

src/librustc_mir/build/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,14 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
550550
upvar_decls);
551551

552552
let fn_def_id = tcx.hir.local_def_id(fn_id);
553-
let call_site_scope = region::Scope::CallSite(body.value.hir_id.local_id);
554-
let arg_scope = region::Scope::Arguments(body.value.hir_id.local_id);
553+
let call_site_scope = region::Scope {
554+
id: body.value.hir_id.local_id,
555+
data: region::ScopeData::CallSite
556+
};
557+
let arg_scope = region::Scope {
558+
id: body.value.hir_id.local_id,
559+
data: region::ScopeData::Arguments
560+
};
555561
let mut block = START_BLOCK;
556562
let source_info = builder.source_info(span);
557563
let call_site_s = (call_site_scope, source_info);

0 commit comments

Comments
 (0)