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(box_patterns)]
12#![feature(decl_macro)]
13#![feature(if_let_guard)]
14#![feature(proc_macro_internals)]
15#![feature(proc_macro_quote)]
16#![feature(rustdoc_internals)]
17#![feature(try_blocks)]
18#![recursion_limit = "256"]
19// tidy-alphabetical-end
20
21use std::sync::Arc;
22
23use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
24use rustc_expand::proc_macro::BangProcMacro;
25use rustc_span::sym;
26
27use crate::deriving::*;
28
29mod alloc_error_handler;
30mod assert;
31mod autodiff;
32mod cfg;
33mod cfg_accessible;
34mod cfg_eval;
35mod cfg_select;
36mod compile_error;
37mod concat;
38mod concat_bytes;
39mod define_opaque;
40mod derive;
41mod deriving;
42mod edition_panic;
43mod env;
44mod errors;
45mod format;
46mod format_foreign;
47mod global_allocator;
48mod iter;
49mod log_syntax;
50mod pattern_type;
51mod source_util;
52mod test;
53mod trace_macros;
54
55pub mod asm;
56pub mod cmdline_attrs;
57pub mod contracts;
58pub mod proc_macro_harness;
59pub mod standard_library_imports;
60pub mod test_harness;
61pub mod util;
62
63rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
64
65pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
66    let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
67    macro register_bang($($name:ident: $f:expr,)*) {
68        $(register(sym::$name, SyntaxExtensionKind::LegacyBang(Arc::new($f as MacroExpanderFn)));)*
69    }
70    macro register_attr($($name:ident: $f:expr,)*) {
71        $(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Arc::new($f)));)*
72    }
73    macro register_derive($($name:ident: $f:expr,)*) {
74        $(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Arc::new(BuiltinDerive($f))));)*
75    }
76
77    register_bang! {
78        // tidy-alphabetical-start
79        asm: asm::expand_asm,
80        assert: assert::expand_assert,
81        cfg: cfg::expand_cfg,
82        cfg_select: cfg_select::expand_cfg_select,
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        const_format_args: format::expand_format_args,
88        core_panic: edition_panic::expand_panic,
89        env: env::expand_env,
90        file: source_util::expand_file,
91        format_args: format::expand_format_args,
92        format_args_nl: format::expand_format_args_nl,
93        global_asm: asm::expand_global_asm,
94        include: source_util::expand_include,
95        include_bytes: source_util::expand_include_bytes,
96        include_str: source_util::expand_include_str,
97        iter: iter::expand,
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        // tidy-alphabetical-start
113        alloc_error_handler: alloc_error_handler::expand,
114        autodiff_forward: autodiff::expand_forward,
115        autodiff_reverse: autodiff::expand_reverse,
116        bench: test::expand_bench,
117        cfg_accessible: cfg_accessible::Expander,
118        cfg_eval: cfg_eval::expand,
119        define_opaque: define_opaque::expand,
120        derive: derive::Expander { is_const: false },
121        derive_const: derive::Expander { is_const: true },
122        global_allocator: global_allocator::expand,
123        test: test::expand_test,
124        test_case: test::expand_test_case,
125        // tidy-alphabetical-end
126    }
127
128    register_derive! {
129        Clone: clone::expand_deriving_clone,
130        Copy: bounds::expand_deriving_copy,
131        ConstParamTy: bounds::expand_deriving_const_param_ty,
132        UnsizedConstParamTy: bounds::expand_deriving_unsized_const_param_ty,
133        Debug: debug::expand_deriving_debug,
134        Default: default::expand_deriving_default,
135        Eq: eq::expand_deriving_eq,
136        Hash: hash::expand_deriving_hash,
137        Ord: ord::expand_deriving_ord,
138        PartialEq: partial_eq::expand_deriving_partial_eq,
139        PartialOrd: partial_ord::expand_deriving_partial_ord,
140        CoercePointee: coerce_pointee::expand_deriving_coerce_pointee,
141        From: from::expand_deriving_from,
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}