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_enum_discriminant,
139 arbitrary_self_types,
145 associated_type_bounds,
146 associated_type_defaults,
153 augmented_assignments,
154 automatically_derived,
155 avx512_target_feature,
160 bind_by_move_pattern_guards,
163 borrowck_graphviz_postflow,
164 borrowck_graphviz_preflow,
167 braced_empty_structs,
175 cfg_target_has_atomic,
176 cfg_target_thread_local,
184 closure_to_fn_coercion,
186 cmpxchg16b_target_feature,
191 conservative_impl_trait,
193 const_compare_raw_pointers,
202 const_raw_ptr_to_usize_cast,
215 crate_visibility_modifier,
218 custom_inner_attributes,
219 custom_test_frameworks,
224 default_lib_allocator,
225 default_type_parameter_fallback,
240 document_private_items,
241 dotdoteq_in_patterns,
242 dotdot_in_tuple_patterns,
243 double_braced_crate: "{{crate}}",
244 double_braced_impl: "{{impl}}",
245 double_braced_misc: "{{misc}}",
246 double_braced_closure: "{{closure}}",
247 double_braced_constructor: "{{constructor}}",
248 double_braced_constant: "{{constant}}",
249 double_braced_opaque: "{{opaque}}",
251 dropck_parametricity,
263 exclusive_range_pattern,
264 exhaustive_integer_patterns,
270 extern_absolute_paths,
272 extern_crate_item_prelude,
283 field_init_shorthand,
305 generic_associated_types,
314 hexagon_target_feature,
316 homogeneous_aggregate,
330 if_while_or_patterns,
332 impl_header_lifetime_elision,
334 impl_trait_in_bindings,
340 inclusive_range_syntax,
341 infer_outlives_requirements,
342 infer_static_outlives_requirements,
349 irrefutable_let_patterns,
352 issue_5723_bootstrap,
353 issue_tracker_base_url,
372 link_llvm_intrinsics,
381 macro_at_most_once_rep,
384 macro_lifetime_matcher,
385 macro_literal_matcher,
396 match_beginning_vert,
397 match_default_bindings,
408 movbe_target_feature,
442 omit_gdb_pretty_printer_section,
449 optin_builtin_traits,
457 overlapping_marker_traits,
462 panic_implementation,
481 poll_with_tls_context,
482 powerpc_target_feature,
483 precise_pointer_size_matching,
487 proc_dash_macro: "proc-macro",
489 proc_macro_attribute,
496 proc_macro_non_items,
497 proc_macro_path_invoc,
501 quad_precision_float,
514 reexport_test_harness_main,
524 re_rebalance_coherence,
538 rustc_allocator_nounwind,
539 rustc_allow_const_fn_ptr,
540 rustc_args_required_const,
544 rustc_const_unstable,
545 rustc_conversion_suggestion,
546 rustc_copy_clone_marker,
549 rustc_diagnostic_macros,
552 rustc_dump_env_program_clauses,
553 rustc_dump_program_clauses,
554 rustc_dump_user_substs,
556 rustc_expected_cgu_reuse,
557 rustc_if_this_changed,
558 rustc_inherit_overflow_checks,
560 rustc_layout_scalar_valid_range_end,
561 rustc_layout_scalar_valid_range_start,
562 rustc_macro_transparency,
564 rustc_nonnull_optimization_guaranteed,
565 rustc_object_lifetime_default,
566 rustc_on_unimplemented,
569 rustc_partition_codegened,
570 rustc_partition_reused,
572 rustc_peek_definite_init,
573 rustc_peek_maybe_init,
574 rustc_peek_maybe_uninit,
576 rustc_proc_macro_decls,
580 rustc_std_internal_symbol,
584 rustc_then_this_would_need,
588 rust_eh_unwind_resume,
590 __rust_unstable_column,
591 rvalue_static_promotion,
608 sse4a_target_feature,
619 stmt_expr_attributes,
621 struct_field_attributes,
633 termination_trait_test,
636 test_accepted_feature,
638 test_removed_feature,
660 type_alias_enum_variants,
670 underscore_const_names,
672 underscore_lifetimes,
674 universal_impl_trait,
677 unrestricted_attribute_tokens,
678 unsafe_destructor_blind_to_params,
681 unsized_tuple_coercion,
696 visible_private_types,
699 warn_directory_ownership,
709 #[derive(Copy, Clone, Eq)]
717 /// Constructs a new identifier from a symbol and a span.
718 pub const fn new(name: Symbol, span: Span) -> Ident {
722 /// Constructs a new identifier with an empty syntax context.
724 pub const fn with_empty_ctxt(name: Symbol) -> Ident {
725 Ident::new(name, DUMMY_SP)
729 pub fn invalid() -> Ident {
730 Ident::with_empty_ctxt(kw::Invalid)
733 /// Maps an interned string to an identifier with an empty syntax context.
734 pub fn from_interned_str(string: InternedString) -> Ident {
735 Ident::with_empty_ctxt(string.as_symbol())
738 /// Maps a string to an identifier with an empty span.
739 pub fn from_str(string: &str) -> Ident {
740 Ident::with_empty_ctxt(Symbol::intern(string))
743 /// Maps a string and a span to an identifier.
744 pub fn from_str_and_span(string: &str, span: Span) -> Ident {
745 Ident::new(Symbol::intern(string), span)
748 /// Replaces `lo` and `hi` with those from `span`, but keep hygiene context.
749 pub fn with_span_pos(self, span: Span) -> Ident {
750 Ident::new(self.name, span.with_ctxt(self.span.ctxt()))
753 pub fn without_first_quote(self) -> Ident {
754 Ident::new(Symbol::intern(self.as_str().trim_start_matches('\'')), self.span)
757 /// "Normalize" ident for use in comparisons using "item hygiene".
758 /// Identifiers with same string value become same if they came from the same "modern" macro
759 /// (e.g., `macro` item, but not `macro_rules` item) and stay different if they came from
760 /// different "modern" macros.
761 /// Technically, this operation strips all non-opaque marks from ident's syntactic context.
762 pub fn modern(self) -> Ident {
763 Ident::new(self.name, self.span.modern())
766 /// "Normalize" ident for use in comparisons using "local variable hygiene".
767 /// Identifiers with same string value become same if they came from the same non-transparent
768 /// macro (e.g., `macro` or `macro_rules!` items) and stay different if they came from different
769 /// non-transparent macros.
770 /// Technically, this operation strips all transparent marks from ident's syntactic context.
771 pub fn modern_and_legacy(self) -> Ident {
772 Ident::new(self.name, self.span.modern_and_legacy())
775 /// Transforms an identifier into one with the same name, but gensymed.
776 pub fn gensym(self) -> Ident {
777 let name = with_interner(|interner| interner.gensymed(self.name));
778 Ident::new(name, self.span)
781 /// Transforms an underscore identifier into one with the same name, but
782 /// gensymed. Leaves non-underscore identifiers unchanged.
783 pub fn gensym_if_underscore(self) -> Ident {
784 if self.name == kw::Underscore { self.gensym() } else { self }
787 // WARNING: this function is deprecated and will be removed in the future.
788 pub fn is_gensymed(self) -> bool {
789 with_interner(|interner| interner.is_gensymed(self.name))
792 pub fn as_str(self) -> LocalInternedString {
796 pub fn as_interned_str(self) -> InternedString {
797 self.name.as_interned_str()
801 impl PartialEq for Ident {
802 fn eq(&self, rhs: &Self) -> bool {
803 self.name == rhs.name && self.span.ctxt() == rhs.span.ctxt()
807 impl Hash for Ident {
808 fn hash<H: Hasher>(&self, state: &mut H) {
809 self.name.hash(state);
810 self.span.ctxt().hash(state);
814 impl fmt::Debug for Ident {
815 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
816 write!(f, "{}{:?}", self.name, self.span.ctxt())
820 impl fmt::Display for Ident {
821 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
822 fmt::Display::fmt(&self.name, f)
826 impl Encodable for Ident {
827 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
828 if self.span.ctxt().modern() == SyntaxContext::empty() {
829 s.emit_str(&self.as_str())
830 } else { // FIXME(jseyfried): intercrate hygiene
831 let mut string = "#".to_owned();
832 string.push_str(&self.as_str());
838 impl Decodable for Ident {
839 fn decode<D: Decoder>(d: &mut D) -> Result<Ident, D::Error> {
840 let string = d.read_str()?;
841 Ok(if !string.starts_with('#') {
842 Ident::from_str(&string)
843 } else { // FIXME(jseyfried): intercrate hygiene
844 Ident::from_str(&string[1..]).gensym()
849 /// A symbol is an interned or gensymed string. A gensym is a symbol that is
850 /// never equal to any other symbol.
852 /// Conceptually, a gensym can be thought of as a normal symbol with an
853 /// invisible unique suffix. Gensyms are useful when creating new identifiers
854 /// that must not match any existing identifiers, e.g. during macro expansion
855 /// and syntax desugaring. Because gensyms should always be identifiers, all
856 /// gensym operations are on `Ident` rather than `Symbol`. (Indeed, in the
857 /// future the gensym-ness may be moved from `Symbol` to hygiene data.)
861 /// assert_eq!(Ident::from_str("x"), Ident::from_str("x"))
862 /// assert_ne!(Ident::from_str("x").gensym(), Ident::from_str("x"))
863 /// assert_ne!(Ident::from_str("x").gensym(), Ident::from_str("x").gensym())
865 /// Internally, a symbol is implemented as an index, and all operations
866 /// (including hashing, equality, and ordering) operate on that index. The use
867 /// of `newtype_index!` means that `Option<Symbol>` only takes up 4 bytes,
868 /// because `newtype_index!` reserves the last 256 values for tagging purposes.
870 /// Note that `Symbol` cannot directly be a `newtype_index!` because it
871 /// implements `fmt::Debug`, `Encodable`, and `Decodable` in special ways.
872 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
873 pub struct Symbol(SymbolIndex);
876 pub struct SymbolIndex { .. }
880 const fn new(n: u32) -> Self {
881 Symbol(SymbolIndex::from_u32_const(n))
884 /// Maps a string to its interned representation.
885 pub fn intern(string: &str) -> Self {
886 with_interner(|interner| interner.intern(string))
889 pub fn as_str(self) -> LocalInternedString {
890 with_interner(|interner| unsafe {
891 LocalInternedString {
892 string: std::mem::transmute::<&str, &str>(interner.get(self))
897 pub fn as_interned_str(self) -> InternedString {
898 with_interner(|interner| InternedString {
899 symbol: interner.interned(self)
903 pub fn as_u32(self) -> u32 {
908 impl fmt::Debug for Symbol {
909 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
910 let is_gensymed = with_interner(|interner| interner.is_gensymed(*self));
912 write!(f, "{}({:?})", self, self.0)
914 write!(f, "{}", self)
919 impl fmt::Display for Symbol {
920 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
921 fmt::Display::fmt(&self.as_str(), f)
925 impl Encodable for Symbol {
926 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
927 s.emit_str(&self.as_str())
931 impl Decodable for Symbol {
932 fn decode<D: Decoder>(d: &mut D) -> Result<Symbol, D::Error> {
933 Ok(Symbol::intern(&d.read_str()?))
937 // The `&'static str`s in this type actually point into the arena.
939 // Note that normal symbols are indexed upward from 0, and gensyms are indexed
940 // downward from SymbolIndex::MAX_AS_U32.
942 pub struct Interner {
943 arena: DroplessArena,
944 names: FxHashMap<&'static str, Symbol>,
945 strings: Vec<&'static str>,
946 gensyms: Vec<Symbol>,
950 fn prefill(init: &[&'static str]) -> Self {
952 strings: init.into(),
953 names: init.iter().copied().zip((0..).map(Symbol::new)).collect(),
958 pub fn intern(&mut self, string: &str) -> Symbol {
959 if let Some(&name) = self.names.get(string) {
963 let name = Symbol::new(self.strings.len() as u32);
965 // `from_utf8_unchecked` is safe since we just allocated a `&str` which is known to be
967 let string: &str = unsafe {
968 str::from_utf8_unchecked(self.arena.alloc_slice(string.as_bytes()))
970 // It is safe to extend the arena allocation to `'static` because we only access
971 // these while the arena is still alive.
972 let string: &'static str = unsafe {
973 &*(string as *const str)
975 self.strings.push(string);
976 self.names.insert(string, name);
980 fn interned(&self, symbol: Symbol) -> Symbol {
981 if (symbol.0.as_usize()) < self.strings.len() {
984 self.gensyms[(SymbolIndex::MAX_AS_U32 - symbol.0.as_u32()) as usize]
988 fn gensymed(&mut self, symbol: Symbol) -> Symbol {
989 self.gensyms.push(symbol);
990 Symbol::new(SymbolIndex::MAX_AS_U32 - self.gensyms.len() as u32 + 1)
993 fn is_gensymed(&mut self, symbol: Symbol) -> bool {
994 symbol.0.as_usize() >= self.strings.len()
997 // Get the symbol as a string. `Symbol::as_str()` should be used in
998 // preference to this function.
999 pub fn get(&self, symbol: Symbol) -> &str {
1000 match self.strings.get(symbol.0.as_usize()) {
1001 Some(string) => string,
1003 let symbol = self.gensyms[(SymbolIndex::MAX_AS_U32 - symbol.0.as_u32()) as usize];
1004 self.strings[symbol.0.as_usize()]
1010 // This module has a very short name because it's used a lot.
1016 // This module has a very short name because it's used a lot.
1018 use std::convert::TryInto;
1023 // Get the symbol for an integer. The first few non-negative integers each
1024 // have a static symbol and therefore are fast.
1025 pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
1026 if let Result::Ok(idx) = n.try_into() {
1027 if let Option::Some(&sym) = digits_array.get(idx) {
1031 Symbol::intern(&n.to_string())
1036 fn is_used_keyword_2018(self) -> bool {
1040 fn is_unused_keyword_2018(self) -> bool {
1041 self >= kw::Async && self <= kw::Try
1044 /// Used for sanity checking rustdoc keyword sections.
1045 pub fn is_doc_keyword(self) -> bool {
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 == kw::Super ||
1052 self == kw::SelfLower ||
1053 self == kw::SelfUpper ||
1054 self == kw::Crate ||
1055 self == kw::PathRoot ||
1056 self == kw::DollarCrate
1059 /// This symbol can be a raw identifier.
1060 pub fn can_be_raw(self) -> bool {
1061 self != kw::Invalid && self != kw::Underscore && !self.is_path_segment_keyword()
1066 // Returns `true` for reserved identifiers used internally for elided lifetimes,
1067 // unnamed method parameters, crate root module, error recovery etc.
1068 pub fn is_special(self) -> bool {
1069 self.name <= kw::Underscore
1072 /// Returns `true` if the token is a keyword used in the language.
1073 pub fn is_used_keyword(self) -> bool {
1074 // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
1075 self.name >= kw::As && self.name <= kw::While ||
1076 self.name.is_used_keyword_2018() && self.span.rust_2018()
1079 /// Returns `true` if the token is a keyword reserved for possible future use.
1080 pub fn is_unused_keyword(self) -> bool {
1081 // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
1082 self.name >= kw::Abstract && self.name <= kw::Yield ||
1083 self.name.is_unused_keyword_2018() && self.span.rust_2018()
1086 /// Returns `true` if the token is either a special identifier or a keyword.
1087 pub fn is_reserved(self) -> bool {
1088 self.is_special() || self.is_used_keyword() || self.is_unused_keyword()
1091 /// A keyword or reserved identifier that can be used as a path segment.
1092 pub fn is_path_segment_keyword(self) -> bool {
1093 self.name.is_path_segment_keyword()
1096 /// We see this identifier in a normal identifier position, like variable name or a type.
1097 /// How was it written originally? Did it use the raw form? Let's try to guess.
1098 pub fn is_raw_guess(self) -> bool {
1099 self.name.can_be_raw() && self.is_reserved()
1103 // If an interner exists, return it. Otherwise, prepare a fresh one.
1105 fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {
1106 GLOBALS.with(|globals| f(&mut *globals.symbol_interner.lock()))
1109 /// An alternative to `Symbol` and `InternedString`, useful when the chars
1110 /// within the symbol need to be accessed. It is best used for temporary
1113 /// Because the interner outlives any thread which uses this type, we can
1114 /// safely treat `string` which points to interner data, as an immortal string,
1115 /// as long as this type never crosses between threads.
1117 // FIXME: ensure that the interner outlives any thread which uses
1118 // `LocalInternedString`, by creating a new thread right after constructing the
1120 #[derive(Clone, Copy, Hash, PartialOrd, Eq, Ord)]
1121 pub struct LocalInternedString {
1122 string: &'static str,
1125 impl LocalInternedString {
1126 /// Maps a string to its interned representation.
1127 pub fn intern(string: &str) -> Self {
1128 let string = with_interner(|interner| {
1129 let symbol = interner.intern(string);
1130 interner.strings[symbol.0.as_usize()]
1132 LocalInternedString {
1133 string: unsafe { std::mem::transmute::<&str, &str>(string) }
1137 pub fn as_interned_str(self) -> InternedString {
1139 symbol: Symbol::intern(self.string)
1144 pub fn get(&self) -> &str {
1145 // This returns a valid string since we ensure that `self` outlives the interner
1146 // by creating the interner on a thread which outlives threads which can access it.
1147 // This type cannot move to a thread which outlives the interner since it does
1148 // not implement Send.
1153 impl<U: ?Sized> std::convert::AsRef<U> for LocalInternedString
1155 str: std::convert::AsRef<U>
1158 fn as_ref(&self) -> &U {
1159 self.string.as_ref()
1163 impl<T: std::ops::Deref<Target = str>> std::cmp::PartialEq<T> for LocalInternedString {
1164 fn eq(&self, other: &T) -> bool {
1165 self.string == other.deref()
1169 impl std::cmp::PartialEq<LocalInternedString> for str {
1170 fn eq(&self, other: &LocalInternedString) -> bool {
1171 self == other.string
1175 impl<'a> std::cmp::PartialEq<LocalInternedString> for &'a str {
1176 fn eq(&self, other: &LocalInternedString) -> bool {
1177 *self == other.string
1181 impl std::cmp::PartialEq<LocalInternedString> for String {
1182 fn eq(&self, other: &LocalInternedString) -> bool {
1183 self == other.string
1187 impl<'a> std::cmp::PartialEq<LocalInternedString> for &'a String {
1188 fn eq(&self, other: &LocalInternedString) -> bool {
1189 *self == other.string
1193 impl !Send for LocalInternedString {}
1194 impl !Sync for LocalInternedString {}
1196 impl std::ops::Deref for LocalInternedString {
1199 fn deref(&self) -> &str { self.string }
1202 impl fmt::Debug for LocalInternedString {
1203 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1204 fmt::Debug::fmt(self.string, f)
1208 impl fmt::Display for LocalInternedString {
1209 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1210 fmt::Display::fmt(self.string, f)
1214 impl Decodable for LocalInternedString {
1215 fn decode<D: Decoder>(d: &mut D) -> Result<LocalInternedString, D::Error> {
1216 Ok(LocalInternedString::intern(&d.read_str()?))
1220 impl Encodable for LocalInternedString {
1221 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
1222 s.emit_str(self.string)
1226 /// An alternative to `Symbol` that is focused on string contents. It has two
1227 /// main differences to `Symbol`.
1229 /// First, its implementations of `Hash`, `PartialOrd` and `Ord` work with the
1230 /// string chars rather than the symbol integer. This is useful when hash
1231 /// stability is required across compile sessions, or a guaranteed sort
1232 /// ordering is required.
1234 /// Second, gensym-ness is irrelevant. E.g.:
1236 /// assert_ne!(Symbol::gensym("x"), Symbol::gensym("x"))
1237 /// assert_eq!(Symbol::gensym("x").as_interned_str(), Symbol::gensym("x").as_interned_str())
1239 #[derive(Clone, Copy, PartialEq, Eq)]
1240 pub struct InternedString {
1244 impl InternedString {
1245 /// Maps a string to its interned representation.
1246 pub fn intern(string: &str) -> Self {
1248 symbol: Symbol::intern(string)
1252 pub fn with<F: FnOnce(&str) -> R, R>(self, f: F) -> R {
1253 let str = with_interner(|interner| {
1254 interner.get(self.symbol) as *const str
1256 // This is safe because the interner keeps string alive until it is dropped.
1257 // We can access it because we know the interner is still alive since we use a
1258 // scoped thread local to access it, and it was alive at the beginning of this scope
1262 fn with2<F: FnOnce(&str, &str) -> R, R>(self, other: &InternedString, f: F) -> R {
1263 let (self_str, other_str) = with_interner(|interner| {
1264 (interner.get(self.symbol) as *const str,
1265 interner.get(other.symbol) as *const str)
1267 // This is safe for the same reason that `with` is safe.
1268 unsafe { f(&*self_str, &*other_str) }
1271 pub fn as_symbol(self) -> Symbol {
1275 pub fn as_str(self) -> LocalInternedString {
1276 self.symbol.as_str()
1280 impl Hash for InternedString {
1281 fn hash<H: Hasher>(&self, state: &mut H) {
1282 self.with(|str| str.hash(state))
1286 impl PartialOrd<InternedString> for InternedString {
1287 fn partial_cmp(&self, other: &InternedString) -> Option<Ordering> {
1288 if self.symbol == other.symbol {
1289 return Some(Ordering::Equal);
1291 self.with2(other, |self_str, other_str| self_str.partial_cmp(other_str))
1295 impl Ord for InternedString {
1296 fn cmp(&self, other: &InternedString) -> Ordering {
1297 if self.symbol == other.symbol {
1298 return Ordering::Equal;
1300 self.with2(other, |self_str, other_str| self_str.cmp(other_str))
1304 impl fmt::Debug for InternedString {
1305 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1306 self.with(|str| fmt::Debug::fmt(&str, f))
1310 impl fmt::Display for InternedString {
1311 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1312 self.with(|str| fmt::Display::fmt(&str, f))
1316 impl Decodable for InternedString {
1317 fn decode<D: Decoder>(d: &mut D) -> Result<InternedString, D::Error> {
1318 Ok(InternedString::intern(&d.read_str()?))
1322 impl Encodable for InternedString {
1323 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
1324 self.with(|string| s.emit_str(string))
1335 fn interner_tests() {
1336 let mut i: Interner = Interner::default();
1337 // first one is zero:
1338 assert_eq!(i.intern("dog"), Symbol::new(0));
1339 // re-use gets the same entry:
1340 assert_eq!(i.intern("dog"), Symbol::new(0));
1341 // different string gets a different #:
1342 assert_eq!(i.intern("cat"), Symbol::new(1));
1343 assert_eq!(i.intern("cat"), Symbol::new(1));
1344 // dog is still at zero
1345 assert_eq!(i.intern("dog"), Symbol::new(0));
1346 let z = i.intern("zebra");
1347 assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32));
1348 // gensym of same string gets new number:
1349 assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32 - 1));
1350 // gensym of *existing* string gets new number:
1351 let d = i.intern("dog");
1352 assert_eq!(i.gensymed(d), Symbol::new(SymbolIndex::MAX_AS_U32 - 2));
1356 fn without_first_quote_test() {
1357 GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || {
1358 let i = Ident::from_str("'break");
1359 assert_eq!(i.without_first_quote().name, kw::Break);