]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_pos/symbol.rs
Auto merge of #66507 - ecstatic-morse:const-if-match, r=oli-obk
[rust.git] / src / libsyntax_pos / symbol.rs
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.
4
5 use arena::DroplessArena;
6 use rustc_data_structures::fx::FxHashMap;
7 use rustc_index::vec::Idx;
8 use rustc_macros::symbols;
9 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
10 use rustc_serialize::{UseSpecializedDecodable, UseSpecializedEncodable};
11 use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHasher};
12
13 use std::cmp::{PartialEq, PartialOrd, Ord};
14 use std::fmt;
15 use std::hash::{Hash, Hasher};
16 use std::str;
17
18 use crate::{Span, DUMMY_SP, GLOBALS};
19
20 #[cfg(test)]
21 mod tests;
22
23 symbols! {
24     // After modifying this list adjust `is_special`, `is_used_keyword`/`is_unused_keyword`,
25     // this should be rarely necessary though if the keywords are kept in alphabetic order.
26     Keywords {
27         // Special reserved identifiers used internally for elided lifetimes,
28         // unnamed method parameters, crate root module, error recovery etc.
29         Invalid:            "",
30         PathRoot:           "{{root}}",
31         DollarCrate:        "$crate",
32         Underscore:         "_",
33
34         // Keywords that are used in stable Rust.
35         As:                 "as",
36         Break:              "break",
37         Const:              "const",
38         Continue:           "continue",
39         Crate:              "crate",
40         Else:               "else",
41         Enum:               "enum",
42         Extern:             "extern",
43         False:              "false",
44         Fn:                 "fn",
45         For:                "for",
46         If:                 "if",
47         Impl:               "impl",
48         In:                 "in",
49         Let:                "let",
50         Loop:               "loop",
51         Match:              "match",
52         Mod:                "mod",
53         Move:               "move",
54         Mut:                "mut",
55         Pub:                "pub",
56         Ref:                "ref",
57         Return:             "return",
58         SelfLower:          "self",
59         SelfUpper:          "Self",
60         Static:             "static",
61         Struct:             "struct",
62         Super:              "super",
63         Trait:              "trait",
64         True:               "true",
65         Type:               "type",
66         Unsafe:             "unsafe",
67         Use:                "use",
68         Where:              "where",
69         While:              "while",
70
71         // Keywords that are used in unstable Rust or reserved for future use.
72         Abstract:           "abstract",
73         Become:             "become",
74         Box:                "box",
75         Do:                 "do",
76         Final:              "final",
77         Macro:              "macro",
78         Override:           "override",
79         Priv:               "priv",
80         Typeof:             "typeof",
81         Unsized:            "unsized",
82         Virtual:            "virtual",
83         Yield:              "yield",
84
85         // Edition-specific keywords that are used in stable Rust.
86         Async:              "async", // >= 2018 Edition only
87         Await:              "await", // >= 2018 Edition only
88         Dyn:                "dyn", // >= 2018 Edition only
89
90         // Edition-specific keywords that are used in unstable Rust or reserved for future use.
91         Try:                "try", // >= 2018 Edition only
92
93         // Special lifetime names
94         UnderscoreLifetime: "'_",
95         StaticLifetime:     "'static",
96
97         // Weak keywords, have special meaning only in specific contexts.
98         Auto:               "auto",
99         Catch:              "catch",
100         Default:            "default",
101         Union:              "union",
102     }
103
104     // Symbols that can be referred to with syntax_pos::sym::*. The symbol is
105     // the stringified identifier unless otherwise specified (e.g.
106     // `proc_dash_macro` represents "proc-macro").
107     //
108     // As well as the symbols listed, there are symbols for the the strings
109     // "0", "1", ..., "9", which are accessible via `sym::integer`.
110     Symbols {
111         aarch64_target_feature,
112         abi,
113         abi_amdgpu_kernel,
114         abi_efiapi,
115         abi_msp430_interrupt,
116         abi_ptx,
117         abi_sysv64,
118         abi_thiscall,
119         abi_unadjusted,
120         abi_vectorcall,
121         abi_x86_interrupt,
122         aborts,
123         advanced_slice_patterns,
124         adx_target_feature,
125         alias,
126         align,
127         alignstack,
128         all,
129         allocator,
130         allocator_internals,
131         alloc_error_handler,
132         allow,
133         allowed,
134         allow_fail,
135         allow_internal_unsafe,
136         allow_internal_unstable,
137         allow_internal_unstable_backcompat_hack,
138         always,
139         and,
140         any,
141         arbitrary_enum_discriminant,
142         arbitrary_self_types,
143         Arguments,
144         ArgumentV1,
145         arm_target_feature,
146         asm,
147         assert,
148         associated_consts,
149         associated_type_bounds,
150         associated_type_defaults,
151         associated_types,
152         assume_init,
153         async_await,
154         async_closure,
155         attr,
156         attributes,
157         attr_literals,
158         augmented_assignments,
159         automatically_derived,
160         avx512_target_feature,
161         await_macro,
162         begin_panic,
163         bench,
164         bin,
165         bind_by_move_pattern_guards,
166         block,
167         bool,
168         borrowck_graphviz_postflow,
169         borrowck_graphviz_preflow,
170         box_patterns,
171         box_syntax,
172         braced_empty_structs,
173         C,
174         cdylib,
175         cfg,
176         cfg_attr,
177         cfg_attr_multi,
178         cfg_doctest,
179         cfg_target_feature,
180         cfg_target_has_atomic,
181         cfg_target_thread_local,
182         cfg_target_vendor,
183         char,
184         clippy,
185         clone,
186         Clone,
187         clone_closures,
188         clone_from,
189         closure_to_fn_coercion,
190         cmp,
191         cmpxchg16b_target_feature,
192         cold,
193         column,
194         compile_error,
195         compiler_builtins,
196         concat,
197         concat_idents,
198         conservative_impl_trait,
199         console,
200         const_compare_raw_pointers,
201         const_constructor,
202         const_extern_fn,
203         const_fn,
204         const_fn_union,
205         const_generics,
206         const_if_match,
207         const_indexing,
208         const_in_array_repeat_expressions,
209         const_let,
210         const_panic,
211         const_raw_ptr_deref,
212         const_raw_ptr_to_usize_cast,
213         const_transmute,
214         contents,
215         context,
216         convert,
217         Copy,
218         copy_closures,
219         core,
220         core_intrinsics,
221         crate_id,
222         crate_in_paths,
223         crate_local,
224         crate_name,
225         crate_type,
226         crate_visibility_modifier,
227         custom_attribute,
228         custom_derive,
229         custom_inner_attributes,
230         custom_test_frameworks,
231         c_variadic,
232         debug_trait,
233         declare_lint_pass,
234         decl_macro,
235         Debug,
236         Decodable,
237         Default,
238         default_lib_allocator,
239         default_type_parameter_fallback,
240         default_type_params,
241         delay_span_bug_from_inside_query,
242         deny,
243         deprecated,
244         deref,
245         deref_mut,
246         derive,
247         diagnostic,
248         direct,
249         doc,
250         doc_alias,
251         doc_cfg,
252         doc_keyword,
253         doc_masked,
254         doc_spotlight,
255         doctest,
256         document_private_items,
257         dotdoteq_in_patterns,
258         dotdot_in_tuple_patterns,
259         double_braced_crate: "{{crate}}",
260         double_braced_impl: "{{impl}}",
261         double_braced_misc: "{{misc}}",
262         double_braced_closure: "{{closure}}",
263         double_braced_constructor: "{{constructor}}",
264         double_braced_constant: "{{constant}}",
265         double_braced_opaque: "{{opaque}}",
266         dropck_eyepatch,
267         dropck_parametricity,
268         drop_types_in_const,
269         dylib,
270         dyn_trait,
271         eh_personality,
272         eh_unwind_resume,
273         enable,
274         Encodable,
275         env,
276         eq,
277         err,
278         Err,
279         Eq,
280         Equal,
281         except,
282         exclusive_range_pattern,
283         exhaustive_integer_patterns,
284         exhaustive_patterns,
285         existential_type,
286         expected,
287         export_name,
288         expr,
289         extern_absolute_paths,
290         external_doc,
291         extern_crate_item_prelude,
292         extern_crate_self,
293         extern_in_paths,
294         extern_prelude,
295         extern_types,
296         f16c_target_feature,
297         f32,
298         f64,
299         feature,
300         ffi_returns_twice,
301         field,
302         field_init_shorthand,
303         file,
304         fmt,
305         fmt_internals,
306         fn_must_use,
307         forbid,
308         format_args,
309         format_args_nl,
310         from,
311         From,
312         from_desugaring,
313         from_error,
314         from_generator,
315         from_method,
316         from_ok,
317         from_usize,
318         fundamental,
319         future,
320         Future,
321         FxHashSet,
322         FxHashMap,
323         gen_future,
324         generators,
325         generic_associated_types,
326         generic_param_attrs,
327         global_allocator,
328         global_asm,
329         globs,
330         hash,
331         Hash,
332         HashSet,
333         HashMap,
334         hexagon_target_feature,
335         hidden,
336         homogeneous_aggregate,
337         html_favicon_url,
338         html_logo_url,
339         html_no_source,
340         html_playground_url,
341         html_root_url,
342         i128,
343         i128_type,
344         i16,
345         i32,
346         i64,
347         i8,
348         ident,
349         if_let,
350         if_while_or_patterns,
351         ignore,
352         impl_header_lifetime_elision,
353         impl_lint_pass,
354         impl_trait_in_bindings,
355         import_shadowing,
356         index,
357         index_mut,
358         in_band_lifetimes,
359         include,
360         include_bytes,
361         include_str,
362         inclusive_range_syntax,
363         infer_outlives_requirements,
364         infer_static_outlives_requirements,
365         inline,
366         intel,
367         into_iter,
368         IntoIterator,
369         into_result,
370         intrinsics,
371         irrefutable_let_patterns,
372         isize,
373         issue,
374         issue_5723_bootstrap,
375         issue_tracker_base_url,
376         item,
377         item_context: "ItemContext",
378         item_like_imports,
379         iter,
380         Iterator,
381         keyword,
382         kind,
383         label,
384         label_break_value,
385         lang,
386         lang_items,
387         let_chains,
388         lhs,
389         lib,
390         lifetime,
391         line,
392         link,
393         linkage,
394         link_args,
395         link_cfg,
396         link_llvm_intrinsics,
397         link_name,
398         link_ordinal,
399         link_section,
400         LintPass,
401         lint_reasons,
402         literal,
403         local_inner_macros,
404         log_syntax,
405         loop_break_value,
406         macro_at_most_once_rep,
407         macro_escape,
408         macro_export,
409         macro_lifetime_matcher,
410         macro_literal_matcher,
411         macro_reexport,
412         macro_rules,
413         macros_in_extern,
414         macro_use,
415         macro_vis_matcher,
416         main,
417         managed_boxes,
418         marker,
419         marker_trait_attr,
420         masked,
421         match_beginning_vert,
422         match_default_bindings,
423         may_dangle,
424         maybe_uninit_uninit,
425         maybe_uninit_zeroed,
426         mem_uninitialized,
427         mem_zeroed,
428         member_constraints,
429         message,
430         meta,
431         min_const_fn,
432         min_const_unsafe_fn,
433         mips_target_feature,
434         mmx_target_feature,
435         module,
436         module_path,
437         more_struct_aliases,
438         move_val_init,
439         movbe_target_feature,
440         must_use,
441         naked,
442         naked_functions,
443         name,
444         needs_allocator,
445         needs_panic_runtime,
446         negate_unsigned,
447         never,
448         never_type,
449         never_type_fallback,
450         new,
451         next,
452         __next,
453         nll,
454         no_builtins,
455         no_core,
456         no_crate_inject,
457         no_debug,
458         no_default_passes,
459         no_implicit_prelude,
460         no_inline,
461         no_link,
462         no_main,
463         no_mangle,
464         non_ascii_idents,
465         None,
466         non_exhaustive,
467         non_modrs_mods,
468         no_stack_check,
469         no_start,
470         no_std,
471         not,
472         note,
473         object_safe_for_dispatch,
474         Ok,
475         omit_gdb_pretty_printer_section,
476         on,
477         on_unimplemented,
478         oom,
479         ops,
480         optimize,
481         optimize_attribute,
482         optin_builtin_traits,
483         option,
484         Option,
485         option_env,
486         opt_out_copy,
487         or,
488         or_patterns,
489         Ord,
490         Ordering,
491         Output,
492         overlapping_marker_traits,
493         packed,
494         panic,
495         panic_handler,
496         panic_impl,
497         panic_implementation,
498         panic_runtime,
499         parent_trait,
500         partial_cmp,
501         param_attrs,
502         PartialEq,
503         PartialOrd,
504         passes,
505         pat,
506         path,
507         pattern_parentheses,
508         Pending,
509         pin,
510         Pin,
511         pinned,
512         platform_intrinsics,
513         plugin,
514         plugin_registrar,
515         plugins,
516         Poll,
517         poll_with_tls_context,
518         powerpc_target_feature,
519         precise_pointer_size_matching,
520         prelude,
521         prelude_import,
522         primitive,
523         proc_dash_macro: "proc-macro",
524         proc_macro,
525         proc_macro_attribute,
526         proc_macro_def_site,
527         proc_macro_derive,
528         proc_macro_expr,
529         proc_macro_gen,
530         proc_macro_hygiene,
531         proc_macro_internals,
532         proc_macro_mod,
533         proc_macro_non_items,
534         proc_macro_path_invoc,
535         profiler_runtime,
536         pub_restricted,
537         pushpop_unsafe,
538         quad_precision_float,
539         question_mark,
540         quote,
541         Range,
542         RangeFrom,
543         RangeFull,
544         RangeInclusive,
545         RangeTo,
546         RangeToInclusive,
547         raw_dylib,
548         raw_identifiers,
549         Ready,
550         reason,
551         recursion_limit,
552         reexport_test_harness_main,
553         reflect,
554         register_attr,
555         register_tool,
556         relaxed_adts,
557         repr,
558         repr128,
559         repr_align,
560         repr_align_enum,
561         repr_packed,
562         repr_simd,
563         repr_transparent,
564         re_rebalance_coherence,
565         result,
566         Result,
567         Return,
568         rhs,
569         rlib,
570         rt,
571         rtm_target_feature,
572         rust,
573         rust_2015_preview,
574         rust_2018_preview,
575         rust_begin_unwind,
576         rustc,
577         RustcDecodable,
578         RustcEncodable,
579         rustc_allocator,
580         rustc_allocator_nounwind,
581         rustc_allow_const_fn_ptr,
582         rustc_args_required_const,
583         rustc_attrs,
584         rustc_builtin_macro,
585         rustc_clean,
586         rustc_const_unstable,
587         rustc_conversion_suggestion,
588         rustc_def_path,
589         rustc_deprecated,
590         rustc_diagnostic_item,
591         rustc_diagnostic_macros,
592         rustc_dirty,
593         rustc_dummy,
594         rustc_dump_env_program_clauses,
595         rustc_dump_program_clauses,
596         rustc_dump_user_substs,
597         rustc_error,
598         rustc_expected_cgu_reuse,
599         rustc_if_this_changed,
600         rustc_inherit_overflow_checks,
601         rustc_layout,
602         rustc_layout_scalar_valid_range_end,
603         rustc_layout_scalar_valid_range_start,
604         rustc_macro_transparency,
605         rustc_mir,
606         rustc_nonnull_optimization_guaranteed,
607         rustc_object_lifetime_default,
608         rustc_on_unimplemented,
609         rustc_outlives,
610         rustc_paren_sugar,
611         rustc_partition_codegened,
612         rustc_partition_reused,
613         rustc_peek,
614         rustc_peek_definite_init,
615         rustc_peek_maybe_init,
616         rustc_peek_maybe_uninit,
617         rustc_peek_indirectly_mutable,
618         rustc_private,
619         rustc_proc_macro_decls,
620         rustc_promotable,
621         rustc_regions,
622         rustc_stable,
623         rustc_std_internal_symbol,
624         rustc_symbol_name,
625         rustc_synthetic,
626         rustc_reservation_impl,
627         rustc_test_marker,
628         rustc_then_this_would_need,
629         rustc_variance,
630         rustdoc,
631         rustfmt,
632         rust_eh_personality,
633         rust_eh_unwind_resume,
634         rust_oom,
635         rvalue_static_promotion,
636         sanitizer_runtime,
637         _Self,
638         self_in_typedefs,
639         self_struct_ctor,
640         should_panic,
641         simd,
642         simd_ffi,
643         since,
644         size,
645         slice_patterns,
646         slicing_syntax,
647         soft,
648         Some,
649         specialization,
650         speed,
651         spotlight,
652         sse4a_target_feature,
653         stable,
654         staged_api,
655         start,
656         static_in_const,
657         staticlib,
658         static_nobundle,
659         static_recursion,
660         std,
661         std_inject,
662         str,
663         stringify,
664         stmt,
665         stmt_expr_attributes,
666         stop_after_dataflow,
667         struct_field_attributes,
668         struct_inherit,
669         structural_match,
670         struct_variant,
671         sty,
672         suggestion,
673         target_feature,
674         target_has_atomic,
675         target_has_atomic_load_store,
676         target_thread_local,
677         task,
678         tbm_target_feature,
679         termination_trait,
680         termination_trait_test,
681         test,
682         test_2018_feature,
683         test_accepted_feature,
684         test_case,
685         test_removed_feature,
686         test_runner,
687         then_with,
688         thread_local,
689         tool_attributes,
690         tool_lints,
691         trace_macros,
692         track_caller,
693         trait_alias,
694         transmute,
695         transparent,
696         transparent_enums,
697         transparent_unions,
698         trivial_bounds,
699         Try,
700         try_blocks,
701         try_trait,
702         tt,
703         tuple_indexing,
704         Ty,
705         ty,
706         type_alias_impl_trait,
707         TyCtxt,
708         TyKind,
709         type_alias_enum_variants,
710         type_ascription,
711         type_length_limit,
712         type_macros,
713         u128,
714         u16,
715         u32,
716         u64,
717         u8,
718         unboxed_closures,
719         underscore_const_names,
720         underscore_imports,
721         underscore_lifetimes,
722         uniform_paths,
723         universal_impl_trait,
724         unmarked_api,
725         unreachable_code,
726         unrestricted_attribute_tokens,
727         unsafe_no_drop_flag,
728         unsized_locals,
729         unsized_tuple_coercion,
730         unstable,
731         untagged_unions,
732         unwind,
733         unwind_attributes,
734         unwrap_or,
735         used,
736         use_extern_macros,
737         use_nested_groups,
738         usize,
739         v1,
740         val,
741         vec,
742         Vec,
743         vis,
744         visible_private_types,
745         volatile,
746         warn,
747         wasm_import_module,
748         wasm_target_feature,
749         while_let,
750         windows,
751         windows_subsystem,
752         Yield,
753     }
754 }
755
756 #[derive(Copy, Clone, Eq)]
757 pub struct Ident {
758     pub name: Symbol,
759     pub span: Span,
760 }
761
762 impl Ident {
763     #[inline]
764     /// Constructs a new identifier from a symbol and a span.
765     pub const fn new(name: Symbol, span: Span) -> Ident {
766         Ident { name, span }
767     }
768
769     /// Constructs a new identifier with a dummy span.
770     #[inline]
771     pub const fn with_dummy_span(name: Symbol) -> Ident {
772         Ident::new(name, DUMMY_SP)
773     }
774
775     #[inline]
776     pub fn invalid() -> Ident {
777         Ident::with_dummy_span(kw::Invalid)
778     }
779
780     /// Maps a string to an identifier with a dummy span.
781     pub fn from_str(string: &str) -> Ident {
782         Ident::with_dummy_span(Symbol::intern(string))
783     }
784
785     /// Maps a string and a span to an identifier.
786     pub fn from_str_and_span(string: &str, span: Span) -> Ident {
787         Ident::new(Symbol::intern(string), span)
788     }
789
790     /// Replaces `lo` and `hi` with those from `span`, but keep hygiene context.
791     pub fn with_span_pos(self, span: Span) -> Ident {
792         Ident::new(self.name, span.with_ctxt(self.span.ctxt()))
793     }
794
795     pub fn without_first_quote(self) -> Ident {
796         Ident::new(Symbol::intern(self.as_str().trim_start_matches('\'')), self.span)
797     }
798
799     /// "Normalize" ident for use in comparisons using "item hygiene".
800     /// Identifiers with same string value become same if they came from the same "modern" macro
801     /// (e.g., `macro` item, but not `macro_rules` item) and stay different if they came from
802     /// different "modern" macros.
803     /// Technically, this operation strips all non-opaque marks from ident's syntactic context.
804     pub fn modern(self) -> Ident {
805         Ident::new(self.name, self.span.modern())
806     }
807
808     /// "Normalize" ident for use in comparisons using "local variable hygiene".
809     /// Identifiers with same string value become same if they came from the same non-transparent
810     /// macro (e.g., `macro` or `macro_rules!` items) and stay different if they came from different
811     /// non-transparent macros.
812     /// Technically, this operation strips all transparent marks from ident's syntactic context.
813     pub fn modern_and_legacy(self) -> Ident {
814         Ident::new(self.name, self.span.modern_and_legacy())
815     }
816
817     /// Convert the name to a `SymbolStr`. This is a slowish operation because
818     /// it requires locking the symbol interner.
819     pub fn as_str(self) -> SymbolStr {
820         self.name.as_str()
821     }
822 }
823
824 impl PartialEq for Ident {
825     fn eq(&self, rhs: &Self) -> bool {
826         self.name == rhs.name && self.span.ctxt() == rhs.span.ctxt()
827     }
828 }
829
830 impl Hash for Ident {
831     fn hash<H: Hasher>(&self, state: &mut H) {
832         self.name.hash(state);
833         self.span.ctxt().hash(state);
834     }
835 }
836
837 impl fmt::Debug for Ident {
838     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
839         write!(f, "{}{:?}", self.name, self.span.ctxt())
840     }
841 }
842
843 impl fmt::Display for Ident {
844     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
845         fmt::Display::fmt(&self.name, f)
846     }
847 }
848
849 impl UseSpecializedEncodable for Ident {
850     fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
851         s.emit_struct("Ident", 2, |s| {
852             s.emit_struct_field("name", 0, |s| {
853                 self.name.encode(s)
854             })?;
855             s.emit_struct_field("span", 1, |s| {
856                 self.span.encode(s)
857             })
858         })
859     }
860 }
861
862 impl UseSpecializedDecodable for Ident {
863     fn default_decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
864         d.read_struct("Ident", 2, |d| {
865             Ok(Ident {
866                 name: d.read_struct_field("name", 0, Decodable::decode)?,
867                 span: d.read_struct_field("span", 1, Decodable::decode)?,
868             })
869         })
870     }
871 }
872
873 /// An interned string.
874 ///
875 /// Internally, a `Symbol` is implemented as an index, and all operations
876 /// (including hashing, equality, and ordering) operate on that index. The use
877 /// of `rustc_index::newtype_index!` means that `Option<Symbol>` only takes up 4 bytes,
878 /// because `rustc_index::newtype_index!` reserves the last 256 values for tagging purposes.
879 ///
880 /// Note that `Symbol` cannot directly be a `rustc_index::newtype_index!` because it
881 /// implements `fmt::Debug`, `Encodable`, and `Decodable` in special ways.
882 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
883 pub struct Symbol(SymbolIndex);
884
885 rustc_index::newtype_index! {
886     pub struct SymbolIndex { .. }
887 }
888
889 impl Symbol {
890     const fn new(n: u32) -> Self {
891         Symbol(SymbolIndex::from_u32_const(n))
892     }
893
894     /// Maps a string to its interned representation.
895     pub fn intern(string: &str) -> Self {
896         with_interner(|interner| interner.intern(string))
897     }
898
899     /// Access the symbol's chars. This is a slowish operation because it
900     /// requires locking the symbol interner.
901     pub fn with<F: FnOnce(&str) -> R, R>(self, f: F) -> R {
902         with_interner(|interner| {
903             f(interner.get(self))
904         })
905     }
906
907     /// Convert to a `SymbolStr`. This is a slowish operation because it
908     /// requires locking the symbol interner.
909     pub fn as_str(self) -> SymbolStr {
910         with_interner(|interner| unsafe {
911             SymbolStr {
912                 string: std::mem::transmute::<&str, &str>(interner.get(self))
913             }
914         })
915     }
916
917     pub fn as_u32(self) -> u32 {
918         self.0.as_u32()
919     }
920 }
921
922 impl fmt::Debug for Symbol {
923     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
924         self.with(|str| fmt::Debug::fmt(&str, f))
925     }
926 }
927
928 impl fmt::Display for Symbol {
929     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
930         self.with(|str| fmt::Display::fmt(&str, f))
931     }
932 }
933
934 impl Encodable for Symbol {
935     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
936         self.with(|string| s.emit_str(string))
937     }
938 }
939
940 impl Decodable for Symbol {
941     fn decode<D: Decoder>(d: &mut D) -> Result<Symbol, D::Error> {
942         Ok(Symbol::intern(&d.read_str()?))
943     }
944 }
945
946 impl<CTX> HashStable<CTX> for Symbol {
947     #[inline]
948     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
949         self.as_str().hash_stable(hcx, hasher);
950     }
951 }
952
953 impl<CTX> ToStableHashKey<CTX> for Symbol {
954     type KeyType = SymbolStr;
955
956     #[inline]
957     fn to_stable_hash_key(&self, _: &CTX) -> SymbolStr {
958         self.as_str()
959     }
960 }
961
962 // The `&'static str`s in this type actually point into the arena.
963 #[derive(Default)]
964 pub struct Interner {
965     arena: DroplessArena,
966     names: FxHashMap<&'static str, Symbol>,
967     strings: Vec<&'static str>,
968 }
969
970 impl Interner {
971     fn prefill(init: &[&'static str]) -> Self {
972         Interner {
973             strings: init.into(),
974             names: init.iter().copied().zip((0..).map(Symbol::new)).collect(),
975             ..Default::default()
976         }
977     }
978
979     pub fn intern(&mut self, string: &str) -> Symbol {
980         if let Some(&name) = self.names.get(string) {
981             return name;
982         }
983
984         let name = Symbol::new(self.strings.len() as u32);
985
986         // `from_utf8_unchecked` is safe since we just allocated a `&str` which is known to be
987         // UTF-8.
988         let string: &str = unsafe {
989             str::from_utf8_unchecked(self.arena.alloc_slice(string.as_bytes()))
990         };
991         // It is safe to extend the arena allocation to `'static` because we only access
992         // these while the arena is still alive.
993         let string: &'static str =  unsafe {
994             &*(string as *const str)
995         };
996         self.strings.push(string);
997         self.names.insert(string, name);
998         name
999     }
1000
1001     // Get the symbol as a string. `Symbol::as_str()` should be used in
1002     // preference to this function.
1003     pub fn get(&self, symbol: Symbol) -> &str {
1004         self.strings[symbol.0.as_usize()]
1005     }
1006 }
1007
1008 // This module has a very short name because it's used a lot.
1009 pub mod kw {
1010     use super::Symbol;
1011     keywords!();
1012 }
1013
1014 // This module has a very short name because it's used a lot.
1015 pub mod sym {
1016     use std::convert::TryInto;
1017     use super::Symbol;
1018
1019     symbols!();
1020
1021     // Get the symbol for an integer. The first few non-negative integers each
1022     // have a static symbol and therefore are fast.
1023     pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
1024         if let Result::Ok(idx) = n.try_into() {
1025             if let Option::Some(&sym) = digits_array.get(idx) {
1026                 return sym;
1027             }
1028         }
1029         Symbol::intern(&n.to_string())
1030     }
1031 }
1032
1033 impl Symbol {
1034     fn is_used_keyword_2018(self) -> bool {
1035         self >= kw::Async && self <= kw::Dyn
1036     }
1037
1038     fn is_unused_keyword_2018(self) -> bool {
1039         self == kw::Try
1040     }
1041
1042     /// Used for sanity checking rustdoc keyword sections.
1043     pub fn is_doc_keyword(self) -> bool {
1044         self <= kw::Union
1045     }
1046
1047     /// A keyword or reserved identifier that can be used as a path segment.
1048     pub fn is_path_segment_keyword(self) -> bool {
1049         self == kw::Super ||
1050         self == kw::SelfLower ||
1051         self == kw::SelfUpper ||
1052         self == kw::Crate ||
1053         self == kw::PathRoot ||
1054         self == kw::DollarCrate
1055     }
1056
1057     /// Returns `true` if the symbol is `true` or `false`.
1058     pub fn is_bool_lit(self) -> bool {
1059         self == kw::True || self == kw::False
1060     }
1061
1062     /// This symbol can be a raw identifier.
1063     pub fn can_be_raw(self) -> bool {
1064         self != kw::Invalid && self != kw::Underscore && !self.is_path_segment_keyword()
1065     }
1066 }
1067
1068 impl Ident {
1069     // Returns `true` for reserved identifiers used internally for elided lifetimes,
1070     // unnamed method parameters, crate root module, error recovery etc.
1071     pub fn is_special(self) -> bool {
1072         self.name <= kw::Underscore
1073     }
1074
1075     /// Returns `true` if the token is a keyword used in the language.
1076     pub fn is_used_keyword(self) -> bool {
1077         // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
1078         self.name >= kw::As && self.name <= kw::While ||
1079         self.name.is_used_keyword_2018() && self.span.rust_2018()
1080     }
1081
1082     /// Returns `true` if the token is a keyword reserved for possible future use.
1083     pub fn is_unused_keyword(self) -> bool {
1084         // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
1085         self.name >= kw::Abstract && self.name <= kw::Yield ||
1086         self.name.is_unused_keyword_2018() && self.span.rust_2018()
1087     }
1088
1089     /// Returns `true` if the token is either a special identifier or a keyword.
1090     pub fn is_reserved(self) -> bool {
1091         self.is_special() || self.is_used_keyword() || self.is_unused_keyword()
1092     }
1093
1094     /// A keyword or reserved identifier that can be used as a path segment.
1095     pub fn is_path_segment_keyword(self) -> bool {
1096         self.name.is_path_segment_keyword()
1097     }
1098
1099     /// We see this identifier in a normal identifier position, like variable name or a type.
1100     /// How was it written originally? Did it use the raw form? Let's try to guess.
1101     pub fn is_raw_guess(self) -> bool {
1102         self.name.can_be_raw() && self.is_reserved()
1103     }
1104 }
1105
1106 #[inline]
1107 fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {
1108     GLOBALS.with(|globals| f(&mut *globals.symbol_interner.lock()))
1109 }
1110
1111 /// An alternative to `Symbol`, useful when the chars within the symbol need to
1112 /// be accessed. It deliberately has limited functionality and should only be
1113 /// used for temporary values.
1114 ///
1115 /// Because the interner outlives any thread which uses this type, we can
1116 /// safely treat `string` which points to interner data, as an immortal string,
1117 /// as long as this type never crosses between threads.
1118 //
1119 // FIXME: ensure that the interner outlives any thread which uses `SymbolStr`,
1120 // by creating a new thread right after constructing the interner.
1121 #[derive(Clone, Eq, PartialOrd, Ord)]
1122 pub struct SymbolStr {
1123     string: &'static str,
1124 }
1125
1126 // This impl allows a `SymbolStr` to be directly equated with a `String` or
1127 // `&str`.
1128 impl<T: std::ops::Deref<Target = str>> std::cmp::PartialEq<T> for SymbolStr {
1129     fn eq(&self, other: &T) -> bool {
1130         self.string == other.deref()
1131     }
1132 }
1133
1134 impl !Send for SymbolStr {}
1135 impl !Sync for SymbolStr {}
1136
1137 /// This impl means that if `ss` is a `SymbolStr`:
1138 /// - `*ss` is a `str`;
1139 /// - `&*ss` is a `&str`;
1140 /// - `&ss as &str` is a `&str`, which means that `&ss` can be passed to a
1141 ///   function expecting a `&str`.
1142 impl std::ops::Deref for SymbolStr {
1143     type Target = str;
1144     #[inline]
1145     fn deref(&self) -> &str { self.string }
1146 }
1147
1148 impl fmt::Debug for SymbolStr {
1149     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1150         fmt::Debug::fmt(self.string, f)
1151     }
1152 }
1153
1154 impl fmt::Display for SymbolStr {
1155     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1156         fmt::Display::fmt(self.string, f)
1157     }
1158 }
1159
1160 impl<CTX> HashStable<CTX> for SymbolStr {
1161     #[inline]
1162     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
1163         self.string.hash_stable(hcx, hasher)
1164     }
1165 }
1166
1167 impl<CTX> ToStableHashKey<CTX> for SymbolStr {
1168     type KeyType = SymbolStr;
1169
1170     #[inline]
1171     fn to_stable_hash_key(&self, _: &CTX) -> SymbolStr {
1172         self.clone()
1173     }
1174 }