]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_span/src/symbol.rs
Test drop_tracking_mir before querying generator.
[rust.git] / compiler / rustc_span / src / 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 rustc_arena::DroplessArena;
6 use rustc_data_structures::fx::FxHashMap;
7 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
8 use rustc_data_structures::sync::Lock;
9 use rustc_macros::HashStable_Generic;
10 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
11
12 use std::fmt;
13 use std::hash::{Hash, Hasher};
14 use std::str;
15
16 use crate::{with_session_globals, Edition, Span, DUMMY_SP};
17
18 #[cfg(test)]
19 mod tests;
20
21 // The proc macro code for this is in `compiler/rustc_macros/src/symbols.rs`.
22 symbols! {
23     // After modifying this list adjust `is_special`, `is_used_keyword`/`is_unused_keyword`,
24     // this should be rarely necessary though if the keywords are kept in alphabetic order.
25     Keywords {
26         // Special reserved identifiers used internally for elided lifetimes,
27         // unnamed method parameters, crate root module, error recovery etc.
28         Empty:              "",
29         PathRoot:           "{{root}}",
30         DollarCrate:        "$crate",
31         Underscore:         "_",
32
33         // Keywords that are used in stable Rust.
34         As:                 "as",
35         Break:              "break",
36         Const:              "const",
37         Continue:           "continue",
38         Crate:              "crate",
39         Else:               "else",
40         Enum:               "enum",
41         Extern:             "extern",
42         False:              "false",
43         Fn:                 "fn",
44         For:                "for",
45         If:                 "if",
46         Impl:               "impl",
47         In:                 "in",
48         Let:                "let",
49         Loop:               "loop",
50         Match:              "match",
51         Mod:                "mod",
52         Move:               "move",
53         Mut:                "mut",
54         Pub:                "pub",
55         Ref:                "ref",
56         Return:             "return",
57         SelfLower:          "self",
58         SelfUpper:          "Self",
59         Static:             "static",
60         Struct:             "struct",
61         Super:              "super",
62         Trait:              "trait",
63         True:               "true",
64         Type:               "type",
65         Unsafe:             "unsafe",
66         Use:                "use",
67         Where:              "where",
68         While:              "while",
69
70         // Keywords that are used in unstable Rust or reserved for future use.
71         Abstract:           "abstract",
72         Become:             "become",
73         Box:                "box",
74         Do:                 "do",
75         Final:              "final",
76         Macro:              "macro",
77         Override:           "override",
78         Priv:               "priv",
79         Typeof:             "typeof",
80         Unsized:            "unsized",
81         Virtual:            "virtual",
82         Yield:              "yield",
83
84         // Edition-specific keywords that are used in stable Rust.
85         Async:              "async", // >= 2018 Edition only
86         Await:              "await", // >= 2018 Edition only
87         Dyn:                "dyn", // >= 2018 Edition only
88
89         // Edition-specific keywords that are used in unstable Rust or reserved for future use.
90         Try:                "try", // >= 2018 Edition only
91
92         // Special lifetime names
93         UnderscoreLifetime: "'_",
94         StaticLifetime:     "'static",
95
96         // Weak keywords, have special meaning only in specific contexts.
97         Auto:               "auto",
98         Catch:              "catch",
99         Default:            "default",
100         MacroRules:         "macro_rules",
101         Raw:                "raw",
102         Union:              "union",
103         Yeet:               "yeet",
104     }
105
106     // Pre-interned symbols that can be referred to with `rustc_span::sym::*`.
107     //
108     // The symbol is the stringified identifier unless otherwise specified, in
109     // which case the name should mention the non-identifier punctuation.
110     // E.g. `sym::proc_dash_macro` represents "proc-macro", and it shouldn't be
111     // called `sym::proc_macro` because then it's easy to mistakenly think it
112     // represents "proc_macro".
113     //
114     // As well as the symbols listed, there are symbols for the strings
115     // "0", "1", ..., "9", which are accessible via `sym::integer`.
116     //
117     // The proc macro will abort if symbols are not in alphabetical order (as
118     // defined by `impl Ord for str`) or if any symbols are duplicated. Vim
119     // users can sort the list by selecting it and executing the command
120     // `:'<,'>!LC_ALL=C sort`.
121     //
122     // There is currently no checking that all symbols are used; that would be
123     // nice to have.
124     Symbols {
125         AcqRel,
126         Acquire,
127         AddToDiagnostic,
128         Alignment,
129         Any,
130         Arc,
131         Argument,
132         ArgumentV1,
133         ArgumentV1Methods,
134         Arguments,
135         AsMut,
136         AsRef,
137         AssertParamIsClone,
138         AssertParamIsCopy,
139         AssertParamIsEq,
140         AtomicBool,
141         AtomicI128,
142         AtomicI16,
143         AtomicI32,
144         AtomicI64,
145         AtomicI8,
146         AtomicIsize,
147         AtomicPtr,
148         AtomicU128,
149         AtomicU16,
150         AtomicU32,
151         AtomicU64,
152         AtomicU8,
153         AtomicUsize,
154         BTreeEntry,
155         BTreeMap,
156         BTreeSet,
157         BinaryHeap,
158         Borrow,
159         BorrowMut,
160         Break,
161         C,
162         CStr,
163         CString,
164         Capture,
165         Center,
166         Clone,
167         Context,
168         Continue,
169         Copy,
170         Count,
171         Cow,
172         Debug,
173         Decodable,
174         Decoder,
175         DecorateLint,
176         Default,
177         Deref,
178         DiagnosticMessage,
179         DirBuilder,
180         Display,
181         DoubleEndedIterator,
182         Duration,
183         Encodable,
184         Encoder,
185         Eq,
186         Equal,
187         Err,
188         Error,
189         File,
190         FileType,
191         FormatSpec,
192         Formatter,
193         From,
194         FromIterator,
195         FromResidual,
196         Future,
197         FutureOutput,
198         FxHashMap,
199         FxHashSet,
200         GlobalAlloc,
201         Hash,
202         HashMap,
203         HashMapEntry,
204         HashSet,
205         Hasher,
206         Implied,
207         Input,
208         Into,
209         IntoDiagnostic,
210         IntoIterator,
211         IoRead,
212         IoWrite,
213         IpAddr,
214         IrTyKind,
215         Is,
216         ItemContext,
217         Iterator,
218         IteratorItem,
219         Layout,
220         Left,
221         LinkedList,
222         LintPass,
223         LocalKey,
224         Mutex,
225         MutexGuard,
226         N,
227         NonZeroI128,
228         NonZeroI16,
229         NonZeroI32,
230         NonZeroI64,
231         NonZeroI8,
232         NonZeroU128,
233         NonZeroU16,
234         NonZeroU32,
235         NonZeroU64,
236         NonZeroU8,
237         None,
238         Ok,
239         Option,
240         Ord,
241         Ordering,
242         OsStr,
243         OsString,
244         Output,
245         Param,
246         PartialEq,
247         PartialOrd,
248         Path,
249         PathBuf,
250         Pending,
251         Pin,
252         Pointer,
253         Poll,
254         ProcMacro,
255         ProceduralMasqueradeDummyType,
256         Range,
257         RangeFrom,
258         RangeFull,
259         RangeInclusive,
260         RangeTo,
261         RangeToInclusive,
262         Rc,
263         Ready,
264         Receiver,
265         RefCell,
266         Relaxed,
267         Release,
268         Result,
269         ResumeTy,
270         Return,
271         Right,
272         Rust,
273         RustcDecodable,
274         RustcEncodable,
275         RwLock,
276         RwLockReadGuard,
277         RwLockWriteGuard,
278         Send,
279         SeqCst,
280         SliceIndex,
281         Some,
282         String,
283         StructuralEq,
284         StructuralPartialEq,
285         SubdiagnosticMessage,
286         Sync,
287         T,
288         Target,
289         ToOwned,
290         ToString,
291         TokenStream,
292         Try,
293         TryCaptureGeneric,
294         TryCapturePrintable,
295         TryFrom,
296         TryInto,
297         Ty,
298         TyCtxt,
299         TyKind,
300         Unknown,
301         UnsafeArg,
302         Vec,
303         VecDeque,
304         Wrapper,
305         Yield,
306         _DECLS,
307         _Self,
308         __D,
309         __H,
310         __S,
311         __awaitee,
312         __try_var,
313         _d,
314         _e,
315         _task_context,
316         a32,
317         aarch64_target_feature,
318         aarch64_ver_target_feature,
319         abi,
320         abi_amdgpu_kernel,
321         abi_avr_interrupt,
322         abi_c_cmse_nonsecure_call,
323         abi_efiapi,
324         abi_msp430_interrupt,
325         abi_ptx,
326         abi_sysv64,
327         abi_thiscall,
328         abi_unadjusted,
329         abi_vectorcall,
330         abi_x86_interrupt,
331         abort,
332         add,
333         add_assign,
334         add_with_overflow,
335         address,
336         adt_const_params,
337         advanced_slice_patterns,
338         adx_target_feature,
339         alias,
340         align,
341         align_offset,
342         alignment,
343         all,
344         alloc,
345         alloc_error_handler,
346         alloc_layout,
347         alloc_zeroed,
348         allocator,
349         allocator_api,
350         allocator_internals,
351         allow,
352         allow_fail,
353         allow_internal_unsafe,
354         allow_internal_unstable,
355         allowed,
356         alu32,
357         always,
358         and,
359         and_then,
360         anonymous_lifetime_in_impl_trait,
361         any,
362         append_const_msg,
363         arbitrary_enum_discriminant,
364         arbitrary_self_types,
365         args,
366         arith_offset,
367         arm,
368         arm_target_feature,
369         array,
370         arrays,
371         as_ptr,
372         as_ref,
373         as_str,
374         asm,
375         asm_const,
376         asm_experimental_arch,
377         asm_sym,
378         asm_unwind,
379         assert,
380         assert_eq_macro,
381         assert_inhabited,
382         assert_macro,
383         assert_mem_uninitialized_valid,
384         assert_ne_macro,
385         assert_receiver_is_total_eq,
386         assert_zero_valid,
387         asserting,
388         associated_const_equality,
389         associated_consts,
390         associated_type_bounds,
391         associated_type_defaults,
392         associated_types,
393         assume,
394         assume_init,
395         async_await,
396         async_closure,
397         async_fn_in_trait,
398         atomic,
399         atomic_mod,
400         atomics,
401         att_syntax,
402         attr,
403         attr_literals,
404         attributes,
405         augmented_assignments,
406         auto_traits,
407         automatically_derived,
408         avx,
409         avx512_target_feature,
410         avx512bw,
411         avx512f,
412         await_macro,
413         bang,
414         begin_panic,
415         bench,
416         bin,
417         bind_by_move_pattern_guards,
418         bindings_after_at,
419         bitand,
420         bitand_assign,
421         bitor,
422         bitor_assign,
423         bitreverse,
424         bitxor,
425         bitxor_assign,
426         black_box,
427         block,
428         bool,
429         borrowck_graphviz_format,
430         borrowck_graphviz_postflow,
431         box_free,
432         box_patterns,
433         box_syntax,
434         bpf_target_feature,
435         braced_empty_structs,
436         branch,
437         breakpoint,
438         bridge,
439         bswap,
440         c_str,
441         c_unwind,
442         c_variadic,
443         call,
444         call_mut,
445         call_once,
446         caller_location,
447         capture_disjoint_fields,
448         cause,
449         cdylib,
450         ceilf32,
451         ceilf64,
452         cfg,
453         cfg_accessible,
454         cfg_attr,
455         cfg_attr_multi,
456         cfg_doctest,
457         cfg_eval,
458         cfg_hide,
459         cfg_panic,
460         cfg_sanitize,
461         cfg_target_abi,
462         cfg_target_compact,
463         cfg_target_feature,
464         cfg_target_has_atomic,
465         cfg_target_has_atomic_equal_alignment,
466         cfg_target_thread_local,
467         cfg_target_vendor,
468         cfg_version,
469         cfi,
470         char,
471         client,
472         clippy,
473         clobber_abi,
474         clone,
475         clone_closures,
476         clone_from,
477         closure,
478         closure_lifetime_binder,
479         closure_to_fn_coercion,
480         closure_track_caller,
481         cmp,
482         cmp_max,
483         cmp_min,
484         cmpxchg16b_target_feature,
485         cmse_nonsecure_entry,
486         coerce_unsized,
487         cold,
488         collapse_debuginfo,
489         column,
490         compare_exchange,
491         compare_exchange_weak,
492         compile_error,
493         compiler,
494         compiler_builtins,
495         compiler_fence,
496         concat,
497         concat_bytes,
498         concat_idents,
499         conservative_impl_trait,
500         console,
501         const_allocate,
502         const_async_blocks,
503         const_closures,
504         const_compare_raw_pointers,
505         const_constructor,
506         const_deallocate,
507         const_eval_limit,
508         const_eval_select,
509         const_evaluatable_checked,
510         const_extern_fn,
511         const_fn,
512         const_fn_floating_point_arithmetic,
513         const_fn_fn_ptr_basics,
514         const_fn_trait_bound,
515         const_fn_transmute,
516         const_fn_union,
517         const_fn_unsize,
518         const_for,
519         const_format_args,
520         const_generics,
521         const_generics_defaults,
522         const_if_match,
523         const_impl_trait,
524         const_in_array_repeat_expressions,
525         const_indexing,
526         const_let,
527         const_loop,
528         const_mut_refs,
529         const_panic,
530         const_panic_fmt,
531         const_precise_live_drops,
532         const_raw_ptr_deref,
533         const_raw_ptr_to_usize_cast,
534         const_refs_to_cell,
535         const_trait,
536         const_trait_bound_opt_out,
537         const_trait_impl,
538         const_try,
539         constant,
540         constructor,
541         context,
542         copy,
543         copy_closures,
544         copy_nonoverlapping,
545         copysignf32,
546         copysignf64,
547         core,
548         core_panic,
549         core_panic_2015_macro,
550         core_panic_2021_macro,
551         core_panic_macro,
552         cosf32,
553         cosf64,
554         count,
555         cr,
556         crate_id,
557         crate_in_paths,
558         crate_local,
559         crate_name,
560         crate_type,
561         crate_visibility_modifier,
562         crt_dash_static: "crt-static",
563         cstring_type,
564         ctlz,
565         ctlz_nonzero,
566         ctpop,
567         cttz,
568         cttz_nonzero,
569         custom_attribute,
570         custom_derive,
571         custom_inner_attributes,
572         custom_mir,
573         custom_test_frameworks,
574         d,
575         d32,
576         dbg_macro,
577         dead_code,
578         dealloc,
579         debug,
580         debug_assert_eq_macro,
581         debug_assert_macro,
582         debug_assert_ne_macro,
583         debug_assertions,
584         debug_struct,
585         debug_struct_fields_finish,
586         debug_tuple,
587         debug_tuple_fields_finish,
588         debugger_visualizer,
589         decl_macro,
590         declare_lint_pass,
591         decode,
592         default_alloc_error_handler,
593         default_lib_allocator,
594         default_method_body_is_const,
595         default_type_parameter_fallback,
596         default_type_params,
597         delay_span_bug_from_inside_query,
598         deny,
599         deprecated,
600         deprecated_safe,
601         deprecated_suggestion,
602         deref,
603         deref_method,
604         deref_mut,
605         deref_target,
606         derive,
607         derive_const,
608         derive_default_enum,
609         destruct,
610         destructuring_assignment,
611         diagnostic,
612         direct,
613         discriminant_kind,
614         discriminant_type,
615         discriminant_value,
616         dispatch_from_dyn,
617         div,
618         div_assign,
619         do_not_recommend,
620         doc,
621         doc_alias,
622         doc_auto_cfg,
623         doc_cfg,
624         doc_cfg_hide,
625         doc_keyword,
626         doc_masked,
627         doc_notable_trait,
628         doc_primitive,
629         doc_spotlight,
630         doctest,
631         document_private_items,
632         dotdot: "..",
633         dotdot_in_tuple_patterns,
634         dotdoteq_in_patterns,
635         dreg,
636         dreg_low16,
637         dreg_low8,
638         drop,
639         drop_in_place,
640         drop_types_in_const,
641         dropck_eyepatch,
642         dropck_parametricity,
643         dylib,
644         dyn_metadata,
645         dyn_star,
646         dyn_trait,
647         e,
648         edition_panic,
649         eh_catch_typeinfo,
650         eh_personality,
651         emit_enum,
652         emit_enum_variant,
653         emit_enum_variant_arg,
654         emit_struct,
655         emit_struct_field,
656         enable,
657         encode,
658         end,
659         env,
660         eprint_macro,
661         eprintln_macro,
662         eq,
663         ermsb_target_feature,
664         exact_div,
665         except,
666         exchange_malloc,
667         exclusive_range_pattern,
668         exhaustive_integer_patterns,
669         exhaustive_patterns,
670         existential_type,
671         exp2f32,
672         exp2f64,
673         expect,
674         expected,
675         expf32,
676         expf64,
677         explicit_generic_args_with_impl_trait,
678         export_name,
679         expr,
680         extended_key_value_attributes,
681         extended_varargs_abi_support,
682         extern_absolute_paths,
683         extern_crate_item_prelude,
684         extern_crate_self,
685         extern_in_paths,
686         extern_prelude,
687         extern_types,
688         external_doc,
689         f,
690         f16c_target_feature,
691         f32,
692         f64,
693         fabsf32,
694         fabsf64,
695         fadd_fast,
696         fake_variadic,
697         fdiv_fast,
698         feature,
699         fence,
700         ferris: "🦀",
701         fetch_update,
702         ffi,
703         ffi_const,
704         ffi_pure,
705         ffi_returns_twice,
706         field,
707         field_init_shorthand,
708         file,
709         fill,
710         flags,
711         float,
712         float_to_int_unchecked,
713         floorf32,
714         floorf64,
715         fmaf32,
716         fmaf64,
717         fmt,
718         fmul_fast,
719         fn_align,
720         fn_must_use,
721         fn_mut,
722         fn_once,
723         fn_once_output,
724         forbid,
725         forget,
726         format,
727         format_alignment,
728         format_args,
729         format_args_capture,
730         format_args_macro,
731         format_args_nl,
732         format_argument,
733         format_arguments,
734         format_count,
735         format_macro,
736         format_placeholder,
737         format_unsafe_arg,
738         freeze,
739         freg,
740         frem_fast,
741         from,
742         from_desugaring,
743         from_iter,
744         from_method,
745         from_output,
746         from_residual,
747         from_size_align_unchecked,
748         from_usize,
749         from_yeet,
750         fsub_fast,
751         fundamental,
752         future,
753         future_trait,
754         gdb_script_file,
755         ge,
756         gen_future,
757         gen_kill,
758         generator,
759         generator_clone,
760         generator_state,
761         generators,
762         generic_arg_infer,
763         generic_assert,
764         generic_associated_types,
765         generic_associated_types_extended,
766         generic_const_exprs,
767         generic_param_attrs,
768         get_context,
769         global_allocator,
770         global_asm,
771         globs,
772         gt,
773         half_open_range_patterns,
774         half_open_range_patterns_in_slices,
775         hash,
776         hexagon_target_feature,
777         hidden,
778         homogeneous_aggregate,
779         html_favicon_url,
780         html_logo_url,
781         html_no_source,
782         html_playground_url,
783         html_root_url,
784         hwaddress,
785         i,
786         i128,
787         i128_type,
788         i16,
789         i32,
790         i64,
791         i8,
792         ident,
793         identity_future,
794         if_let,
795         if_let_guard,
796         if_while_or_patterns,
797         ignore,
798         impl_header_lifetime_elision,
799         impl_lint_pass,
800         impl_trait_in_bindings,
801         impl_trait_in_fn_trait_return,
802         impl_trait_projections,
803         implied_by,
804         import,
805         import_name_type,
806         import_shadowing,
807         imported_main,
808         in_band_lifetimes,
809         include,
810         include_bytes,
811         include_bytes_macro,
812         include_str,
813         include_str_macro,
814         inclusive_range_syntax,
815         index,
816         index_mut,
817         infer_outlives_requirements,
818         infer_static_outlives_requirements,
819         inherent_associated_types,
820         inherit,
821         inlateout,
822         inline,
823         inline_const,
824         inline_const_pat,
825         inout,
826         instruction_set,
827         integer_: "integer",
828         integral,
829         into_future,
830         into_iter,
831         intra_doc_pointers,
832         intrinsics,
833         irrefutable_let_patterns,
834         isa_attribute,
835         isize,
836         issue,
837         issue_5723_bootstrap,
838         issue_tracker_base_url,
839         item,
840         item_like_imports,
841         iter,
842         iter_repeat,
843         iterator_collect_fn,
844         kcfi,
845         keyword,
846         kind,
847         kreg,
848         kreg0,
849         label,
850         label_break_value,
851         lang,
852         lang_items,
853         large_assignments,
854         lateout,
855         lazy_normalization_consts,
856         le,
857         len,
858         let_chains,
859         let_else,
860         lhs,
861         lib,
862         libc,
863         lifetime,
864         lifetimes,
865         likely,
866         line,
867         link,
868         link_args,
869         link_cfg,
870         link_llvm_intrinsics,
871         link_name,
872         link_ordinal,
873         link_section,
874         linkage,
875         linker,
876         lint_reasons,
877         literal,
878         load,
879         loaded_from_disk,
880         local,
881         local_inner_macros,
882         log10f32,
883         log10f64,
884         log2f32,
885         log2f64,
886         log_syntax,
887         logf32,
888         logf64,
889         loop_break_value,
890         lt,
891         macro_at_most_once_rep,
892         macro_attributes_in_derive_output,
893         macro_escape,
894         macro_export,
895         macro_lifetime_matcher,
896         macro_literal_matcher,
897         macro_metavar_expr,
898         macro_reexport,
899         macro_use,
900         macro_vis_matcher,
901         macros_in_extern,
902         main,
903         managed_boxes,
904         manually_drop,
905         map,
906         marker,
907         marker_trait_attr,
908         masked,
909         match_beginning_vert,
910         match_default_bindings,
911         matches_macro,
912         maxnumf32,
913         maxnumf64,
914         may_dangle,
915         may_unwind,
916         maybe_uninit,
917         maybe_uninit_uninit,
918         maybe_uninit_zeroed,
919         mem_discriminant,
920         mem_drop,
921         mem_forget,
922         mem_replace,
923         mem_size_of,
924         mem_size_of_val,
925         mem_uninitialized,
926         mem_variant_count,
927         mem_zeroed,
928         member_constraints,
929         memory,
930         memtag,
931         message,
932         meta,
933         metadata_type,
934         min_align_of,
935         min_align_of_val,
936         min_const_fn,
937         min_const_generics,
938         min_const_unsafe_fn,
939         min_specialization,
940         min_type_alias_impl_trait,
941         minnumf32,
942         minnumf64,
943         mips_target_feature,
944         miri,
945         misc,
946         mmx_reg,
947         modifiers,
948         module,
949         module_path,
950         more_qualified_paths,
951         more_struct_aliases,
952         movbe_target_feature,
953         move_ref_pattern,
954         move_size_limit,
955         mul,
956         mul_assign,
957         mul_with_overflow,
958         must_not_suspend,
959         must_use,
960         naked,
961         naked_functions,
962         name,
963         names,
964         native_link_modifiers,
965         native_link_modifiers_as_needed,
966         native_link_modifiers_bundle,
967         native_link_modifiers_verbatim,
968         native_link_modifiers_whole_archive,
969         natvis_file,
970         ne,
971         nearbyintf32,
972         nearbyintf64,
973         needs_allocator,
974         needs_drop,
975         needs_panic_runtime,
976         neg,
977         negate_unsigned,
978         negative_impls,
979         neon,
980         never,
981         never_type,
982         never_type_fallback,
983         new,
984         new_binary,
985         new_debug,
986         new_display,
987         new_lower_exp,
988         new_lower_hex,
989         new_octal,
990         new_pointer,
991         new_unchecked,
992         new_upper_exp,
993         new_upper_hex,
994         new_v1,
995         new_v1_formatted,
996         next,
997         nll,
998         no,
999         no_builtins,
1000         no_core,
1001         no_coverage,
1002         no_crate_inject,
1003         no_debug,
1004         no_default_passes,
1005         no_implicit_prelude,
1006         no_inline,
1007         no_link,
1008         no_main,
1009         no_mangle,
1010         no_sanitize,
1011         no_stack_check,
1012         no_start,
1013         no_std,
1014         nomem,
1015         non_ascii_idents,
1016         non_exhaustive,
1017         non_exhaustive_omitted_patterns_lint,
1018         non_modrs_mods,
1019         nontemporal_store,
1020         noop_method_borrow,
1021         noop_method_clone,
1022         noop_method_deref,
1023         noreturn,
1024         nostack,
1025         not,
1026         notable_trait,
1027         note,
1028         object_safe_for_dispatch,
1029         of,
1030         offset,
1031         omit_gdb_pretty_printer_section,
1032         on,
1033         on_unimplemented,
1034         oom,
1035         opaque,
1036         ops,
1037         opt_out_copy,
1038         optimize,
1039         optimize_attribute,
1040         optin_builtin_traits,
1041         option,
1042         option_env,
1043         options,
1044         or,
1045         or_patterns,
1046         other,
1047         out,
1048         overlapping_marker_traits,
1049         owned_box,
1050         packed,
1051         panic,
1052         panic_2015,
1053         panic_2021,
1054         panic_abort,
1055         panic_bounds_check,
1056         panic_cannot_unwind,
1057         panic_display,
1058         panic_fmt,
1059         panic_handler,
1060         panic_impl,
1061         panic_implementation,
1062         panic_info,
1063         panic_location,
1064         panic_nounwind,
1065         panic_runtime,
1066         panic_str,
1067         panic_unwind,
1068         panicking,
1069         param_attrs,
1070         parent_label,
1071         partial_cmp,
1072         partial_ord,
1073         passes,
1074         pat,
1075         pat_param,
1076         path,
1077         pattern_parentheses,
1078         phantom_data,
1079         pin,
1080         platform_intrinsics,
1081         plugin,
1082         plugin_registrar,
1083         plugins,
1084         pointee_trait,
1085         pointer,
1086         pointer_sized,
1087         poll,
1088         position,
1089         post_dash_lto: "post-lto",
1090         powerpc_target_feature,
1091         powf32,
1092         powf64,
1093         powif32,
1094         powif64,
1095         pre_dash_lto: "pre-lto",
1096         precise_pointer_size_matching,
1097         precision,
1098         pref_align_of,
1099         prefetch_read_data,
1100         prefetch_read_instruction,
1101         prefetch_write_data,
1102         prefetch_write_instruction,
1103         preg,
1104         prelude,
1105         prelude_import,
1106         preserves_flags,
1107         primitive,
1108         print_macro,
1109         println_macro,
1110         proc_dash_macro: "proc-macro",
1111         proc_macro,
1112         proc_macro_attribute,
1113         proc_macro_derive,
1114         proc_macro_expr,
1115         proc_macro_gen,
1116         proc_macro_hygiene,
1117         proc_macro_internals,
1118         proc_macro_mod,
1119         proc_macro_non_items,
1120         proc_macro_path_invoc,
1121         profiler_builtins,
1122         profiler_runtime,
1123         ptr,
1124         ptr_guaranteed_cmp,
1125         ptr_mask,
1126         ptr_null,
1127         ptr_null_mut,
1128         ptr_offset_from,
1129         ptr_offset_from_unsigned,
1130         pub_macro_rules,
1131         pub_restricted,
1132         public,
1133         pure,
1134         pushpop_unsafe,
1135         qreg,
1136         qreg_low4,
1137         qreg_low8,
1138         quad_precision_float,
1139         question_mark,
1140         quote,
1141         range_inclusive_new,
1142         raw_dylib,
1143         raw_eq,
1144         raw_identifiers,
1145         raw_ref_op,
1146         re_rebalance_coherence,
1147         read_enum,
1148         read_enum_variant,
1149         read_enum_variant_arg,
1150         read_struct,
1151         read_struct_field,
1152         readonly,
1153         realloc,
1154         reason,
1155         receiver,
1156         recursion_limit,
1157         reexport_test_harness_main,
1158         ref_unwind_safe_trait,
1159         reference,
1160         reflect,
1161         reg,
1162         reg16,
1163         reg32,
1164         reg64,
1165         reg_abcd,
1166         reg_byte,
1167         reg_iw,
1168         reg_nonzero,
1169         reg_pair,
1170         reg_ptr,
1171         reg_upper,
1172         register_attr,
1173         register_tool,
1174         relaxed_adts,
1175         relaxed_struct_unsize,
1176         rem,
1177         rem_assign,
1178         repr,
1179         repr128,
1180         repr_align,
1181         repr_align_enum,
1182         repr_packed,
1183         repr_simd,
1184         repr_transparent,
1185         require,
1186         residual,
1187         result,
1188         return_position_impl_trait_in_trait,
1189         rhs,
1190         rintf32,
1191         rintf64,
1192         riscv_target_feature,
1193         rlib,
1194         rotate_left,
1195         rotate_right,
1196         roundf32,
1197         roundf64,
1198         rt,
1199         rtm_target_feature,
1200         rust,
1201         rust_2015,
1202         rust_2015_preview,
1203         rust_2018,
1204         rust_2018_preview,
1205         rust_2021,
1206         rust_2021_preview,
1207         rust_2024,
1208         rust_2024_preview,
1209         rust_begin_unwind,
1210         rust_cold_cc,
1211         rust_eh_catch_typeinfo,
1212         rust_eh_personality,
1213         rustc,
1214         rustc_allocator,
1215         rustc_allocator_zeroed,
1216         rustc_allow_const_fn_unstable,
1217         rustc_allow_incoherent_impl,
1218         rustc_allowed_through_unstable_modules,
1219         rustc_attrs,
1220         rustc_box,
1221         rustc_builtin_macro,
1222         rustc_capture_analysis,
1223         rustc_clean,
1224         rustc_coherence_is_core,
1225         rustc_const_stable,
1226         rustc_const_unstable,
1227         rustc_conversion_suggestion,
1228         rustc_deallocator,
1229         rustc_def_path,
1230         rustc_default_body_unstable,
1231         rustc_deny_explicit_impl,
1232         rustc_diagnostic_item,
1233         rustc_diagnostic_macros,
1234         rustc_dirty,
1235         rustc_do_not_const_check,
1236         rustc_dummy,
1237         rustc_dump_env_program_clauses,
1238         rustc_dump_program_clauses,
1239         rustc_dump_user_substs,
1240         rustc_dump_vtable,
1241         rustc_effective_visibility,
1242         rustc_error,
1243         rustc_evaluate_where_clauses,
1244         rustc_expected_cgu_reuse,
1245         rustc_has_incoherent_inherent_impls,
1246         rustc_if_this_changed,
1247         rustc_inherit_overflow_checks,
1248         rustc_insignificant_dtor,
1249         rustc_layout,
1250         rustc_layout_scalar_valid_range_end,
1251         rustc_layout_scalar_valid_range_start,
1252         rustc_legacy_const_generics,
1253         rustc_lint_diagnostics,
1254         rustc_lint_opt_deny_field_access,
1255         rustc_lint_opt_ty,
1256         rustc_lint_query_instability,
1257         rustc_macro_transparency,
1258         rustc_main,
1259         rustc_mir,
1260         rustc_must_implement_one_of,
1261         rustc_nonnull_optimization_guaranteed,
1262         rustc_nounwind,
1263         rustc_object_lifetime_default,
1264         rustc_on_unimplemented,
1265         rustc_outlives,
1266         rustc_paren_sugar,
1267         rustc_partition_codegened,
1268         rustc_partition_reused,
1269         rustc_pass_by_value,
1270         rustc_peek,
1271         rustc_peek_definite_init,
1272         rustc_peek_liveness,
1273         rustc_peek_maybe_init,
1274         rustc_peek_maybe_uninit,
1275         rustc_polymorphize_error,
1276         rustc_private,
1277         rustc_proc_macro_decls,
1278         rustc_promotable,
1279         rustc_reallocator,
1280         rustc_regions,
1281         rustc_reservation_impl,
1282         rustc_safe_intrinsic,
1283         rustc_serialize,
1284         rustc_skip_array_during_method_dispatch,
1285         rustc_specialization_trait,
1286         rustc_std_internal_symbol,
1287         rustc_strict_coherence,
1288         rustc_symbol_name,
1289         rustc_test_marker,
1290         rustc_then_this_would_need,
1291         rustc_trivial_field_reads,
1292         rustc_unsafe_specialization_marker,
1293         rustc_variance,
1294         rustdoc,
1295         rustdoc_internals,
1296         rustdoc_missing_doc_code_examples,
1297         rustfmt,
1298         rvalue_static_promotion,
1299         s,
1300         safety,
1301         sanitize,
1302         sanitizer_runtime,
1303         saturating_add,
1304         saturating_sub,
1305         self_in_typedefs,
1306         self_struct_ctor,
1307         semitransparent,
1308         shadow_call_stack,
1309         shl,
1310         shl_assign,
1311         should_panic,
1312         shr,
1313         shr_assign,
1314         sig_dfl,
1315         sig_ign,
1316         simd,
1317         simd_add,
1318         simd_and,
1319         simd_arith_offset,
1320         simd_as,
1321         simd_bitmask,
1322         simd_cast,
1323         simd_cast_ptr,
1324         simd_ceil,
1325         simd_div,
1326         simd_eq,
1327         simd_expose_addr,
1328         simd_extract,
1329         simd_fabs,
1330         simd_fcos,
1331         simd_fexp,
1332         simd_fexp2,
1333         simd_ffi,
1334         simd_flog,
1335         simd_flog10,
1336         simd_flog2,
1337         simd_floor,
1338         simd_fma,
1339         simd_fmax,
1340         simd_fmin,
1341         simd_fpow,
1342         simd_fpowi,
1343         simd_from_exposed_addr,
1344         simd_fsin,
1345         simd_fsqrt,
1346         simd_gather,
1347         simd_ge,
1348         simd_gt,
1349         simd_insert,
1350         simd_le,
1351         simd_lt,
1352         simd_mul,
1353         simd_ne,
1354         simd_neg,
1355         simd_or,
1356         simd_reduce_add_ordered,
1357         simd_reduce_add_unordered,
1358         simd_reduce_all,
1359         simd_reduce_and,
1360         simd_reduce_any,
1361         simd_reduce_max,
1362         simd_reduce_max_nanless,
1363         simd_reduce_min,
1364         simd_reduce_min_nanless,
1365         simd_reduce_mul_ordered,
1366         simd_reduce_mul_unordered,
1367         simd_reduce_or,
1368         simd_reduce_xor,
1369         simd_rem,
1370         simd_round,
1371         simd_saturating_add,
1372         simd_saturating_sub,
1373         simd_scatter,
1374         simd_select,
1375         simd_select_bitmask,
1376         simd_shl,
1377         simd_shr,
1378         simd_shuffle,
1379         simd_sub,
1380         simd_trunc,
1381         simd_xor,
1382         since,
1383         sinf32,
1384         sinf64,
1385         size,
1386         size_of,
1387         size_of_val,
1388         sized,
1389         skip,
1390         slice,
1391         slice_len_fn,
1392         slice_patterns,
1393         slicing_syntax,
1394         soft,
1395         specialization,
1396         speed,
1397         spotlight,
1398         sqrtf32,
1399         sqrtf64,
1400         sreg,
1401         sreg_low16,
1402         sse,
1403         sse4a_target_feature,
1404         stable,
1405         staged_api,
1406         start,
1407         state,
1408         static_in_const,
1409         static_nobundle,
1410         static_recursion,
1411         staticlib,
1412         std,
1413         std_panic,
1414         std_panic_2015_macro,
1415         std_panic_macro,
1416         stmt,
1417         stmt_expr_attributes,
1418         stop_after_dataflow,
1419         store,
1420         str,
1421         str_split_whitespace,
1422         str_trim,
1423         str_trim_end,
1424         str_trim_start,
1425         strict_provenance,
1426         string_deref_patterns,
1427         stringify,
1428         struct_field_attributes,
1429         struct_inherit,
1430         struct_variant,
1431         structural_match,
1432         structural_peq,
1433         structural_teq,
1434         sty,
1435         sub,
1436         sub_assign,
1437         sub_with_overflow,
1438         suggestion,
1439         sym,
1440         sync,
1441         t32,
1442         target,
1443         target_abi,
1444         target_arch,
1445         target_endian,
1446         target_env,
1447         target_family,
1448         target_feature,
1449         target_feature_11,
1450         target_has_atomic,
1451         target_has_atomic_equal_alignment,
1452         target_has_atomic_load_store,
1453         target_os,
1454         target_pointer_width,
1455         target_thread_local,
1456         target_vendor,
1457         tbm_target_feature,
1458         termination,
1459         termination_trait,
1460         termination_trait_test,
1461         test,
1462         test_2018_feature,
1463         test_accepted_feature,
1464         test_case,
1465         test_removed_feature,
1466         test_runner,
1467         test_unstable_lint,
1468         thread,
1469         thread_local,
1470         thread_local_macro,
1471         thumb2,
1472         thumb_mode: "thumb-mode",
1473         tmm_reg,
1474         to_string,
1475         to_vec,
1476         todo_macro,
1477         tool_attributes,
1478         tool_lints,
1479         trace_macros,
1480         track_caller,
1481         trait_alias,
1482         trait_upcasting,
1483         transmute,
1484         transmute_opts,
1485         transmute_trait,
1486         transparent,
1487         transparent_enums,
1488         transparent_unions,
1489         trivial_bounds,
1490         truncf32,
1491         truncf64,
1492         try_blocks,
1493         try_capture,
1494         try_from,
1495         try_into,
1496         try_trait_v2,
1497         tt,
1498         tuple,
1499         tuple_indexing,
1500         tuple_trait,
1501         two_phase,
1502         ty,
1503         type_alias_enum_variants,
1504         type_alias_impl_trait,
1505         type_ascribe,
1506         type_ascription,
1507         type_changing_struct_update,
1508         type_id,
1509         type_length_limit,
1510         type_macros,
1511         type_name,
1512         u128,
1513         u16,
1514         u32,
1515         u64,
1516         u8,
1517         unaligned_volatile_load,
1518         unaligned_volatile_store,
1519         unboxed_closures,
1520         unchecked_add,
1521         unchecked_div,
1522         unchecked_mul,
1523         unchecked_rem,
1524         unchecked_shl,
1525         unchecked_shr,
1526         unchecked_sub,
1527         underscore_const_names,
1528         underscore_imports,
1529         underscore_lifetimes,
1530         uniform_paths,
1531         unimplemented_macro,
1532         unit,
1533         universal_impl_trait,
1534         unix,
1535         unix_sigpipe,
1536         unlikely,
1537         unmarked_api,
1538         unpin,
1539         unreachable,
1540         unreachable_2015,
1541         unreachable_2015_macro,
1542         unreachable_2021,
1543         unreachable_code,
1544         unreachable_display,
1545         unreachable_macro,
1546         unrestricted_attribute_tokens,
1547         unsafe_block_in_unsafe_fn,
1548         unsafe_cell,
1549         unsafe_no_drop_flag,
1550         unsafe_pin_internals,
1551         unsize,
1552         unsized_fn_params,
1553         unsized_locals,
1554         unsized_tuple_coercion,
1555         unstable,
1556         unstable_location_reason_default: "this crate is being loaded from the sysroot, an \
1557                           unstable location; did you mean to load this crate \
1558                           from crates.io via `Cargo.toml` instead?",
1559         untagged_unions,
1560         unused_imports,
1561         unwind,
1562         unwind_attributes,
1563         unwind_safe_trait,
1564         unwrap,
1565         unwrap_or,
1566         use_extern_macros,
1567         use_nested_groups,
1568         used,
1569         used_with_arg,
1570         using,
1571         usize,
1572         v1,
1573         va_arg,
1574         va_copy,
1575         va_end,
1576         va_list,
1577         va_start,
1578         val,
1579         validity,
1580         values,
1581         var,
1582         variant_count,
1583         vec,
1584         vec_macro,
1585         version,
1586         vfp2,
1587         vis,
1588         visible_private_types,
1589         volatile,
1590         volatile_copy_memory,
1591         volatile_copy_nonoverlapping_memory,
1592         volatile_load,
1593         volatile_set_memory,
1594         volatile_store,
1595         vreg,
1596         vreg_low16,
1597         vtable_align,
1598         vtable_size,
1599         warn,
1600         wasm_abi,
1601         wasm_import_module,
1602         wasm_target_feature,
1603         while_let,
1604         width,
1605         windows,
1606         windows_subsystem,
1607         with_negative_coherence,
1608         wrapping_add,
1609         wrapping_mul,
1610         wrapping_sub,
1611         wreg,
1612         write_bytes,
1613         write_macro,
1614         write_str,
1615         writeln_macro,
1616         x87_reg,
1617         xer,
1618         xmm_reg,
1619         yeet_desugar_details,
1620         yeet_expr,
1621         ymm_reg,
1622         zmm_reg,
1623     }
1624 }
1625
1626 #[derive(Copy, Clone, Eq, HashStable_Generic, Encodable, Decodable)]
1627 pub struct Ident {
1628     pub name: Symbol,
1629     pub span: Span,
1630 }
1631
1632 impl Ident {
1633     #[inline]
1634     /// Constructs a new identifier from a symbol and a span.
1635     pub const fn new(name: Symbol, span: Span) -> Ident {
1636         Ident { name, span }
1637     }
1638
1639     /// Constructs a new identifier with a dummy span.
1640     #[inline]
1641     pub const fn with_dummy_span(name: Symbol) -> Ident {
1642         Ident::new(name, DUMMY_SP)
1643     }
1644
1645     #[inline]
1646     pub fn empty() -> Ident {
1647         Ident::with_dummy_span(kw::Empty)
1648     }
1649
1650     /// Maps a string to an identifier with a dummy span.
1651     pub fn from_str(string: &str) -> Ident {
1652         Ident::with_dummy_span(Symbol::intern(string))
1653     }
1654
1655     /// Maps a string and a span to an identifier.
1656     pub fn from_str_and_span(string: &str, span: Span) -> Ident {
1657         Ident::new(Symbol::intern(string), span)
1658     }
1659
1660     /// Replaces `lo` and `hi` with those from `span`, but keep hygiene context.
1661     pub fn with_span_pos(self, span: Span) -> Ident {
1662         Ident::new(self.name, span.with_ctxt(self.span.ctxt()))
1663     }
1664
1665     pub fn without_first_quote(self) -> Ident {
1666         Ident::new(Symbol::intern(self.as_str().trim_start_matches('\'')), self.span)
1667     }
1668
1669     /// "Normalize" ident for use in comparisons using "item hygiene".
1670     /// Identifiers with same string value become same if they came from the same macro 2.0 macro
1671     /// (e.g., `macro` item, but not `macro_rules` item) and stay different if they came from
1672     /// different macro 2.0 macros.
1673     /// Technically, this operation strips all non-opaque marks from ident's syntactic context.
1674     pub fn normalize_to_macros_2_0(self) -> Ident {
1675         Ident::new(self.name, self.span.normalize_to_macros_2_0())
1676     }
1677
1678     /// "Normalize" ident for use in comparisons using "local variable hygiene".
1679     /// Identifiers with same string value become same if they came from the same non-transparent
1680     /// macro (e.g., `macro` or `macro_rules!` items) and stay different if they came from different
1681     /// non-transparent macros.
1682     /// Technically, this operation strips all transparent marks from ident's syntactic context.
1683     #[inline]
1684     pub fn normalize_to_macro_rules(self) -> Ident {
1685         Ident::new(self.name, self.span.normalize_to_macro_rules())
1686     }
1687
1688     /// Access the underlying string. This is a slowish operation because it
1689     /// requires locking the symbol interner.
1690     ///
1691     /// Note that the lifetime of the return value is a lie. See
1692     /// `Symbol::as_str()` for details.
1693     pub fn as_str(&self) -> &str {
1694         self.name.as_str()
1695     }
1696 }
1697
1698 impl PartialEq for Ident {
1699     #[inline]
1700     fn eq(&self, rhs: &Self) -> bool {
1701         self.name == rhs.name && self.span.eq_ctxt(rhs.span)
1702     }
1703 }
1704
1705 impl Hash for Ident {
1706     fn hash<H: Hasher>(&self, state: &mut H) {
1707         self.name.hash(state);
1708         self.span.ctxt().hash(state);
1709     }
1710 }
1711
1712 impl fmt::Debug for Ident {
1713     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1714         fmt::Display::fmt(self, f)?;
1715         fmt::Debug::fmt(&self.span.ctxt(), f)
1716     }
1717 }
1718
1719 /// This implementation is supposed to be used in error messages, so it's expected to be identical
1720 /// to printing the original identifier token written in source code (`token_to_string`),
1721 /// except that AST identifiers don't keep the rawness flag, so we have to guess it.
1722 impl fmt::Display for Ident {
1723     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1724         fmt::Display::fmt(&IdentPrinter::new(self.name, self.is_raw_guess(), None), f)
1725     }
1726 }
1727
1728 /// The most general type to print identifiers.
1729 ///
1730 /// AST pretty-printer is used as a fallback for turning AST structures into token streams for
1731 /// proc macros. Additionally, proc macros may stringify their input and expect it survive the
1732 /// stringification (especially true for proc macro derives written between Rust 1.15 and 1.30).
1733 /// So we need to somehow pretty-print `$crate` in a way preserving at least some of its
1734 /// hygiene data, most importantly name of the crate it refers to.
1735 /// As a result we print `$crate` as `crate` if it refers to the local crate
1736 /// and as `::other_crate_name` if it refers to some other crate.
1737 /// Note, that this is only done if the ident token is printed from inside of AST pretty-printing,
1738 /// but not otherwise. Pretty-printing is the only way for proc macros to discover token contents,
1739 /// so we should not perform this lossy conversion if the top level call to the pretty-printer was
1740 /// done for a token stream or a single token.
1741 pub struct IdentPrinter {
1742     symbol: Symbol,
1743     is_raw: bool,
1744     /// Span used for retrieving the crate name to which `$crate` refers to,
1745     /// if this field is `None` then the `$crate` conversion doesn't happen.
1746     convert_dollar_crate: Option<Span>,
1747 }
1748
1749 impl IdentPrinter {
1750     /// The most general `IdentPrinter` constructor. Do not use this.
1751     pub fn new(symbol: Symbol, is_raw: bool, convert_dollar_crate: Option<Span>) -> IdentPrinter {
1752         IdentPrinter { symbol, is_raw, convert_dollar_crate }
1753     }
1754
1755     /// This implementation is supposed to be used when printing identifiers
1756     /// as a part of pretty-printing for larger AST pieces.
1757     /// Do not use this either.
1758     pub fn for_ast_ident(ident: Ident, is_raw: bool) -> IdentPrinter {
1759         IdentPrinter::new(ident.name, is_raw, Some(ident.span))
1760     }
1761 }
1762
1763 impl fmt::Display for IdentPrinter {
1764     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1765         if self.is_raw {
1766             f.write_str("r#")?;
1767         } else if self.symbol == kw::DollarCrate {
1768             if let Some(span) = self.convert_dollar_crate {
1769                 let converted = span.ctxt().dollar_crate_name();
1770                 if !converted.is_path_segment_keyword() {
1771                     f.write_str("::")?;
1772                 }
1773                 return fmt::Display::fmt(&converted, f);
1774             }
1775         }
1776         fmt::Display::fmt(&self.symbol, f)
1777     }
1778 }
1779
1780 /// An newtype around `Ident` that calls [Ident::normalize_to_macro_rules] on
1781 /// construction.
1782 // FIXME(matthewj, petrochenkov) Use this more often, add a similar
1783 // `ModernIdent` struct and use that as well.
1784 #[derive(Copy, Clone, Eq, PartialEq, Hash)]
1785 pub struct MacroRulesNormalizedIdent(Ident);
1786
1787 impl MacroRulesNormalizedIdent {
1788     pub fn new(ident: Ident) -> Self {
1789         Self(ident.normalize_to_macro_rules())
1790     }
1791 }
1792
1793 impl fmt::Debug for MacroRulesNormalizedIdent {
1794     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1795         fmt::Debug::fmt(&self.0, f)
1796     }
1797 }
1798
1799 impl fmt::Display for MacroRulesNormalizedIdent {
1800     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1801         fmt::Display::fmt(&self.0, f)
1802     }
1803 }
1804
1805 /// An interned string.
1806 ///
1807 /// Internally, a `Symbol` is implemented as an index, and all operations
1808 /// (including hashing, equality, and ordering) operate on that index. The use
1809 /// of `rustc_index::newtype_index!` means that `Option<Symbol>` only takes up 4 bytes,
1810 /// because `rustc_index::newtype_index!` reserves the last 256 values for tagging purposes.
1811 ///
1812 /// Note that `Symbol` cannot directly be a `rustc_index::newtype_index!` because it
1813 /// implements `fmt::Debug`, `Encodable`, and `Decodable` in special ways.
1814 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1815 pub struct Symbol(SymbolIndex);
1816
1817 rustc_index::newtype_index! {
1818     struct SymbolIndex {}
1819 }
1820
1821 impl Symbol {
1822     const fn new(n: u32) -> Self {
1823         Symbol(SymbolIndex::from_u32(n))
1824     }
1825
1826     /// for use in Decoder only
1827     pub fn new_from_decoded(n: u32) -> Self {
1828         Self::new(n)
1829     }
1830
1831     /// Maps a string to its interned representation.
1832     pub fn intern(string: &str) -> Self {
1833         with_session_globals(|session_globals| session_globals.symbol_interner.intern(string))
1834     }
1835
1836     /// Access the underlying string. This is a slowish operation because it
1837     /// requires locking the symbol interner.
1838     ///
1839     /// Note that the lifetime of the return value is a lie. It's not the same
1840     /// as `&self`, but actually tied to the lifetime of the underlying
1841     /// interner. Interners are long-lived, and there are very few of them, and
1842     /// this function is typically used for short-lived things, so in practice
1843     /// it works out ok.
1844     pub fn as_str(&self) -> &str {
1845         with_session_globals(|session_globals| unsafe {
1846             std::mem::transmute::<&str, &str>(session_globals.symbol_interner.get(*self))
1847         })
1848     }
1849
1850     pub fn as_u32(self) -> u32 {
1851         self.0.as_u32()
1852     }
1853
1854     pub fn is_empty(self) -> bool {
1855         self == kw::Empty
1856     }
1857
1858     /// This method is supposed to be used in error messages, so it's expected to be
1859     /// identical to printing the original identifier token written in source code
1860     /// (`token_to_string`, `Ident::to_string`), except that symbols don't keep the rawness flag
1861     /// or edition, so we have to guess the rawness using the global edition.
1862     pub fn to_ident_string(self) -> String {
1863         Ident::with_dummy_span(self).to_string()
1864     }
1865 }
1866
1867 impl fmt::Debug for Symbol {
1868     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1869         fmt::Debug::fmt(self.as_str(), f)
1870     }
1871 }
1872
1873 impl fmt::Display for Symbol {
1874     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1875         fmt::Display::fmt(self.as_str(), f)
1876     }
1877 }
1878
1879 // takes advantage of `str::to_string` specialization
1880 impl ToString for Symbol {
1881     fn to_string(&self) -> String {
1882         self.as_str().to_string()
1883     }
1884 }
1885
1886 impl<S: Encoder> Encodable<S> for Symbol {
1887     default fn encode(&self, s: &mut S) {
1888         s.emit_str(self.as_str());
1889     }
1890 }
1891
1892 impl<D: Decoder> Decodable<D> for Symbol {
1893     #[inline]
1894     default fn decode(d: &mut D) -> Symbol {
1895         Symbol::intern(d.read_str())
1896     }
1897 }
1898
1899 impl<CTX> HashStable<CTX> for Symbol {
1900     #[inline]
1901     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
1902         self.as_str().hash_stable(hcx, hasher);
1903     }
1904 }
1905
1906 impl<CTX> ToStableHashKey<CTX> for Symbol {
1907     type KeyType = String;
1908     #[inline]
1909     fn to_stable_hash_key(&self, _: &CTX) -> String {
1910         self.as_str().to_string()
1911     }
1912 }
1913
1914 #[derive(Default)]
1915 pub(crate) struct Interner(Lock<InternerInner>);
1916
1917 // The `&'static str`s in this type actually point into the arena.
1918 //
1919 // The `FxHashMap`+`Vec` pair could be replaced by `FxIndexSet`, but #75278
1920 // found that to regress performance up to 2% in some cases. This might be
1921 // revisited after further improvements to `indexmap`.
1922 //
1923 // This type is private to prevent accidentally constructing more than one
1924 // `Interner` on the same thread, which makes it easy to mix up `Symbol`s
1925 // between `Interner`s.
1926 #[derive(Default)]
1927 struct InternerInner {
1928     arena: DroplessArena,
1929     names: FxHashMap<&'static str, Symbol>,
1930     strings: Vec<&'static str>,
1931 }
1932
1933 impl Interner {
1934     fn prefill(init: &[&'static str]) -> Self {
1935         Interner(Lock::new(InternerInner {
1936             strings: init.into(),
1937             names: init.iter().copied().zip((0..).map(Symbol::new)).collect(),
1938             ..Default::default()
1939         }))
1940     }
1941
1942     #[inline]
1943     fn intern(&self, string: &str) -> Symbol {
1944         let mut inner = self.0.lock();
1945         if let Some(&name) = inner.names.get(string) {
1946             return name;
1947         }
1948
1949         let name = Symbol::new(inner.strings.len() as u32);
1950
1951         // SAFETY: we convert from `&str` to `&[u8]`, clone it into the arena,
1952         // and immediately convert the clone back to `&[u8], all because there
1953         // is no `inner.arena.alloc_str()` method. This is clearly safe.
1954         let string: &str =
1955             unsafe { str::from_utf8_unchecked(inner.arena.alloc_slice(string.as_bytes())) };
1956
1957         // SAFETY: we can extend the arena allocation to `'static` because we
1958         // only access these while the arena is still alive.
1959         let string: &'static str = unsafe { &*(string as *const str) };
1960         inner.strings.push(string);
1961
1962         // This second hash table lookup can be avoided by using `RawEntryMut`,
1963         // but this code path isn't hot enough for it to be worth it. See
1964         // #91445 for details.
1965         inner.names.insert(string, name);
1966         name
1967     }
1968
1969     // Get the symbol as a string. `Symbol::as_str()` should be used in
1970     // preference to this function.
1971     fn get(&self, symbol: Symbol) -> &str {
1972         self.0.lock().strings[symbol.0.as_usize()]
1973     }
1974 }
1975
1976 // This module has a very short name because it's used a lot.
1977 /// This module contains all the defined keyword `Symbol`s.
1978 ///
1979 /// Given that `kw` is imported, use them like `kw::keyword_name`.
1980 /// For example `kw::Loop` or `kw::Break`.
1981 pub mod kw {
1982     pub use super::kw_generated::*;
1983 }
1984
1985 // This module has a very short name because it's used a lot.
1986 /// This module contains all the defined non-keyword `Symbol`s.
1987 ///
1988 /// Given that `sym` is imported, use them like `sym::symbol_name`.
1989 /// For example `sym::rustfmt` or `sym::u8`.
1990 pub mod sym {
1991     use super::Symbol;
1992
1993     #[doc(inline)]
1994     pub use super::sym_generated::*;
1995
1996     // Used from a macro in `librustc_feature/accepted.rs`
1997     pub use super::kw::MacroRules as macro_rules;
1998
1999     /// Get the symbol for an integer.
2000     ///
2001     /// The first few non-negative integers each have a static symbol and therefore
2002     /// are fast.
2003     pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
2004         if let Result::Ok(idx) = n.try_into() {
2005             if idx < 10 {
2006                 return Symbol::new(super::SYMBOL_DIGITS_BASE + idx as u32);
2007             }
2008         }
2009         Symbol::intern(&n.to_string())
2010     }
2011 }
2012
2013 impl Symbol {
2014     fn is_special(self) -> bool {
2015         self <= kw::Underscore
2016     }
2017
2018     fn is_used_keyword_always(self) -> bool {
2019         self >= kw::As && self <= kw::While
2020     }
2021
2022     fn is_used_keyword_conditional(self, edition: impl FnOnce() -> Edition) -> bool {
2023         (self >= kw::Async && self <= kw::Dyn) && edition() >= Edition::Edition2018
2024     }
2025
2026     fn is_unused_keyword_always(self) -> bool {
2027         self >= kw::Abstract && self <= kw::Yield
2028     }
2029
2030     fn is_unused_keyword_conditional(self, edition: impl FnOnce() -> Edition) -> bool {
2031         self == kw::Try && edition() >= Edition::Edition2018
2032     }
2033
2034     pub fn is_reserved(self, edition: impl Copy + FnOnce() -> Edition) -> bool {
2035         self.is_special()
2036             || self.is_used_keyword_always()
2037             || self.is_unused_keyword_always()
2038             || self.is_used_keyword_conditional(edition)
2039             || self.is_unused_keyword_conditional(edition)
2040     }
2041
2042     /// A keyword or reserved identifier that can be used as a path segment.
2043     pub fn is_path_segment_keyword(self) -> bool {
2044         self == kw::Super
2045             || self == kw::SelfLower
2046             || self == kw::SelfUpper
2047             || self == kw::Crate
2048             || self == kw::PathRoot
2049             || self == kw::DollarCrate
2050     }
2051
2052     /// Returns `true` if the symbol is `true` or `false`.
2053     pub fn is_bool_lit(self) -> bool {
2054         self == kw::True || self == kw::False
2055     }
2056
2057     /// Returns `true` if this symbol can be a raw identifier.
2058     pub fn can_be_raw(self) -> bool {
2059         self != kw::Empty && self != kw::Underscore && !self.is_path_segment_keyword()
2060     }
2061
2062     /// Is this symbol was interned in compiler's `symbols!` macro
2063     pub fn is_preinterned(self) -> bool {
2064         self.as_u32() < PREINTERNED_SYMBOLS_COUNT
2065     }
2066 }
2067
2068 impl Ident {
2069     /// Returns `true` for reserved identifiers used internally for elided lifetimes,
2070     /// unnamed method parameters, crate root module, error recovery etc.
2071     pub fn is_special(self) -> bool {
2072         self.name.is_special()
2073     }
2074
2075     /// Returns `true` if the token is a keyword used in the language.
2076     pub fn is_used_keyword(self) -> bool {
2077         // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
2078         self.name.is_used_keyword_always()
2079             || self.name.is_used_keyword_conditional(|| self.span.edition())
2080     }
2081
2082     /// Returns `true` if the token is a keyword reserved for possible future use.
2083     pub fn is_unused_keyword(self) -> bool {
2084         // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
2085         self.name.is_unused_keyword_always()
2086             || self.name.is_unused_keyword_conditional(|| self.span.edition())
2087     }
2088
2089     /// Returns `true` if the token is either a special identifier or a keyword.
2090     pub fn is_reserved(self) -> bool {
2091         // Note: `span.edition()` is relatively expensive, don't call it unless necessary.
2092         self.name.is_reserved(|| self.span.edition())
2093     }
2094
2095     /// A keyword or reserved identifier that can be used as a path segment.
2096     pub fn is_path_segment_keyword(self) -> bool {
2097         self.name.is_path_segment_keyword()
2098     }
2099
2100     /// We see this identifier in a normal identifier position, like variable name or a type.
2101     /// How was it written originally? Did it use the raw form? Let's try to guess.
2102     pub fn is_raw_guess(self) -> bool {
2103         self.name.can_be_raw() && self.is_reserved()
2104     }
2105 }