Skip to content

Commit 94967f2

Browse files
committed
Remove gensyms
1 parent 1a2597b commit 94967f2

File tree

2 files changed

+9
-88
lines changed

2 files changed

+9
-88
lines changed

src/libsyntax_pos/symbol.rs

+9-81
Original file line numberDiff line numberDiff line change
@@ -807,25 +807,13 @@ impl Ident {
807807
Ident::new(self.name, self.span.modern_and_legacy())
808808
}
809809

810-
/// Transforms an underscore identifier into one with the same name, but
811-
/// gensymed. Leaves non-underscore identifiers unchanged.
812-
pub fn gensym_if_underscore(self) -> Ident {
813-
if self.name == kw::Underscore {
814-
let name = with_interner(|interner| interner.gensymed(self.name));
815-
Ident::new(name, self.span)
816-
} else {
817-
self
818-
}
819-
}
820-
821810
/// Convert the name to a `LocalInternedString`. This is a slowish
822811
/// operation because it requires locking the symbol interner.
823812
pub fn as_str(self) -> LocalInternedString {
824813
self.name.as_str()
825814
}
826815

827-
/// Convert the name to an `InternedString`. This is a slowish operation
828-
/// because it requires locking the symbol interner.
816+
/// Convert the name to an `InternedString`.
829817
pub fn as_interned_str(self) -> InternedString {
830818
self.name.as_interned_str()
831819
}
@@ -880,26 +868,9 @@ impl UseSpecializedDecodable for Ident {
880868
}
881869
}
882870

883-
/// A symbol is an interned or gensymed string. A gensym is a symbol that is
884-
/// never equal to any other symbol.
871+
/// An interned string.
885872
///
886-
/// Conceptually, a gensym can be thought of as a normal symbol with an
887-
/// invisible unique suffix. Gensyms are useful when creating new identifiers
888-
/// that must not match any existing identifiers, e.g. during macro expansion
889-
/// and syntax desugaring. Because gensyms should always be identifiers, all
890-
/// gensym operations are on `Ident` rather than `Symbol`. (Indeed, in the
891-
/// future the gensym-ness may be moved from `Symbol` to hygiene data.)
892-
///
893-
/// Examples:
894-
/// ```
895-
/// assert_eq!(Ident::from_str("_"), Ident::from_str("_"))
896-
/// assert_ne!(Ident::from_str("_").gensym_if_underscore(), Ident::from_str("_"))
897-
/// assert_ne!(
898-
/// Ident::from_str("_").gensym_if_underscore(),
899-
/// Ident::from_str("_").gensym_if_underscore(),
900-
/// )
901-
/// ```
902-
/// Internally, a symbol is implemented as an index, and all operations
873+
/// Internally, a `Symbol` is implemented as an index, and all operations
903874
/// (including hashing, equality, and ordering) operate on that index. The use
904875
/// of `rustc_index::newtype_index!` means that `Option<Symbol>` only takes up 4 bytes,
905876
/// because `rustc_index::newtype_index!` reserves the last 256 values for tagging purposes.
@@ -950,12 +921,9 @@ impl Symbol {
950921
})
951922
}
952923

953-
/// Convert to an `InternedString`. This is a slowish operation because it
954-
/// requires locking the symbol interner.
924+
/// Convert to an `InternedString`.
955925
pub fn as_interned_str(self) -> InternedString {
956-
with_interner(|interner| InternedString {
957-
symbol: interner.interned(self)
958-
})
926+
InternedString { symbol: self }
959927
}
960928

