Skip to content

Commit b8dc2a7

Browse files
committed
Remove the lowering context's id caching system
1 parent ca88c44 commit b8dc2a7

File tree

1 file changed

+27
-131
lines changed

1 file changed

+27
-131
lines changed

src/librustc/hir/lowering.rs

+27-131
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@
2929
// are unique). Every new node must have a unique id. Avoid cloning HIR nodes.
3030
// If you do, you must then set the new node's id to a fresh one.
3131
//
32-
// Lowering must be reproducable (the compiler only lowers once, but tools and
33-
// custom lints may lower an AST node to a HIR node to interact with the
34-
// compiler). The most interesting bit of this is ids - if you lower an AST node
35-
// and create new HIR nodes with fresh ids, when re-lowering the same node, you
36-
// must ensure you get the same ids! To do this, we keep track of the next id
37-
// when we translate a node which requires new ids. By checking this cache and
38-
// using node ids starting with the cached id, we ensure ids are reproducible.
39-
// To use this system, you just need to hold on to a CachedIdSetter object
40-
// whilst lowering. This is an RAII object that takes care of setting and
41-
// restoring the cached id, etc.
42-
//
43-
// This whole system relies on node ids being incremented one at a time and
44-
// all increments being for lowering. This means that you should not call any
45-
// non-lowering function which will use new node ids.
46-
//
4732
// We must also cache gensym'ed Idents to ensure that we get the same Ident
4833
// every time we lower a node with gensym'ed names. One consequence of this is
4934
// that you can only gensym a name once in a lowering (you don't need to worry
@@ -67,7 +52,6 @@ use hir::map::definitions::DefPathData;
6752
use hir::def_id::DefIndex;
6853

6954
use std::collections::BTreeMap;
70-
use std::collections::HashMap;
7155
use std::iter;
7256
use syntax::ast::*;
7357
use syntax::attr::{ThinAttributes, ThinAttributesExt};
@@ -83,18 +67,8 @@ use std::cell::{Cell, RefCell};
8367

8468
pub struct LoweringContext<'a> {
8569
crate_root: Option<&'static str>,
86-
// Map AST ids to ids used for expanded nodes.
87-
id_cache: RefCell<HashMap<NodeId, NodeId>>,
88-
// Use if there are no cached ids for the current node.
70+
// Use to assign ids to hir nodes that do not directly correspond to an ast node
8971
id_assigner: &'a NodeIdAssigner,
90-
// 0 == no cached id. Must be incremented to align with previous id
91-
// incrementing.
92-
cached_id: Cell<u32>,
93-
// Keep track of gensym'ed idents.
94-
gensym_cache: RefCell<HashMap<(NodeId, &'static str), hir::Ident>>,
95-
// A copy of cached_id, but is also set to an id while a node is lowered for
96-
// the first time.
97-
gensym_key: Cell<u32>,
9872
// We must keep the set of definitions up to date as we add nodes that
9973
// weren't in the AST.
10074
definitions: Option<&'a RefCell<Definitions>>,
@@ -121,11 +95,7 @@ impl<'a, 'hir> LoweringContext<'a> {
12195

12296
LoweringContext {
12397
crate_root: crate_root,
124-
id_cache: RefCell::new(HashMap::new()),
12598
id_assigner: id_assigner,
126-
cached_id: Cell::new(0),
127-
gensym_cache: RefCell::new(HashMap::new()),
128-
gensym_key: Cell::new(0),
12999
definitions: Some(defs),
130100
parent_def: Cell::new(None),
131101
}
@@ -136,40 +106,18 @@ impl<'a, 'hir> LoweringContext<'a> {
136106
pub fn testing_context(id_assigner: &'a NodeIdAssigner) -> LoweringContext<'a> {
137107
LoweringContext {
138108
crate_root: None,
139-
id_cache: RefCell::new(HashMap::new()),
140109
id_assigner: id_assigner,
141-
cached_id: Cell::new(0),
142-
gensym_cache: RefCell::new(HashMap::new()),
143-
gensym_key: Cell::new(0),
144110
definitions: None,
145111
parent_def: Cell::new(None),
146112
}
147113
}
148114

149115
fn next_id(&self) -> NodeId {
150-
let cached_id = self.cached_id.get();
151-
if cached_id == 0 {
152-
return self.id_assigner.next_node_id();
153-
}
154-
155-
self.cached_id.set(cached_id + 1);
156-
cached_id
116+
self.id_assigner.next_node_id()
157117
}
158118

159119
fn str_to_ident(&self, s: &'static str) -> hir::Ident {
160-
let gensym_key = self.gensym_key.get();
161-
if gensym_key == 0 {
162-
return hir::Ident::from_name(token::gensym(s));
163-
}
164-
165-
let cached = self.gensym_cache.borrow().contains_key(&(gensym_key, s));
166-
if cached {
167-
self.gensym_cache.borrow()[&(gensym_key, s)]
168-
} else {
169-
let result = hir::Ident::from_name(token::gensym(s));
170-
self.gensym_cache.borrow_mut().insert((gensym_key, s), result);
171-
result
172-
}
120+
hir::Ident::from_name(token::gensym(s))
173121
}
174122

175123
// Panics if this LoweringContext's NodeIdAssigner is not able to emit diagnostics.
@@ -197,56 +145,6 @@ impl<'a, 'hir> LoweringContext<'a> {
197145
}
198146
}
199147

