Skip to content

Commit 7efb40f

Browse files
committed
Remove gensyms
1 parent c646cda commit 7efb40f

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
@@ -803,17 +803,6 @@ impl Ident {
803803
Ident::new(self.name, self.span.modern_and_legacy())
804804
}
805805

806-
/// Transforms an underscore identifier into one with the same name, but
807-
/// gensymed. Leaves non-underscore identifiers unchanged.
808-
pub fn gensym_if_underscore(self) -> Ident {
809-
if self.name == kw::Underscore {
810-
let name = with_interner(|interner| interner.gensymed(self.name));
811-
Ident::new(name, self.span)
812-
} else {
813-
self
814-
}
815-
}
816-
817806
/// If the name of this ident is "_", then return a new unique identifier.
818807
/// Otherwise returns `self` unmodified
819808
#[inline]
@@ -830,8 +819,7 @@ impl Ident {
830819
self.name.as_str()
831820
}
832821

833-
/// Convert the name to an `InternedString`. This is a slowish operation
834-
/// because it requires locking the symbol interner.
822+
/// Convert the name to an `InternedString`.
835823
pub fn as_interned_str(self) -> InternedString {
836824
self.name.as_interned_str()
837825
}
@@ -886,26 +874,9 @@ impl UseSpecializedDecodable for Ident {
886874
}
887875
}
888876

889-
/// A symbol is an interned or gensymed string. A gensym is a symbol that is
890-
/// never equal to any other symbol.
891-
///
892-
/// Conceptually, a gensym can be thought of as a normal symbol with an
893-
/// invisible unique suffix. Gensyms are useful when creating new identifiers
894-
/// that must not match any existing identifiers, e.g. during macro expansion
895-
/// and syntax desugaring. Because gensyms should always be identifiers, all
896-
/// gensym operations are on `Ident` rather than `Symbol`. (Indeed, in the
897-
/// future the gensym-ness may be moved from `Symbol` to hygiene data.)
877+
/// An interned string.
898878
///
899-
/// Examples:
900-
/// ```
901-
/// assert_eq!(Ident::from_str("_"), Ident::from_str("_"))
902-
/// assert_ne!(Ident::from_str("_").gensym_if_underscore(), Ident::from_str("_"))
903-
/// assert_ne!(
904-
/// Ident::from_str("_").gensym_if_underscore(),
905-
/// Ident::from_str("_").gensym_if_underscore(),
906-
/// )
907-
/// ```
908-
/// Internally, a symbol is implemented as an index, and all operations
879+
/// Internally, a `Symbol` is implemented as an index, and all operations
909880
/// (including hashing, equality, and ordering) operate on that index. The use
910881
/// of `newtype_index!` means that `Option<Symbol>` only takes up 4 bytes,
911882
/// because `newtype_index!` reserves the last 256 values for tagging purposes.
@@ -956,12 +927,9 @@ impl Symbol {
956927
})
957928
}
958929

959-
/// Convert to an `InternedString`. This is a slowish operation because it
960-
/// requires locking the symbol interner.
930+
/// Convert to an `InternedString`.
961931
pub fn as_interned_str(self) -> InternedString {
962-
with_interner(|interner| InternedString {
963-
symbol: interner.interned(self)
964-
})
932+
InternedString { symbol: self }
965933
}
966934

967935
pub fn as_u32(self) -> u32 {
@@ -971,12 +939,7 @@ impl Symbol {
971939

972940
impl fmt::Debug for Symbol {
973941
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
974-
let is_gensymed = with_interner(|interner| interner.is_gensymed(*self));
975-
if is_gensymed {
976-
write!(f, "{}({:?})", self, self.0)
977-
} else {
978-
write!(f, "{}", self)
979-
}
942+
fmt::Display::fmt(self, f)
980943
}
981944
}
982945

@@ -999,15 +962,11 @@ impl Decodable for Symbol {
999962
}
1000963

1001964
// The `&'static str`s in this type actually point into the arena.
1002-
//
1003-
// Note that normal symbols are indexed upward from 0, and gensyms are indexed
1004-
// downward from SymbolIndex::MAX_AS_U32.
1005965
#[derive(Default)]
1006966
pub struct Interner {
1007967
arena: DroplessArena,
1008968
names: FxHashMap<&'static str, Symbol>,
1009969
strings: Vec<&'static str>,
1010-
gensyms: Vec<Symbol>,
1011970
}
1012971

1013972
impl Interner {
@@ -1040,34 +999,10 @@ impl Interner {
1040999
self.names.insert(string, name);
10411000
name
10421001
}
1043-
1044-
fn interned(&self, symbol: Symbol) -> Symbol {
1045-
if (symbol.0.as_usize()) < self.strings.len() {
1046-
symbol
1047-
} else {
1048-
self.gensyms[(SymbolIndex::MAX_AS_U32 - symbol.0.as_u32()) as usize]
1049-
}
1050-
}
1051-
1052-
fn gensymed(&mut self, symbol: Symbol) -> Symbol {
1053-
self.gensyms.push(symbol);
1054-
Symbol::new(SymbolIndex::MAX_AS_U32 - self.gensyms.len() as u32 + 1)
1055-
}
1056-
1057-
fn is_gensymed(&mut self, symbol: Symbol) -> bool {
1058-
symbol.0.as_usize() >= self.strings.len()
1059-
}
1060-
10611002
// Get the symbol as a string. `Symbol::as_str()` should be used in
10621003
// preference to this function.
10631004
pub fn get(&self, symbol: Symbol) -> &str {
1064-
match self.strings.get(symbol.0.as_usize()) {
1065-
Some(string) => string,
1066-
None => {
1067-
let symbol = self.gensyms[(SymbolIndex::MAX_AS_U32 - symbol.0.as_u32()) as usize];
1068-
self.strings[symbol.0.as_usize()]
1069-
}
1070-
}
1005+
self.strings[symbol.0.as_usize()]
10711006
}
10721007
}
10731008

@@ -1252,19 +1187,12 @@ impl fmt::Display for LocalInternedString {
12521187
}
12531188
}
12541189

1255-
/// An alternative to `Symbol` that is focused on string contents. It has two
1256-
/// main differences to `Symbol`.
1190+
/// An alternative to `Symbol` that is focused on string contents.
12571191
///
1258-
/// First, its implementations of `Hash`, `PartialOrd` and `Ord` work with the
1192+
/// Its implementations of `Hash`, `PartialOrd` and `Ord` work with the
12591193
/// string chars rather than the symbol integer. This is useful when hash
12601194
/// stability is required across compile sessions, or a guaranteed sort
12611195
/// ordering is required.
1262-
///
1263-
/// Second, gensym-ness is irrelevant. E.g.:
1264-
/// ```
1265-
/// assert_ne!(Symbol::gensym("x"), Symbol::gensym("x"))
1266-
/// assert_eq!(Symbol::gensym("x").as_interned_str(), Symbol::gensym("x").as_interned_str())
1267-
/// ```
12681196
#[derive(Clone, Copy, PartialEq, Eq)]
12691197
pub struct InternedString {
12701198
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)