Introduce sym::dummy and Ident::dummy.

The idea is to identify cases of symbols/identifiers that are not
expected to be used. There isn't a perfectly sharp line between "dummy"
and "not dummy", but I think it's useful nonetheless.
This commit is contained in:
Nicholas Nethercote 2025-03-04 17:10:55 +11:00
parent fe04460f6f
commit 0b2d7062c4
7 changed files with 29 additions and 8 deletions

View file

@ -33,6 +33,15 @@ symbols! {
// Special reserved identifiers used internally for elided lifetimes,
// unnamed method parameters, crate root module, error recovery etc.
// Matching predicates: `is_any_keyword`, `is_special`/`is_reserved`
//
// Notes about `kw::Empty`:
// - Its use can blur the lines between "empty symbol" and "no symbol".
// Using `Option<Symbol>` is preferable, where possible, because that
// is unambiguous.
// - For dummy symbols that are never used and absolutely must be
// present, it's better to use `sym::dummy` than `kw::Empty`, because
// it's clearer that it's intended as a dummy value, and more likely
// to be detected if it accidentally does get used.
Empty: "",
PathRoot: "{{root}}",
DollarCrate: "$crate",
@ -834,6 +843,7 @@ symbols! {
drop_types_in_const,
dropck_eyepatch,
dropck_parametricity,
dummy: "<!dummy!>", // use this instead of `kw::Empty` for symbols that won't be used
dummy_cgu_name,
dylib,
dyn_compatible_for_dispatch,
@ -2305,11 +2315,23 @@ impl Ident {
Ident::new(name, DUMMY_SP)
}
/// This is best avoided, because it blurs the lines between "empty
/// identifier" and "no identifier". Using `Option<Ident>` is preferable,
/// where possible, because that is unambiguous.
#[inline]
pub fn empty() -> Ident {
Ident::with_dummy_span(kw::Empty)
}
// For dummy identifiers that are never used and absolutely must be
// present, it's better to use `Ident::dummy` than `Ident::Empty`, because
// it's clearer that it's intended as a dummy value, and more likely to be
// detected if it accidentally does get used.
#[inline]
pub fn dummy() -> Ident {
Ident::with_dummy_span(sym::dummy)
}
/// Maps a string to an identifier with a dummy span.
pub fn from_str(string: &str) -> Ident {
Ident::with_dummy_span(Symbol::intern(string))