@@ -807,25 +807,13 @@ impl Ident {
807
807
Ident :: new ( self . name , self . span . modern_and_legacy ( ) )
808
808
}
809
809
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
-
821
810
/// Convert the name to a `LocalInternedString`. This is a slowish
822
811
/// operation because it requires locking the symbol interner.
823
812
pub fn as_str ( self ) -> LocalInternedString {
824
813
self . name . as_str ( )
825
814
}
826
815
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`.
829
817
pub fn as_interned_str ( self ) -> InternedString {
830
818
self . name . as_interned_str ( )
831
819
}
@@ -880,26 +868,9 @@ impl UseSpecializedDecodable for Ident {
880
868
}
881
869
}
882
870
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.
885
872
///
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
903
874
/// (including hashing, equality, and ordering) operate on that index. The use
904
875
/// of `rustc_index::newtype_index!` means that `Option<Symbol>` only takes up 4 bytes,
905
876
/// because `rustc_index::newtype_index!` reserves the last 256 values for tagging purposes.
@@ -950,12 +921,9 @@ impl Symbol {
950
921
} )
951
922
}
952
923
953
- /// Convert to an `InternedString`. This is a slowish operation because it
954
- /// requires locking the symbol interner.
924
+ /// Convert to an `InternedString`.
955
925
pub fn as_interned_str ( self ) -> InternedString {
956
- with_interner ( |interner| InternedString {
957
- symbol : interner. interned ( self )
958
- } )
926
+ InternedString { symbol : self }
959
927
}
960
928
961
929
pub fn as_u32 ( self ) -> u32 {
@@ -965,12 +933,7 @@ impl Symbol {
965
933
966
934
impl fmt:: Debug for Symbol {
967
935
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)
974
937
}
975
938
}
976
939
@@ -993,15 +956,11 @@ impl Decodable for Symbol {
993
956
}
994
957
995
958
// 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.
999
959
#[ derive( Default ) ]
1000
960
pub struct Interner {
1001
961
arena : DroplessArena ,
1002
962
names : FxHashMap < & ' static str , Symbol > ,
1003
963
strings : Vec < & ' static str > ,
1004
- gensyms : Vec < Symbol > ,
1005
964
}
1006
965
1007
966
impl Interner {
@@ -1034,34 +993,10 @@ impl Interner {
1034
993
self . names . insert ( string, name) ;
1035
994
name
1036
995
}
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
-
1055
996
// Get the symbol as a string. `Symbol::as_str()` should be used in
1056
997
// preference to this function.
1057
998
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 ( ) ]
1065
1000
}
1066
1001
}
1067
1002
@@ -1246,19 +1181,12 @@ impl fmt::Display for LocalInternedString {
1246
1181
}
1247
1182
}
1248
1183
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.
1251
1185
///
1252
- /// First, its implementations of `Hash`, `PartialOrd` and `Ord` work with the
1186
+ /// Its implementations of `Hash`, `PartialOrd` and `Ord` work with the
1253
1187
/// string chars rather than the symbol integer. This is useful when hash
1254
1188
/// stability is required across compile sessions, or a guaranteed sort
1255
1189
/// 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
- /// ```
1262
1190
#[ derive( Clone , Copy , PartialEq , Eq ) ]
1263
1191
pub struct InternedString {
1264
1192
symbol : Symbol ,
0 commit comments