Skip to content

Commit c3a1c31

Browse files
committed
Don't use kw::Empty as the dollar_crate_name placeholder.
Using in-band values for exceptional cases is error prone. This commit changes it to an `Option`.
1 parent 08c347a commit c3a1c31

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

compiler/rustc_span/src/hygiene.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub struct SyntaxContextData {
7171
/// This context, but with all transparent expansions filtered away.
7272
opaque_and_semitransparent: SyntaxContext,
7373
/// Name of the crate to which `$crate` with this context would resolve.
74-
dollar_crate_name: Symbol,
74+
dollar_crate_name: Option<Symbol>,
7575
}
7676

7777
impl SyntaxContextData {
@@ -82,16 +82,16 @@ impl SyntaxContextData {
8282
parent: SyntaxContext::root(),
8383
opaque: SyntaxContext::root(),
8484
opaque_and_semitransparent: SyntaxContext::root(),
85-
dollar_crate_name: kw::DollarCrate,
85+
dollar_crate_name: Some(kw::DollarCrate),
8686
}
8787
}
8888

8989
fn decode_placeholder() -> SyntaxContextData {
90-
SyntaxContextData { dollar_crate_name: kw::Empty, ..SyntaxContextData::root() }
90+
SyntaxContextData { dollar_crate_name: None, ..SyntaxContextData::root() }
9191
}
9292

9393
fn is_decode_placeholder(&self) -> bool {
94-
self.dollar_crate_name == kw::Empty
94+
self.dollar_crate_name.is_none()
9595
}
9696

9797
fn key(&self) -> SyntaxContextKey {
@@ -597,7 +597,7 @@ impl HygieneData {
597597
parent,
598598
opaque: new_opaque,
599599
opaque_and_semitransparent: new_opaque,
600-
dollar_crate_name: kw::DollarCrate,
600+
dollar_crate_name: Some(kw::DollarCrate),
601601
});
602602
new_opaque
603603
});
@@ -617,7 +617,7 @@ impl HygieneData {
617617
parent,
618618
opaque,
619619
opaque_and_semitransparent: new_opaque_and_semitransparent,
620-
dollar_crate_name: kw::DollarCrate,
620+
dollar_crate_name: Some(kw::DollarCrate),
621621
});
622622
new_opaque_and_semitransparent
623623
});
@@ -631,7 +631,7 @@ impl HygieneData {
631631
parent,
632632
opaque,
633633
opaque_and_semitransparent,
634-
dollar_crate_name: kw::DollarCrate,
634+
dollar_crate_name: Some(kw::DollarCrate),
635635
});
636636
SyntaxContext::from_usize(syntax_context_data.len() - 1)
637637
})
@@ -657,7 +657,7 @@ pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symb
657657
let mut to_update = vec![];
658658
HygieneData::with(|data| {
659659
for (idx, scdata) in data.syntax_context_data.iter().enumerate().rev() {
660-
if scdata.dollar_crate_name == kw::DollarCrate {
660+
if scdata.dollar_crate_name == Some(kw::DollarCrate) {
661661
to_update.push((idx, kw::DollarCrate));
662662
} else if !scdata.is_decode_placeholder() {
663663
break;
@@ -671,7 +671,7 @@ pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symb
671671
}
672672
HygieneData::with(|data| {
673673
for (idx, name) in to_update {
674-
data.syntax_context_data[idx].dollar_crate_name = name;
674+
data.syntax_context_data[idx].dollar_crate_name = Some(name);
675675
}
676676
})
677677
}
@@ -927,7 +927,7 @@ impl SyntaxContext {
927927
pub(crate) fn dollar_crate_name(self) -> Symbol {
928928
HygieneData::with(|data| {
929929
debug_assert!(!data.syntax_context_data[self.0 as usize].is_decode_placeholder());
930-
data.syntax_context_data[self.0 as usize].dollar_crate_name
930+
data.syntax_context_data[self.0 as usize].dollar_crate_name.unwrap()
931931
})
932932
}
933933

@@ -1478,10 +1478,10 @@ pub fn decode_syntax_context<D: Decoder, F: FnOnce(&mut D, u32) -> SyntaxContext
14781478
let ctxt_data_ref =
14791479
&mut hygiene_data.syntax_context_data[pending_ctxt.as_u32() as usize];
14801480
let prev_ctxt_data = mem::replace(ctxt_data_ref, ctxt_data);
1481-
// Reset `dollar_crate_name` so that it will be updated by `update_dollar_crate_names`.
1481+
// Reset `dollar_crate_name` so it will be updated by `update_dollar_crate_names`.
14821482
// We don't care what the encoding crate set this to - we want to resolve it
14831483
// from the perspective of the current compilation session.
1484-
ctxt_data_ref.dollar_crate_name = kw::DollarCrate;
1484+
ctxt_data_ref.dollar_crate_name = Some(kw::DollarCrate);
14851485
// Make sure nothing weird happened while `decode_data` was running.
14861486
if !prev_ctxt_data.is_decode_placeholder() {
14871487
// Another thread may have already inserted the decoded data,

0 commit comments

Comments
 (0)