200-
// Utility fn for setting and unsetting the cached id.
201-
fn cache_ids<'a, OP, R>(lctx: &LoweringContext, expr_id: NodeId, op: OP) -> R
202-
where OP: FnOnce(&LoweringContext) -> R
203-
{
204-
// Only reset the id if it was previously 0, i.e., was not cached.
205-
// If it was cached, we are in a nested node, but our id count will
206-
// still count towards the parent's count.
207-
let reset_cached_id = lctx.cached_id.get() == 0;
208-
// We always reset gensym_key so that if we use the same name in a nested
209-
// node and after that node, they get different values.
210-
let old_gensym_key = lctx.gensym_key.get();
211-
212-
{
213-
let id_cache: &mut HashMap<_, _> = &mut lctx.id_cache.borrow_mut();
214-
215-
if id_cache.contains_key(&expr_id) {
216-
panic!("relowering!!!");
217-
/*
218-
let cached_id = lctx.cached_id.get();
219-
if cached_id == 0 {
220-
// We're entering a node where we need to track ids, but are not
221-
// yet tracking.
222-
lctx.cached_id.set(id_cache[&expr_id]);
223-
} else {
224-
// We're already tracking - check that the tracked id is the same
225-
// as the expected id.
226-
assert!(cached_id == id_cache[&expr_id], "id mismatch");
227-
}
228-
lctx.gensym_key.set(id_cache[&expr_id]);
229-
*/
230-
} else {
231-
// We've never lowered this node before, remember it for next time.
232-
let next_id = lctx.id_assigner.peek_node_id();
233-
id_cache.insert(expr_id, next_id);
234-
lctx.gensym_key.set(next_id);
235-
// self.cached_id is not set when we lower a node for the first time,
236-
// only on re-lowering.
237-
}
238-
}
239-
240-
let result = op(lctx);
241-
242-
if reset_cached_id {
243-
lctx.cached_id.set(0);
244-
}
245-
lctx.gensym_key.set(old_gensym_key);
246-
247-
result
248-
}
249-
250148
pub fn lower_ident(_lctx: &LoweringContext, ident: Ident) -> hir::Ident {
251149
hir::Ident {
252150
name: mtwt::resolve(ident),
@@ -1080,7 +978,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
1080978
// std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR ));
1081979
// InPlace::finalize(place)
1082980
// })
1083-
return cache_ids(lctx, e.id, |lctx| {
981+
return {
1084982
let placer_expr = lower_expr(lctx, placer);
1085983
let value_expr = lower_expr(lctx, value_expr);
1086984

@@ -1175,7 +1073,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
11751073
e.span,
11761074
hir::PushUnstableBlock,
11771075
e.attrs.clone())
1178-
});
1076+
}
11791077
}
11801078

