Skip to content

Commit b35ceee

Browse files
committed
Simplify Expn creation.
1 parent dddaa6d commit b35ceee

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

compiler/rustc_span/src/hygiene.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,25 @@ impl LocalExpnId {
153153
}
154154

155155
pub fn fresh_empty() -> LocalExpnId {
156-
HygieneData::with(|data| data.fresh_expn(None))
156+
HygieneData::with(|data| {
157+
let expn_id = data.local_expn_data.push(None);
158+
let _eid = data.local_expn_hashes.push(ExpnHash(Fingerprint::ZERO));
159+
debug_assert_eq!(expn_id, _eid);
160+
expn_id
161+
})
157162
}
158163

159-
pub fn fresh(expn_data: ExpnData, ctx: impl HashStableContext) -> LocalExpnId {
164+
pub fn fresh(mut expn_data: ExpnData, ctx: impl HashStableContext) -> LocalExpnId {
160165
debug_assert_eq!(expn_data.parent.krate, LOCAL_CRATE);
161-
let expn_id = HygieneData::with(|data| data.fresh_expn(Some(expn_data)));
162-
update_disambiguator(expn_id, ctx);
163-
expn_id
166+
let expn_hash = update_disambiguator(&mut expn_data, ctx);
167+
HygieneData::with(|data| {
168+
let expn_id = data.local_expn_data.push(Some(expn_data));
169+
let _eid = data.local_expn_hashes.push(expn_hash);
170+
debug_assert_eq!(expn_id, _eid);
171+
let _old_id = data.expn_hash_to_expn_id.insert(expn_hash, expn_id.to_expn_id());
172+
debug_assert!(_old_id.is_none());
173+
expn_id
174+
})
164175
}
165176

166177
#[inline]
@@ -179,14 +190,18 @@ impl LocalExpnId {
179190
}
180191

181192
#[inline]
182-
pub fn set_expn_data(self, expn_data: ExpnData, ctx: impl HashStableContext) {
193+
pub fn set_expn_data(self, mut expn_data: ExpnData, ctx: impl HashStableContext) {
183194
debug_assert_eq!(expn_data.parent.krate, LOCAL_CRATE);
195+
let expn_hash = update_disambiguator(&mut expn_data, ctx);
184196
HygieneData::with(|data| {
185197
let old_expn_data = &mut data.local_expn_data[self];
186198
assert!(old_expn_data.is_none(), "expansion data is reset for an expansion ID");
187199
*old_expn_data = Some(expn_data);
200+
debug_assert_eq!(data.local_expn_hashes[self].0, Fingerprint::ZERO);
201+
data.local_expn_hashes[self] = expn_hash;
202+
let _old_id = data.expn_hash_to_expn_id.insert(expn_hash, self.to_expn_id());
203+
debug_assert!(_old_id.is_none());
188204
});
189-
update_disambiguator(self, ctx)
190205
}
191206

192207
#[inline]
@@ -335,13 +350,6 @@ impl HygieneData {
335350
with_session_globals(|session_globals| f(&mut *session_globals.hygiene_data.borrow_mut()))
336351
}
337352

338-
fn fresh_expn(&mut self, expn_data: Option<ExpnData>) -> LocalExpnId {
339-
let expn_id = self.local_expn_data.push(expn_data);
340-
let _eid = self.local_expn_hashes.push(ExpnHash(Fingerprint::ZERO));
341-
debug_assert_eq!(expn_id, _eid);
342-
expn_id
343-
}
344-
345353
#[inline]
346354
fn local_expn_hash(&self, expn_id: LocalExpnId) -> ExpnHash {
347355
self.local_expn_hashes[expn_id]
@@ -1413,8 +1421,7 @@ impl<D: Decoder> Decodable<D> for SyntaxContext {
14131421
/// `set_expn_data`). It is *not* called for foreign `ExpnId`s deserialized
14141422
/// from another crate's metadata - since `ExpnHash` includes the stable crate id,
14151423
/// collisions are only possible between `ExpnId`s within the same crate.
1416-
fn update_disambiguator(expn_id: LocalExpnId, mut ctx: impl HashStableContext) {
1417-
let mut expn_data = expn_id.expn_data();
1424+
fn update_disambiguator(expn_data: &mut ExpnData, mut ctx: impl HashStableContext) -> ExpnHash {
14181425
// This disambiguator should not have been set yet.
14191426
assert_eq!(
14201427
expn_data.disambiguator, 0,
@@ -1433,8 +1440,7 @@ fn update_disambiguator(expn_id: LocalExpnId, mut ctx: impl HashStableContext) {
14331440
});
14341441

14351442
if disambiguator != 0 {
1436-
debug!("Set disambiguator for {:?} (hash {:?})", expn_id, expn_hash);
1437-
debug!("expn_data = {:?}", expn_data);
1443+
debug!("Set disambiguator for expn_data={:?} expn_hash={:?}", expn_data, expn_hash);
14381444

14391445
expn_data.disambiguator = disambiguator;
14401446
expn_hash = expn_data.hash_expn(&mut ctx);
@@ -1450,15 +1456,7 @@ fn update_disambiguator(expn_id: LocalExpnId, mut ctx: impl HashStableContext) {
14501456
});
14511457
}
14521458

1453-
let expn_hash =
1454-
ExpnHash::new(ctx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(), expn_hash);
1455-
HygieneData::with(|data| {
1456-
data.local_expn_data[expn_id].as_mut().unwrap().disambiguator = disambiguator;
1457-
debug_assert_eq!(data.local_expn_hashes[expn_id].0, Fingerprint::ZERO);
1458-
data.local_expn_hashes[expn_id] = expn_hash;
1459-
let _old_id = data.expn_hash_to_expn_id.insert(expn_hash, expn_id.to_expn_id());
1460-
debug_assert!(_old_id.is_none());
1461-
});
1459+
ExpnHash::new(ctx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(), expn_hash)
14621460
}
14631461

14641462
impl<CTX: HashStableContext> HashStable<CTX> for SyntaxContext {

0 commit comments

Comments
 (0)