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(let_chains)]
16#![feature(proc_macro_internals)]
17#![feature(proc_macro_quote)]
18#![feature(rustdoc_internals)]
19#![feature(string_from_utf8_lossy_owned)]
20#![feature(try_blocks)]
21#![warn(unreachable_pub)]
22// tidy-alphabetical-end
23
24extern crate proc_macro;
25
26use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
27use rustc_expand::proc_macro::BangProcMacro;
28use rustc_span::sym;
29
30use crate::deriving::*;
31
32mod alloc_error_handler;
33mod assert;
34mod autodiff;
35mod cfg;
36mod cfg_accessible;
37mod cfg_eval;
38mod compile_error;
39mod concat;
40mod concat_bytes;
41mod concat_idents;
42mod derive;
43mod deriving;
44mod edition_panic;
45mod env;
46mod errors;
47mod format;
48mod format_foreign;
49mod global_allocator;
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(Box::new($f as MacroExpanderFn)));)*
70    }
71    macro register_attr($($name:ident: $f:expr,)*) {
72        $(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Box::new($f)));)*
73    }
74    macro register_derive($($name:ident: $f:expr,)*) {
75        $(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Box::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        column: source_util::expand_column,
84        compile_error: compile_error::expand_compile_error,
85        concat: concat::expand_concat,
86        concat_bytes: concat_bytes::expand_concat_bytes,
87        concat_idents: concat_idents::expand_concat_idents,
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        line: source_util::expand_line,
99        log_syntax: log_syntax::expand_log_syntax,
100        module_path: source_util::expand_mod,
101        naked_asm: asm::expand_naked_asm,
102        option_env: env::expand_option_env,
103        pattern_type: pattern_type::expand,
104        std_panic: edition_panic::expand_panic,
105        stringify: source_util::expand_stringify,
106        trace_macros: trace_macros::expand_trace_macros,
107        unreachable: edition_panic::expand_unreachable,
108        // tidy-alphabetical-end
109    }
110
111    register_attr! {
112        alloc_error_handler: alloc_error_handler::expand,
113        autodiff: autodiff::expand,
114        bench: test::expand_bench,
115        cfg_accessible: cfg_accessible::Expander,
116        cfg_eval: cfg_eval::expand,
117        derive: derive::Expander { is_const: false },
118        derive_const: derive::Expander { is_const: true },
119        global_allocator: global_allocator::expand,
120        test: test::expand_test,
121        test_case: test::expand_test_case,
122    }
123
124    register_derive! {
125        Clone: clone::expand_deriving_clone,
126        Copy: bounds::expand_deriving_copy,
127        ConstParamTy: bounds::expand_deriving_const_param_ty,
128        UnsizedConstParamTy: bounds::expand_deriving_unsized_const_param_ty,
129        Debug: debug::expand_deriving_debug,
130        Default: default::expand_deriving_default,
131        Eq: eq::expand_deriving_eq,
132        Hash: hash::expand_deriving_hash,
133        Ord: ord::expand_deriving_ord,
134        PartialEq: partial_eq::expand_deriving_partial_eq,
135        PartialOrd: partial_ord::expand_deriving_partial_ord,
136        CoercePointee: coerce_pointee::expand_deriving_coerce_pointee,
137    }
138
139    let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
140    register(sym::quote, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })));
141    let requires = SyntaxExtensionKind::Attr(Box::new(contracts::ExpandRequires));
142    register(sym::contracts_requires, requires);
143    let ensures = SyntaxExtensionKind::Attr(Box::new(contracts::ExpandEnsures));
144    register(sym::contracts_ensures, ensures);
145}