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