Skip to content

Commit d31dd80

Browse files
committed
Add for_stmt
1 parent 97ab6d8 commit d31dd80

File tree

8 files changed

+75
-52
lines changed

8 files changed

+75
-52
lines changed

compiler/rustc_middle/src/ich/impls_hir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::ItemLocalId {
123123
}
124124
}
125125

126+
impl<'a> ToStableHashKey<StableHashingContext<'a>> for (hir::ItemLocalId, bool) {
127+
type KeyType = (hir::ItemLocalId, bool);
128+
129+
#[inline]
130+
fn to_stable_hash_key(&self, _: &StableHashingContext<'a>) -> (hir::ItemLocalId, bool) {
131+
*self
132+
}
133+
}
134+
126135
impl<'a> HashStable<StableHashingContext<'a>> for hir::Body<'_> {
127136
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
128137
let hir::Body { params, value, generator_kind } = self;

compiler/rustc_middle/src/middle/region.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,23 @@ use std::fmt;
8484
#[derive(HashStable)]
8585
pub struct Scope {
8686
pub id: hir::ItemLocalId,
87+
pub for_stmt: bool,
8788
pub data: ScopeData,
8889
}
8990

9091
impl fmt::Debug for Scope {
9192
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
9293
match self.data {
93-
ScopeData::Node => write!(fmt, "Node({:?})", self.id),
94-
ScopeData::CallSite => write!(fmt, "CallSite({:?})", self.id),
95-
ScopeData::Arguments => write!(fmt, "Arguments({:?})", self.id),
96-
ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.id),
94+
ScopeData::Node => write!(fmt, "Node({:?}, {:?})", self.id, self.for_stmt),
95+
ScopeData::CallSite => write!(fmt, "CallSite({:?}. {:?})", self.id, self.for_stmt),
96+
ScopeData::Arguments => write!(fmt, "Arguments({:?}, {:?})", self.id, self.for_stmt),
97+
ScopeData::Destruction => write!(fmt, "Destruction({:?}, {:?})", self.id, self.for_stmt),
9798
ScopeData::Remainder(fsi) => write!(
9899
fmt,
99-
"Remainder {{ block: {:?}, first_statement_index: {}}}",
100+
"Remainder {{ block: {:?}, first_statement_index: {}, for_stmt: {:?}}}",
100101
self.id,
101102
fsi.as_u32(),
103+
self.for_stmt,
102104
),
103105
}
104106
}
@@ -223,7 +225,7 @@ pub struct ScopeTree {
223225
var_map: FxHashMap<hir::ItemLocalId, Scope>,
224226

225227
/// Maps from a `NodeId` to the associated destruction scope (if any).
226-
destruction_scopes: FxHashMap<hir::ItemLocalId, Scope>,
228+
destruction_scopes: FxHashMap<(hir::ItemLocalId, bool), Scope>,
227229

228230
/// `rvalue_scopes` includes entries for those expressions whose
229231
/// cleanup scope is larger than the default. The map goes from the
@@ -331,17 +333,17 @@ impl ScopeTree {
331333

332334
if let Some(p) = parent {
333335
let prev = self.parent_map.insert(child, p);
334-
assert!(prev.is_none());
336+
assert!(prev.is_none(), "Scope {:?} with parent {:?} has prev {:?}", child, p, prev);
335337
}
336338

337339
// Record the destruction scopes for later so we can query them.
338340
if let ScopeData::Destruction = child.data {
339-
self.destruction_scopes.insert(child.item_local_id(), child);
341+
assert_eq!(self.destruction_scopes.insert((child.item_local_id(), child.for_stmt), child), None);
340342
}
341343
}
342344

343-
pub fn opt_destruction_scope(&self, n: hir::ItemLocalId) -> Option<Scope> {
344-
self.destruction_scopes.get(&n).cloned()
345+
pub fn opt_destruction_scope(&self, n: hir::ItemLocalId, for_stmt: bool) -> Option<Scope> {
346+
self.destruction_scopes.get(&(n, for_stmt)).cloned()
345347
}
346348

347349
pub fn record_var_scope(&mut self, var: hir::ItemLocalId, lifetime: Scope) {
@@ -372,7 +374,7 @@ impl ScopeTree {
372374
}
373375

374376
/// Returns the scope when the temp created by `expr_id` will be cleaned up.
375-
pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> Option<Scope> {
377+
pub fn temporary_scope(&self, expr_id: hir::ItemLocalId, for_stmt: bool) -> Option<Scope> {
376378
// Check for a designated rvalue scope.
377379
if let Some(&s) = self.rvalue_scopes.get(&expr_id) {
378380
debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s);
@@ -383,7 +385,7 @@ impl ScopeTree {
383385
// if there's one. Static items, for instance, won't
384386
// have an enclosing scope, hence no scope will be
385387
// returned.
386-
let mut id = Scope { id: expr_id, data: ScopeData::Node };
388+
let mut id = Scope { id: expr_id, data: ScopeData::Node, for_stmt };
387389

388390
while let Some(&(p, _)) = self.parent_map.get(&id) {
389391
match p.data {

compiler/rustc_middle/src/thir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub enum StmtKind<'tcx> {
189189

190190
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
191191
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
192-
rustc_data_structures::static_assert_size!(Expr<'_>, 104);
192+
rustc_data_structures::static_assert_size!(Expr<'_>, 112);
193193

194194
/// A THIR expression.
195195
#[derive(Debug, HashStable)]

compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,9 +625,9 @@ where
625625
);
626626

627627
let call_site_scope =
628-
region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::CallSite };
628+
region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::CallSite, for_stmt: false };
629629
let arg_scope =
630-
region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::Arguments };
630+
region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::Arguments, for_stmt: false };
631631
let source_info = builder.source_info(span);
632632
let call_site_s = (call_site_scope, source_info);
633633
unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {

compiler/rustc_mir_build/src/thir/cx/block.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ impl<'tcx> Cx<'tcx> {
1313
// in order to get the lexical scoping correctly.
1414
let stmts = self.mirror_stmts(block.hir_id.local_id, block.stmts);
1515
let opt_destruction_scope =
16-
self.region_scope_tree.opt_destruction_scope(block.hir_id.local_id);
16+
self.region_scope_tree.opt_destruction_scope(block.hir_id.local_id, false);
1717
Block {
1818
targeted_by_break: block.targeted_by_break,
1919
region_scope: region::Scope {
2020
id: block.hir_id.local_id,
2121
data: region::ScopeData::Node,
22+
for_stmt: false,
2223
},
2324
opt_destruction_scope,
2425
span: block.span,
@@ -46,14 +47,15 @@ impl<'tcx> Cx<'tcx> {
4647
.enumerate()
4748
.filter_map(|(index, stmt)| {
4849
let hir_id = stmt.kind.hir_id();
49-
let opt_dxn_ext = self.region_scope_tree.opt_destruction_scope(hir_id.local_id);
50+
let opt_dxn_ext = self.region_scope_tree.opt_destruction_scope(hir_id.local_id, true);
5051
match stmt.kind {
5152
hir::StmtKind::Expr(ref expr) | hir::StmtKind::Semi(ref expr) => {
5253
let stmt = Stmt {
5354
kind: StmtKind::Expr {
5455
scope: region::Scope {
5556
id: hir_id.local_id,
5657
data: region::ScopeData::Node,
58+
for_stmt: true,
5759
},
5860
expr: self.mirror_expr(expr),
5961
},
@@ -71,6 +73,7 @@ impl<'tcx> Cx<'tcx> {
7173
data: region::ScopeData::Remainder(region::FirstStatementIndex::new(
7274
index,
7375
)),
76+
for_stmt: false,
7477
};
7578

7679
let mut pattern = self.pattern_from_hir(local.pat);
@@ -101,6 +104,7 @@ impl<'tcx> Cx<'tcx> {
101104
init_scope: region::Scope {
102105
id: hir_id.local_id,
103106
data: region::ScopeData::Node,
107+
for_stmt: true,
104108
},
105109
pattern,
106110
initializer: local.init.map(|init| self.mirror_expr(init)),

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ impl<'tcx> Cx<'tcx> {
3131
}
3232

3333
pub(super) fn mirror_expr_inner(&mut self, hir_expr: &'tcx hir::Expr<'tcx>) -> ExprId {
34-
let temp_lifetime = self.region_scope_tree.temporary_scope(hir_expr.hir_id.local_id);
34+
let temp_lifetime = self.region_scope_tree.temporary_scope(hir_expr.hir_id.local_id, false);
3535
let expr_scope =
36-
region::Scope { id: hir_expr.hir_id.local_id, data: region::ScopeData::Node };
36+
region::Scope { id: hir_expr.hir_id.local_id, data: region::ScopeData::Node, for_stmt: false };
3737

3838
debug!("Expr::make_mirror(): id={}, span={:?}", hir_expr.hir_id, hir_expr.span);
3939

@@ -59,7 +59,7 @@ impl<'tcx> Cx<'tcx> {
5959

6060
// Finally, create a destruction scope, if any.
6161
if let Some(region_scope) =
62-
self.region_scope_tree.opt_destruction_scope(hir_expr.hir_id.local_id)
62+
self.region_scope_tree.opt_destruction_scope(hir_expr.hir_id.local_id, false)
6363
{
6464
expr = Expr {
6565
temp_lifetime,
@@ -150,7 +150,7 @@ impl<'tcx> Cx<'tcx> {
150150

151151
fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
152152
let expr_ty = self.typeck_results().expr_ty(expr);
153-
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
153+
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id, false);
154154

155155
let kind = match expr.kind {
156156
// Here comes the interesting stuff:
@@ -499,7 +499,7 @@ impl<'tcx> Cx<'tcx> {
499499
),
500500
};
501501
let temp_lifetime =
502-
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
502+
self.region_scope_tree.temporary_scope(expr.hir_id.local_id, false);
503503
let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
504504
let ty;
505505
match res {
@@ -579,14 +579,14 @@ impl<'tcx> Cx<'tcx> {
579579
}
580580
hir::ExprKind::Break(dest, ref value) => match dest.target_id {
581581
Ok(target_id) => ExprKind::Break {
582-
label: region::Scope { id: target_id.local_id, data: region::ScopeData::Node },
582+
label: region::Scope { id: target_id.local_id, data: region::ScopeData::Node, for_stmt: false, },
583583
value: value.as_ref().map(|value| self.mirror_expr(value)),
584584
},
585585
Err(err) => bug!("invalid loop id for break: {}", err),
586586
},
587587
hir::ExprKind::Continue(dest) => match dest.target_id {
588588
Ok(loop_id) => ExprKind::Continue {
589-
label: region::Scope { id: loop_id.local_id, data: region::ScopeData::Node },
589+
label: region::Scope { id: loop_id.local_id, data: region::ScopeData::Node, for_stmt: false, },
590590
},
591591
Err(err) => bug!("invalid loop id for continue: {}", err),
592592
},
@@ -601,7 +601,7 @@ impl<'tcx> Cx<'tcx> {
601601
},
602602
hir::ExprKind::Loop(ref body, ..) => {
603603
let block_ty = self.typeck_results().node_type(body.hir_id);
604-
let temp_lifetime = self.region_scope_tree.temporary_scope(body.hir_id.local_id);
604+
let temp_lifetime = self.region_scope_tree.temporary_scope(body.hir_id.local_id, false);
605605
let block = self.mirror_block(body);
606606
let body = self.thir.exprs.push(Expr {
607607
ty: block_ty,
@@ -800,7 +800,7 @@ impl<'tcx> Cx<'tcx> {
800800
span: Span,
801801
overloaded_callee: Option<(DefId, SubstsRef<'tcx>)>,
802802
) -> Expr<'tcx> {
803-
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
803+
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id, false);
804804
let (def_id, substs, user_ty) = match overloaded_callee {
805805
Some((def_id, substs)) => (def_id, substs, None),
806806
None => {
@@ -837,7 +837,7 @@ impl<'tcx> Cx<'tcx> {
837837
}),
838838
body: self.mirror_expr(arm.body),
839839
lint_level: LintLevel::Explicit(arm.hir_id),
840-
scope: region::Scope { id: arm.hir_id.local_id, data: region::ScopeData::Node },
840+
scope: region::Scope { id: arm.hir_id.local_id, data: region::ScopeData::Node, for_stmt: false },
841841
span: arm.span,
842842
};
843843
self.thir.arms.push(arm)
@@ -922,7 +922,7 @@ impl<'tcx> Cx<'tcx> {
922922
// a constant reference (or constant raw pointer for `static mut`) in MIR
923923
Res::Def(DefKind::Static, id) => {
924924
let ty = self.tcx.static_ptr_ty(id);
925-
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
925+
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id, false);
926926
let kind = if self.tcx.is_thread_local_static(id) {
927927
ExprKind::ThreadLocalRef(id)
928928
} else {
@@ -1006,7 +1006,7 @@ impl<'tcx> Cx<'tcx> {
10061006

10071007
// construct the complete expression `foo()` for the overloaded call,
10081008
// which will yield the &T type
1009-
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
1009+
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id, false);
10101010
let fun = self.method_callee(expr, span, overloaded_callee);
10111011
let fun = self.thir.exprs.push(fun);
10121012
let fun_ty = self.thir[fun].ty;
@@ -1026,7 +1026,7 @@ impl<'tcx> Cx<'tcx> {
10261026
closure_expr: &'tcx hir::Expr<'tcx>,
10271027
place: HirPlace<'tcx>,
10281028
) -> Expr<'tcx> {
1029-
let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
1029+
let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id, false);
10301030
let var_ty = place.base_ty;
10311031

10321032
// The result of capture analysis in `rustc_typeck/check/upvar.rs`represents a captured path
@@ -1081,7 +1081,7 @@ impl<'tcx> Cx<'tcx> {
10811081
let upvar_capture = captured_place.info.capture_kind;
10821082
let captured_place_expr =
10831083
self.convert_captured_hir_place(closure_expr, captured_place.place.clone());
1084-
let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
1084+
let temp_lifetime = self.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id, false);
10851085

10861086
match upvar_capture {
10871087
ty::UpvarCapture::ByValue(_) => captured_place_expr,

0 commit comments

Comments
 (0)