1 //! An "interner" is a data structure that associates values with usize tags and
2 //! allows bidirectional lookup; i.e., given a value, one can easily find the
3 //! type, and vice versa.
5 use arena::DroplessArena;
6 use rustc_data_structures::fx::FxHashMap;
7 use rustc_data_structures::indexed_vec::Idx;
8 use rustc_data_structures::newtype_index;
9 use rustc_macros::symbols;
10 use serialize::{Decodable, Decoder, Encodable, Encoder};
12 use std::cmp::{PartialEq, Ordering, PartialOrd, Ord};
14 use std::hash::{Hash, Hasher};
17 use crate::hygiene::SyntaxContext;
18 use crate::{Span, DUMMY_SP, GLOBALS};
21 // After modifying this list adjust `is_special`, `is_used_keyword`/`is_unused_keyword`,
22 // this should be rarely necessary though if the keywords are kept in alphabetic order.
24 // Special reserved identifiers used internally for elided lifetimes,
25 // unnamed method parameters, crate root module, error recovery etc.
28 DollarCrate: "$crate",
31 // Keywords that are used in stable Rust.
68 // Keywords that are used in unstable Rust or reserved for future use.
82 // Edition-specific keywords that are used in stable Rust.
83 Dyn: "dyn", // >= 2018 Edition only
85 // Edition-specific keywords that are used in unstable Rust or reserved for future use.
86 Async: "async", // >= 2018 Edition only
87 Await: "await", // >= 2018 Edition only
88 Try: "try", // >= 2018 Edition only
90 // Special lifetime names
91 UnderscoreLifetime: "'_",
92 StaticLifetime: "'static",
94 // Weak keywords, have special meaning only in specific contexts.
98 Existential: "existential",
102 // Symbols that can be referred to with syntax_pos::sym::*. The symbol is
103 // the stringified identifier unless otherwise specified (e.g.
104 // `proc_dash_macro` represents "proc-macro").
106 // As well as the symbols listed, there are symbols for the the strings
107 // "0", "1", ..., "9", which are accessible via `sym::integer`.
109 aarch64_target_feature,
112 abi_msp430_interrupt,
120 advanced_slice_patterns,
132 allow_internal_unsafe,
133 allow_internal_unstable,
134 allow_internal_unstable_backcompat_hack,
138 arbitrary_self_types,
144 associated_type_defaults,
150 augmented_assignments,
151 automatically_derived,
152 avx512_target_feature,
157 bind_by_move_pattern_guards,
159 borrowck_graphviz_postflow,
160 borrowck_graphviz_preflow,
163 braced_empty_structs,
170 cfg_target_has_atomic,
171 cfg_target_thread_local,
177 closure_to_fn_coercion,
179 cmpxchg16b_target_feature,
184 conservative_impl_trait,
186 const_compare_raw_pointers,
194 const_raw_ptr_to_usize_cast,
206 crate_visibility_modifier,
209 custom_inner_attributes,
210 custom_test_frameworks,
214 default_lib_allocator,
215 default_type_parameter_fallback,
228 document_private_items,
229 dotdoteq_in_patterns,
230 dotdot_in_tuple_patterns,
231 double_braced_crate: "{{crate}}",
232 double_braced_impl: "{{impl}}",
233 double_braced_misc: "{{misc}}",
234 double_braced_closure: "{{closure}}",
235 double_braced_constructor: "{{constructor}}",
236 double_braced_constant: "{{constant}}",
237 double_braced_opaque: "{{opaque}}",
239 dropck_parametricity,
250 exclusive_range_pattern,
251 exhaustive_integer_patterns,
257 extern_absolute_paths,
259 extern_crate_item_prelude,
270 field_init_shorthand,
290 generic_associated_types,
299 hexagon_target_feature,
301 homogeneous_aggregate,
315 if_while_or_patterns,
317 impl_header_lifetime_elision,
318 impl_trait_in_bindings,
324 inclusive_range_syntax,
325 infer_outlives_requirements,
326 infer_static_outlives_requirements,
333 irrefutable_let_patterns,
336 issue_5723_bootstrap,
337 issue_tracker_base_url,
354 link_llvm_intrinsics,
362 macro_at_most_once_rep,
365 macro_lifetime_matcher,
366 macro_literal_matcher,
377 match_beginning_vert,
378 match_default_bindings,
388 movbe_target_feature,
422 omit_gdb_pretty_printer_section,
429 optin_builtin_traits,
437 overlapping_marker_traits,
442 panic_implementation,
459 poll_with_tls_context,
460 powerpc_target_feature,
461 precise_pointer_size_matching,
465 proc_dash_macro: "proc-macro",
467 proc_macro_attribute,
474 proc_macro_non_items,
475 proc_macro_path_invoc,
479 quad_precision_float,
492 reexport_test_harness_main,
502 re_rebalance_coherence,
514 rustc_allocator_nounwind,
515 rustc_allow_const_fn_ptr,
516 rustc_args_required_const,
519 rustc_const_unstable,
520 rustc_conversion_suggestion,
521 rustc_copy_clone_marker,
524 rustc_diagnostic_macros,
526 rustc_doc_only_macro,
527 rustc_dump_env_program_clauses,
528 rustc_dump_program_clauses,
529 rustc_dump_user_substs,
531 rustc_expected_cgu_reuse,
532 rustc_if_this_changed,
533 rustc_inherit_overflow_checks,
535 rustc_layout_scalar_valid_range_end,
536 rustc_layout_scalar_valid_range_start,
538 rustc_nonnull_optimization_guaranteed,
539 rustc_object_lifetime_default,
540 rustc_on_unimplemented,
543 rustc_partition_codegened,
544 rustc_partition_reused,
546 rustc_peek_definite_init,
547 rustc_peek_maybe_init,
548 rustc_peek_maybe_uninit,
550 rustc_proc_macro_decls,
554 rustc_std_internal_symbol,
558 rustc_then_this_would_need,
559 rustc_transparent_macro,
563 rust_eh_unwind_resume,
565 __rust_unstable_column,
566 rvalue_static_promotion,
582 sse4a_target_feature,
593 stmt_expr_attributes,
595 struct_field_attributes,
607 termination_trait_test,
610 test_accepted_feature,
612 test_removed_feature,
632 type_alias_enum_variants,
642 underscore_const_names,
644 underscore_lifetimes,
646 universal_impl_trait,
649 unrestricted_attribute_tokens,
650 unsafe_destructor_blind_to_params,
653 unsized_tuple_coercion,
668 visible_private_types,
671 warn_directory_ownership,
681 #[derive(Copy, Clone, Eq)]
689 /// Constructs a new identifier from a symbol and a span.
690 pub const fn new(name: Symbol, span: Span) -> Ident {
694 /// Constructs a new identifier with an empty syntax context.
696 pub const fn with_empty_ctxt(name: Symbol) -> Ident {
697 Ident::new(name, DUMMY_SP)
701 pub fn invalid() -> Ident {
702 Ident::with_empty_ctxt(kw::Invalid)
705 /// Maps an interned string to an identifier with an empty syntax context.
706 pub fn from_interned_str(string: InternedString) -> Ident {
707 Ident::with_empty_ctxt(string.as_symbol())
710 /// Maps a string to an identifier with an empty span.
711 pub fn from_str(string: &str) -> Ident {
712 Ident::with_empty_ctxt(Symbol::intern(string))
715 /// Maps a string and a span to an identifier.
716 pub fn from_str_and_span(string: &str, span: Span) -> Ident {
717 Ident::new(Symbol::intern(string), span)
720 /// Replaces `lo` and `hi` with those from `span`, but keep hygiene context.
721 pub fn with_span_pos(self, span: Span) -> Ident {
722 Ident::new(self.name, span.with_ctxt(self.span.ctxt()))
725 pub fn without_first_quote(self) -> Ident {
726 Ident::new(Symbol::intern(self.as_str().trim_start_matches('\'')), self.span)
729 /// "Normalize" ident for use in comparisons using "item hygiene".
730 /// Identifiers with same string value become same if they came from the same "modern" macro
731 /// (e.g., `macro` item, but not `macro_rules` item) and stay different if they came from
732 /// different "modern" macros.
733 /// Technically, this operation strips all non-opaque marks from ident's syntactic context.
734 pub fn modern(self) -> Ident {
735 Ident::new(self.name, self.span.modern())
738 /// "Normalize" ident for use in comparisons using "local variable hygiene".
739 /// Identifiers with same string value become same if they came from the same non-transparent
740 /// macro (e.g., `macro` or `macro_rules!` items) and stay different if they came from different
741 /// non-transparent macros.
742 /// Technically, this operation strips all transparent marks from ident's syntactic context.
743 pub fn modern_and_legacy(self) -> Ident {
744 Ident::new(self.name, self.span.modern_and_legacy())
747 /// Transforms an identifier into one with the same name, but gensymed.
748 pub fn gensym(self) -> Ident {
749 let name = with_interner(|interner| interner.gensymed(self.name));
750 Ident::new(name, self.span)
753 /// Transforms an underscore identifier into one with the same name, but
754 /// gensymed. Leaves non-underscore identifiers unchanged.
755 pub fn gensym_if_underscore(self) -> Ident {
756 if self.name == kw::Underscore { self.gensym() } else { self }
759 // WARNING: this function is deprecated and will be removed in the future.
760 pub fn is_gensymed(self) -> bool {
761 with_interner(|interner| interner.is_gensymed(self.name))
764 pub fn as_str(self) -> LocalInternedString {
768 pub fn as_interned_str(self) -> InternedString {
769 self.name.as_interned_str()
773 impl PartialEq for Ident {
774 fn eq(&self, rhs: &Self) -> bool {
775 self.name == rhs.name && self.span.ctxt() == rhs.span.ctxt()
779 impl Hash for Ident {
780 fn hash<H: Hasher>(&self, state: &mut H) {
781 self.name.hash(state);
782 self.span.ctxt().hash(state);
786 impl fmt::Debug for Ident {
787 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
788 write!(f, "{}{:?}", self.name, self.span.ctxt())
792 impl fmt::Display for Ident {
793 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
794 fmt::Display::fmt(&self.name, f)
798 impl Encodable for Ident {
799 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
800 if self.span.ctxt().modern() == SyntaxContext::empty() {
801 s.emit_str(&self.as_str())
802 } else { // FIXME(jseyfried): intercrate hygiene
803 let mut string = "#".to_owned();
804 string.push_str(&self.as_str());
810 impl Decodable for Ident {
811 fn decode<D: Decoder>(d: &mut D) -> Result<Ident, D::Error> {
812 let string = d.read_str()?;
813 Ok(if !string.starts_with('#') {
814 Ident::from_str(&string)
815 } else { // FIXME(jseyfried): intercrate hygiene
816 Ident::from_str(&string[1..]).gensym()
821 /// A symbol is an interned or gensymed string. A gensym is a symbol that is
822 /// never equal to any other symbol.
824 /// Conceptually, a gensym can be thought of as a normal symbol with an
825 /// invisible unique suffix. Gensyms are useful when creating new identifiers
826 /// that must not match any existing identifiers, e.g. during macro expansion
827 /// and syntax desugaring. Because gensyms should always be identifiers, all
828 /// gensym operations are on `Ident` rather than `Symbol`. (Indeed, in the
829 /// future the gensym-ness may be moved from `Symbol` to hygiene data.)
833 /// assert_eq!(Ident::from_str("x"), Ident::from_str("x"))
834 /// assert_ne!(Ident::from_str("x").gensym(), Ident::from_str("x"))
835 /// assert_ne!(Ident::from_str("x").gensym(), Ident::from_str("x").gensym())
837 /// Internally, a symbol is implemented as an index, and all operations
838 /// (including hashing, equality, and ordering) operate on that index. The use
839 /// of `newtype_index!` means that `Option<Symbol>` only takes up 4 bytes,
840 /// because `newtype_index!` reserves the last 256 values for tagging purposes.
842 /// Note that `Symbol` cannot directly be a `newtype_index!` because it
843 /// implements `fmt::Debug`, `Encodable`, and `Decodable` in special ways.
844 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
845 pub struct Symbol(SymbolIndex);
848 pub struct SymbolIndex { .. }
852 const fn new(n: u32) -> Self {
853 Symbol(SymbolIndex::from_u32_const(n))
856 /// Maps a string to its interned representation.
857 pub fn intern(string: &str) -> Self {
858 with_interner(|interner| interner.intern(string))
861 pub fn as_str(self) -> LocalInternedString {
862 with_interner(|interner| unsafe {
863 LocalInternedString {
864 string: std::mem::transmute::<&str, &str>(interner.get(self))
869 pub fn as_interned_str(self) -> InternedString {
870 with_interner(|interner| InternedString {
871 symbol: interner.interned(self)
875 pub fn as_u32(self) -> u32 {
880 impl fmt::Debug for Symbol {
881 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
882 let is_gensymed = with_interner(|interner| interner.is_gensymed(*self));
884 write!(f, "{}({:?})", self, self.0)
886 write!(f, "{}", self)
891 impl fmt::Display for Symbol {
892 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
893 fmt::Display::fmt(&self.as_str(), f)
897 impl Encodable for Symbol {
898 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
899 s.emit_str(&self.as_str())
903 impl Decodable for Symbol {
904 fn decode<D: Decoder>(d: &mut D) -> Result<Symbol, D::Error> {
905 Ok(Symbol::intern(&d.read_str()?))
909 // The `&'static str`s in this type actually point into the arena.
911 // Note that normal symbols are indexed upward from 0, and gensyms are indexed
912 // downward from SymbolIndex::MAX_AS_U32.
914 pub struct Interner {
915 arena: DroplessArena,
916 names: FxHashMap<&'static str, Symbol>,
917 strings: Vec<&'static str>,
918 gensyms: Vec<Symbol>,
922 fn prefill(init: &[&'static str]) -> Self {
923 let symbols = (0 .. init.len() as u32).map(Symbol::new);
925 strings: init.to_vec(),
926 names: init.iter().copied().zip(symbols).collect(),
931 pub fn intern(&mut self, string: &str) -> Symbol {
932 if let Some(&name) = self.names.get(string) {
936 let name = Symbol::new(self.strings.len() as u32);
938 // `from_utf8_unchecked` is safe since we just allocated a `&str` which is known to be
940 let string: &str = unsafe {
941 str::from_utf8_unchecked(self.arena.alloc_slice(string.as_bytes()))
943 // It is safe to extend the arena allocation to `'static` because we only access
944 // these while the arena is still alive.
945 let string: &'static str = unsafe {
946 &*(string as *const str)
948 self.strings.push(string);
949 self.names.insert(string, name);
953 fn interned(&self, symbol: Symbol) -> Symbol {
954 if (symbol.0.as_usize()) < self.strings.len() {
957 self.gensyms[(SymbolIndex::MAX_AS_U32 - symbol.0.as_u32()) as usize]
961 fn gensymed(&mut self, symbol: Symbol) -> Symbol {
962 self.gensyms.push(symbol);
963 Symbol::new(SymbolIndex::MAX_AS_U32 - self.gensyms.len() as u32 + 1)
966 fn is_gensymed(&mut self, symbol: Symbol) -> bool {
967 symbol.0.as_usize() >= self.strings.len()
970 // Get the symbol as a string. `Symbol::as_str()` should be used in
971 // preference to this function.
972 pub fn get(&self, symbol: Symbol) -> &str {
973 match self.strings.get(symbol.0.as_usize()) {
974 Some(string) => string,
976 let symbol = self.gensyms[(SymbolIndex::MAX_AS_U32 - symbol.0.as_u32()) as usize];
977 self.strings[symbol.0.as_usize()]
983 // This module has a very short name because it's used a lot.
989 // This module has a very short name because it's used a lot.
991 use std::convert::TryInto;
996 // Get the symbol for an integer. The first few non-negative integers each
997 // have a static symbol and therefore are fast.
998 pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
999 if let Result::Ok(idx) = n.try_into() {
1000 if let Option::Some(&sym) = digits_array.get(idx) {
1004 Symbol::intern(&n.to_string())
1009 fn is_used_keyword_2018(self) -> bool {
1013 fn is_unused_keyword_2018(self) -> bool {
1014 self >= kw::Async && self <= kw::Try
1017 /// Used for sanity checking rustdoc keyword sections.
1018 pub fn is_doc_keyword(self) -> bool {
1024 // Returns `true` for reserved identifiers used internally for elided lifetimes,
1025 // unnamed method parameters, crate root module, error recovery etc.
1026 pub fn is_special(self) -> bool {
1027 self.name <= kw::Underscore
1030 /// Returns `true` if the token is a keyword used in the language.
1031 pub fn is_used_keyword(self) -> bool {
1032 // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
1033 self.name >= kw::As && self.name <= kw::While ||
1034 self.name.is_used_keyword_2018() && self.span.rust_2018()
1037 /// Returns `true` if the token is a keyword reserved for possible future use.
1038 pub fn is_unused_keyword(self) -> bool {
1039 // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
1040 self.name >= kw::Abstract && self.name <= kw::Yield ||
1041 self.name.is_unused_keyword_2018() && self.span.rust_2018()
1044 /// Returns `true` if the token is either a special identifier or a keyword.
1045 pub fn is_reserved(self) -> bool {
1046 self.is_special() || self.is_used_keyword() || self.is_unused_keyword()
1049 /// A keyword or reserved identifier that can be used as a path segment.
1050 pub fn is_path_segment_keyword(self) -> bool {
1051 self.name == kw::Super ||
1052 self.name == kw::SelfLower ||
1053 self.name == kw::SelfUpper ||
1054 self.name == kw::Crate ||
1055 self.name == kw::PathRoot ||
1056 self.name == kw::DollarCrate
1059 /// This identifier can be a raw identifier.
1060 pub fn can_be_raw(self) -> bool {
1061 self.name != kw::Invalid && self.name != kw::Underscore &&
1062 !self.is_path_segment_keyword()
1065 /// We see this identifier in a normal identifier position, like variable name or a type.
1066 /// How was it written originally? Did it use the raw form? Let's try to guess.
1067 pub fn is_raw_guess(self) -> bool {
1068 self.can_be_raw() && self.is_reserved()
1072 // If an interner exists, return it. Otherwise, prepare a fresh one.
1074 fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {
1075 GLOBALS.with(|globals| f(&mut *globals.symbol_interner.lock()))
1078 /// An alternative to `Symbol` and `InternedString`, useful when the chars
1079 /// within the symbol need to be accessed. It is best used for temporary
1082 /// Because the interner outlives any thread which uses this type, we can
1083 /// safely treat `string` which points to interner data, as an immortal string,
1084 /// as long as this type never crosses between threads.
1086 // FIXME: ensure that the interner outlives any thread which uses
1087 // `LocalInternedString`, by creating a new thread right after constructing the
1089 #[derive(Clone, Copy, Hash, PartialOrd, Eq, Ord)]
1090 pub struct LocalInternedString {
1091 string: &'static str,
1094 impl LocalInternedString {
1095 /// Maps a string to its interned representation.
1096 pub fn intern(string: &str) -> Self {
1097 let string = with_interner(|interner| {
1098 let symbol = interner.intern(string);
1099 interner.strings[symbol.0.as_usize()]
1101 LocalInternedString {
1102 string: unsafe { std::mem::transmute::<&str, &str>(string) }
1106 pub fn as_interned_str(self) -> InternedString {
1108 symbol: Symbol::intern(self.string)
1112 pub fn get(&self) -> &str {
1113 // This returns a valid string since we ensure that `self` outlives the interner
1114 // by creating the interner on a thread which outlives threads which can access it.
1115 // This type cannot move to a thread which outlives the interner since it does
1116 // not implement Send.
1121 impl<U: ?Sized> std::convert::AsRef<U> for LocalInternedString
1123 str: std::convert::AsRef<U>
1125 fn as_ref(&self) -> &U {
1126 self.string.as_ref()
1130 impl<T: std::ops::Deref<Target = str>> std::cmp::PartialEq<T> for LocalInternedString {
1131 fn eq(&self, other: &T) -> bool {
1132 self.string == other.deref()
1136 impl std::cmp::PartialEq<LocalInternedString> for str {
1137 fn eq(&self, other: &LocalInternedString) -> bool {
1138 self == other.string
1142 impl<'a> std::cmp::PartialEq<LocalInternedString> for &'a str {
1143 fn eq(&self, other: &LocalInternedString) -> bool {
1144 *self == other.string
1148 impl std::cmp::PartialEq<LocalInternedString> for String {
1149 fn eq(&self, other: &LocalInternedString) -> bool {
1150 self == other.string
1154 impl<'a> std::cmp::PartialEq<LocalInternedString> for &'a String {
1155 fn eq(&self, other: &LocalInternedString) -> bool {
1156 *self == other.string
1160 impl !Send for LocalInternedString {}
1161 impl !Sync for LocalInternedString {}
1163 impl std::ops::Deref for LocalInternedString {
1165 fn deref(&self) -> &str { self.string }
1168 impl fmt::Debug for LocalInternedString {
1169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1170 fmt::Debug::fmt(self.string, f)
1174 impl fmt::Display for LocalInternedString {
1175 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1176 fmt::Display::fmt(self.string, f)
1180 impl Decodable for LocalInternedString {
1181 fn decode<D: Decoder>(d: &mut D) -> Result<LocalInternedString, D::Error> {
1182 Ok(LocalInternedString::intern(&d.read_str()?))
1186 impl Encodable for LocalInternedString {
1187 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
1188 s.emit_str(self.string)
1192 /// An alternative to `Symbol` that is focused on string contents. It has two
1193 /// main differences to `Symbol`.
1195 /// First, its implementations of `Hash`, `PartialOrd` and `Ord` work with the
1196 /// string chars rather than the symbol integer. This is useful when hash
1197 /// stability is required across compile sessions, or a guaranteed sort
1198 /// ordering is required.
1200 /// Second, gensym-ness is irrelevant. E.g.:
1202 /// assert_ne!(Symbol::gensym("x"), Symbol::gensym("x"))
1203 /// assert_eq!(Symbol::gensym("x").as_interned_str(), Symbol::gensym("x").as_interned_str())
1205 #[derive(Clone, Copy, PartialEq, Eq)]
1206 pub struct InternedString {
1210 impl InternedString {
1211 /// Maps a string to its interned representation.
1212 pub fn intern(string: &str) -> Self {
1214 symbol: Symbol::intern(string)
1218 pub fn with<F: FnOnce(&str) -> R, R>(self, f: F) -> R {
1219 let str = with_interner(|interner| {
1220 interner.get(self.symbol) as *const str
1222 // This is safe because the interner keeps string alive until it is dropped.
1223 // We can access it because we know the interner is still alive since we use a
1224 // scoped thread local to access it, and it was alive at the beginning of this scope
1228 fn with2<F: FnOnce(&str, &str) -> R, R>(self, other: &InternedString, f: F) -> R {
1229 let (self_str, other_str) = with_interner(|interner| {
1230 (interner.get(self.symbol) as *const str,
1231 interner.get(other.symbol) as *const str)
1233 // This is safe for the same reason that `with` is safe.
1234 unsafe { f(&*self_str, &*other_str) }
1237 pub fn as_symbol(self) -> Symbol {
1241 pub fn as_str(self) -> LocalInternedString {
1242 self.symbol.as_str()
1246 impl Hash for InternedString {
1247 fn hash<H: Hasher>(&self, state: &mut H) {
1248 self.with(|str| str.hash(state))
1252 impl PartialOrd<InternedString> for InternedString {
1253 fn partial_cmp(&self, other: &InternedString) -> Option<Ordering> {
1254 if self.symbol == other.symbol {
1255 return Some(Ordering::Equal);
1257 self.with2(other, |self_str, other_str| self_str.partial_cmp(other_str))
1261 impl Ord for InternedString {
1262 fn cmp(&self, other: &InternedString) -> Ordering {
1263 if self.symbol == other.symbol {
1264 return Ordering::Equal;
1266 self.with2(other, |self_str, other_str| self_str.cmp(other_str))
1270 impl fmt::Debug for InternedString {
1271 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1272 self.with(|str| fmt::Debug::fmt(&str, f))
1276 impl fmt::Display for InternedString {
1277 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1278 self.with(|str| fmt::Display::fmt(&str, f))
1282 impl Decodable for InternedString {
1283 fn decode<D: Decoder>(d: &mut D) -> Result<InternedString, D::Error> {
1284 Ok(InternedString::intern(&d.read_str()?))
1288 impl Encodable for InternedString {
1289 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
1290 self.with(|string| s.emit_str(string))
1301 fn interner_tests() {
1302 let mut i: Interner = Interner::default();
1303 // first one is zero:
1304 assert_eq!(i.intern("dog"), Symbol::new(0));
1305 // re-use gets the same entry:
1306 assert_eq!(i.intern("dog"), Symbol::new(0));
1307 // different string gets a different #:
1308 assert_eq!(i.intern("cat"), Symbol::new(1));
1309 assert_eq!(i.intern("cat"), Symbol::new(1));
1310 // dog is still at zero
1311 assert_eq!(i.intern("dog"), Symbol::new(0));
1312 let z = i.intern("zebra");
1313 assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32));
1314 // gensym of same string gets new number:
1315 assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32 - 1));
1316 // gensym of *existing* string gets new number:
1317 let d = i.intern("dog");
1318 assert_eq!(i.gensymed(d), Symbol::new(SymbolIndex::MAX_AS_U32 - 2));
1322 fn without_first_quote_test() {
1323 GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || {
1324 let i = Ident::from_str("'break");
1325 assert_eq!(i.without_first_quote().name, kw::Break);