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