rustc_builtin_macros/
lib.rs

1//! This crate contains implementations of built-in macros and other code generating facilities
2//! injecting code into the crate before it is lowered to HIR.
3
4// tidy-alphabetical-start
5#![allow(internal_features)]
6#![allow(rustc::diagnostic_outside_of_impl)]
7#![allow(rustc::untranslatable_diagnostic)]
8#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
9#![doc(rust_logo)]
10#![feature(assert_matches)]
11#![feature(autodiff)]
12#![feature(box_patterns)]
13#![feature(decl_macro)]
14#![feature(if_let_guard)]
15#![feature(proc_macro_internals)]
16#![feature(proc_macro_quote)]
17#![feature(rustdoc_internals)]
18#![feature(try_blocks)]
19#![recursion_limit = "256"]
20// tidy-alphabetical-end
21
22use std::sync::Arc;
23
24use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
25use rustc_expand::proc_macro::BangProcMacro;
26use rustc_span::sym;
27
28use crate::deriving::*;
29
30mod alloc_error_handler;
31mod assert;
32mod autodiff;
33mod cfg;
34mod cfg_accessible;
35mod cfg_eval;
36mod cfg_select;
37mod compile_error;
38mod concat;
39mod concat_bytes;
40mod define_opaque;
41mod derive;
42mod deriving;
43mod edition_panic;
44mod env;
45mod errors;
46mod format;
47mod format_foreign;
48mod global_allocator;
49mod iter;
50mod log_syntax;
51mod pattern_type;
52mod source_util;
53mod test;
54mod trace_macros;
55
56pub mod asm;
57pub mod cmdline_attrs;
58pub mod contracts;
59pub mod proc_macro_harness;
60pub mod standard_library_imports;
61pub mod test_harness;
62pub mod util;
63
64rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
65
66pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
67    let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
68    macro register_bang($($name:ident: $f:expr,)*) {
69        $(register(sym::$name, SyntaxExtensionKind::LegacyBang(Arc::new($f as MacroExpanderFn)));)*
70    }
71    macro register_attr($($name:ident: $f:expr,)*) {
72        $(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Arc::new($f)));)*
73    }
74    macro register_derive($($name:ident: $f:expr,)*) {
75        $(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Arc::new(BuiltinDerive($f))));)*
76    }
77
78    register_bang! {
79        // tidy-alphabetical-start
80        asm: asm::expand_asm,
81        assert: assert::expand_assert,
82        cfg: cfg::expand_cfg,
83        cfg_select: cfg_select::expand_cfg_select,
84        column: source_util::expand_column,
85        compile_error: compile_error::expand_compile_error,
86        concat: concat::expand_concat,
87        concat_bytes: concat_bytes::expand_concat_bytes,
88        const_format_args: format::expand_format_args,
89        core_panic: edition_panic::expand_panic,
90        env: env::expand_env,
91        file: source_util::expand_file,
92        format_args: format::expand_format_args,
93        format_args_nl: format::expand_format_args_nl,
94        global_asm: asm::expand_global_asm,
95        include: source_util::expand_include,
96        include_bytes: source_util::expand_include_bytes,
97        include_str: source_util::expand_include_str,
98        iter: iter::expand,
99        line: source_util::expand_line,
100        log_syntax: log_syntax::expand_log_syntax,
101        module_path: source_util::expand_mod,
102        naked_asm: asm::expand_naked_asm,
103        option_env: env::expand_option_env,
104        pattern_type: pattern_type::expand,
105        std_panic: edition_panic::expand_panic,
106        stringify: source_util::expand_stringify,
107        trace_macros: trace_macros::expand_trace_macros,
108        unreachable: edition_panic::expand_unreachable,
109        // tidy-alphabetical-end
110    }
111
112    register_attr! {
113        // tidy-alphabetical-start
114        alloc_error_handler: alloc_error_handler::expand,
115        autodiff_forward: autodiff::expand_forward,
116        autodiff_reverse: autodiff::expand_reverse,
117        bench: test::expand_bench,
118        cfg_accessible: cfg_accessible::Expander,
119        cfg_eval: cfg_eval::expand,
120        define_opaque: define_opaque::expand,
121        derive: derive::Expander { is_const: false },
122        derive_const: derive::Expander { is_const: true },
123        global_allocator: global_allocator::expand,
124        test: test::expand_test,
125        test_case: test::expand_test_case,
126        // tidy-alphabetical-end
127    }
128
129    register_derive! {
130        Clone: clone::expand_deriving_clone,
131        Copy: bounds::expand_deriving_copy,
132        ConstParamTy: bounds::expand_deriving_const_param_ty,
133        UnsizedConstParamTy: bounds::expand_deriving_unsized_const_param_ty,
134        Debug: debug::expand_deriving_debug,
135        Default: default::expand_deriving_default,
136        Eq: eq::expand_deriving_eq,
137        Hash: hash::expand_deriving_hash,
138        Ord: ord::expand_deriving_ord,
139        PartialEq: partial_eq::expand_deriving_partial_eq,
140        PartialOrd: partial_ord::expand_deriving_partial_ord,
141        CoercePointee: coerce_pointee::expand_deriving_coerce_pointee,
142    }
143
144    let client = rustc_proc_macro::bridge::client::Client::expand1(rustc_proc_macro::quote);
145    register(sym::quote, SyntaxExtensionKind::Bang(Arc::new(BangProcMacro { client })));
146    let requires = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandRequires));
147    register(sym::contracts_requires, requires);
148    let ensures = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandEnsures));
149    register(sym::contracts_ensures, ensures);
150}