961929
pub fn as_u32(self) -> u32 {
@@ -965,12 +933,7 @@ impl Symbol {
965933

966934
impl fmt::Debug for Symbol {
967935
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
968-
let is_gensymed = with_interner(|interner| interner.is_gensymed(*self));
969-
if is_gensymed {
970-
write!(f, "{}({:?})", self, self.0)
971-
} else {
972-
write!(f, "{}", self)
973-
}
936+
fmt::Display::fmt(self, f)
974937
}
975938
}
976939

@@ -993,15 +956,11 @@ impl Decodable for Symbol {
993956
}
994957

995958
// The `&'static str`s in this type actually point into the arena.
996-
//
997-
// Note that normal symbols are indexed upward from 0, and gensyms are indexed
998-
// downward from SymbolIndex::MAX_AS_U32.
999959
#[derive(Default)]
1000960
pub struct Interner {
1001961
arena: DroplessArena,
1002962
names: FxHashMap<&'static str, Symbol>,
1003963
strings: Vec<&'static str>,
1004-
gensyms: Vec<Symbol>,
1005964
}
1006965

1007966
impl Interner {
@@ -1034,34 +993,10 @@ impl Interner {
1034993
self.names.insert(string, name);
1035994
name
1036995
}
1037-
1038-
fn interned(&self, symbol: Symbol) -> Symbol {
1039-
if (symbol.0.as_usize()) < self.strings.len() {
1040-
symbol
1041-
} else {
1042-
self.gensyms[(SymbolIndex::MAX_AS_U32 - symbol.0.as_u32()) as usize]
1043-
}
1044-
}
1045-
1046-
fn gensymed(&mut self, symbol: Symbol) -> Symbol {
1047-
self.gensyms.push(symbol);
1048-
Symbol::new(SymbolIndex::MAX_AS_U32 - self.gensyms.len() as u32 + 1)
1049-
}
1050-
1051-
fn is_gensymed(&mut self, symbol: Symbol) -> bool {
1052-
symbol.0.as_usize() >= self.strings.len()
1053-
}
1054-
1055996
// Get the symbol as a string. `Symbol::as_str()` should be used in
1056997
// preference to this function.
1057998
pub fn get(&self, symbol: Symbol) -> &str {
1058-
match self.strings.get(symbol.0.as_usize()) {
1059-
Some(string) => string,
1060-
None => {
1061-
let symbol = self.gensyms[(SymbolIndex::MAX_AS_U32 - symbol.0.as_u32()) as usize];
1062-
self.strings[symbol.0.as_usize()]
1063-
}
1064-
}
999+
self.strings[symbol.0.as_usize()]
10651000
}
10661001
}
10671002

@@ -1246,19 +1181,12 @@ impl fmt::Display for LocalInternedString {
12461181
}
12471182
}
12481183

1249-
/// An alternative to `Symbol` that is focused on string contents. It has two
1250-
/// main differences to `Symbol`.
1184+
/// An alternative to `Symbol` that is focused on string contents.
12511185
///
1252-
/// First, its implementations of `Hash`, `PartialOrd` and `Ord` work with the
1186+
/// Its implementations of `Hash`, `PartialOrd` and `Ord` work with the
12531187
/// string chars rather than the symbol integer. This is useful when hash
12541188
/// stability is required across compile sessions, or a guaranteed sort
12551189
/// ordering is required.
1256-
///
1257-
/// Second, gensym-ness is irrelevant. E.g.:
1258-
/// ```
1259-
/// assert_ne!(Symbol::gensym("x"), Symbol::gensym("x"))
1260-
/// assert_eq!(Symbol::gensym("x").as_interned_str(), Symbol::gensym("x").as_interned_str())
1261-
/// ```
12621190
#[derive(Clone, Copy, PartialEq, Eq)]
12631191
pub struct InternedString {
12641192
symbol: Symbol,

src/libsyntax_pos/symbol/tests.rs

-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ fn interner_tests() {
1414
assert_eq!(i.intern("cat"), Symbol::new(1));
1515
// dog is still at zero
1616
assert_eq!(i.intern("dog"), Symbol::new(0));
17-
let z = i.intern("zebra");
18-
assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32));
19-
// gensym of same string gets new number:
20-
assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32 - 1));
21-
// gensym of *existing* string gets new number:
22-
let d = i.intern("dog");
23-
assert_eq!(i.gensymed(d), Symbol::new(SymbolIndex::MAX_AS_U32 - 2));
2417
}
2518

2619
#[test]

0 commit comments

Comments
 (0)