11811079
ExprKind::Vec(ref exprs) => {
@@ -1229,20 +1127,18 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
12291127
let else_opt = else_opt.as_ref().map(|els| {
12301128
match els.node {
12311129
ExprKind::IfLet(..) => {
1232-
cache_ids(lctx, e.id, |lctx| {
1233-
// wrap the if-let expr in a block
1234-
let span = els.span;
1235-
let els = lower_expr(lctx, els);
1236-
let id = lctx.next_id();
1237-
let blk = P(hir::Block {
1238-
stmts: hir_vec![],
1239-
expr: Some(els),
1240-
id: id,
1241-
rules: hir::DefaultBlock,
1242-
span: span,
1243-
});
1244-
expr_block(lctx, blk, None)
1245-
})
1130+
// wrap the if-let expr in a block
1131+
let span = els.span;
1132+
let els = lower_expr(lctx, els);
1133+
let id = lctx.next_id();
1134+
let blk = P(hir::Block {
1135+
stmts: hir_vec![],
1136+
expr: Some(els),
1137+
id: id,
1138+
rules: hir::DefaultBlock,
1139+
span: span,
1140+
});
1141+
expr_block(lctx, blk, None)
12461142
}
12471143
_ => lower_expr(lctx, els),
12481144
}
@@ -1331,7 +1227,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
13311227
None)
13321228
}
13331229

1334-
return cache_ids(lctx, e.id, |lctx| {
1230+
return {
13351231
use syntax::ast::RangeLimits::*;
13361232

13371233
match (e1, e2, lims) {
@@ -1362,7 +1258,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
13621258
_ => panic!(lctx.diagnostic().span_fatal(e.span,
13631259
"inclusive range with no end"))
13641260
}
1365-
});
1261+
}
13661262
}
13671263
ExprKind::Path(ref qself, ref path) => {
13681264
let hir_qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
@@ -1436,7 +1332,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
14361332
// _ => [<else_opt> | ()]
14371333
// }
14381334

1439-
return cache_ids(lctx, e.id, |lctx| {
1335+
return {
14401336
// `<pat> => <body>`
14411337
let pat_arm = {
14421338
let body = lower_block(lctx, body);
@@ -1510,7 +1406,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
15101406
contains_else_clause: contains_else_clause,
15111407
}),
15121408
e.attrs.clone())
1513-
});
1409+
}
15141410
}
15151411

15161412
// Desugar ExprWhileLet
@@ -1525,7 +1421,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
15251421
// }
15261422
// }
15271423

1528-
return cache_ids(lctx, e.id, |lctx| {
1424+
return {
15291425
// `<pat> => <body>`
15301426
let pat_arm = {
15311427
let body = lower_block(lctx, body);
@@ -1556,7 +1452,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
15561452
opt_ident.map(|ident| lower_ident(lctx, ident)));
15571453
// add attributes to the outer returned expr node
15581454
expr(lctx, e.span, loop_expr, e.attrs.clone())
1559-
});
1455+
}
15601456
}
15611457

15621458
// Desugar ExprForLoop
@@ -1578,7 +1474,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
15781474
// result
15791475
// }
15801476

1581-
return cache_ids(lctx, e.id, |lctx| {
1477+
return {
15821478
// expand <head>
15831479
let head = lower_expr(lctx, head);
15841480

@@ -1677,7 +1573,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
16771573
let block = block_all(lctx, e.span, hir_vec![let_stmt], Some(result));
16781574
// add the attributes to the outer returned expr node
16791575
expr_block(lctx, block, e.attrs.clone())
1680-
});
1576+
}
16811577
}
16821578

16831579
// Desugar ExprKind::Try
@@ -1694,7 +1590,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
16941590
// }
16951591
// }
16961592

1697-
return cache_ids(lctx, e.id, |lctx| {
1593+
return {
16981594
// expand <expr>
16991595
let sub_expr = lower_expr(lctx, sub_expr);
17001596

@@ -1735,7 +1631,7 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
17351631

17361632
expr_match(lctx, e.span, sub_expr, hir_vec![err_arm, ok_arm],
17371633
hir::MatchSource::TryDesugar, None)
1738-
})
1634+
}
17391635
}
17401636

17411637
ExprKind::Mac(_) => panic!("Shouldn't exist here"),

0 commit comments

Comments
 (0)