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#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
9#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
10#![doc(rust_logo)]
11#![feature(assert_matches)]
12#![feature(autodiff)]
13#![feature(box_patterns)]
14#![feature(decl_macro)]
15#![feature(if_let_guard)]
16#![feature(let_chains)]
17#![feature(proc_macro_internals)]
18#![feature(proc_macro_quote)]
19#![feature(rustdoc_internals)]
20#![feature(string_from_utf8_lossy_owned)]
21#![feature(try_blocks)]
22// tidy-alphabetical-end
23
24extern crate proc_macro;
25
26use std::sync::Arc;
27
28use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
29use rustc_expand::proc_macro::BangProcMacro;
30use rustc_span::sym;
31
32use crate::deriving::*;
33
34mod alloc_error_handler;
35mod assert;
36mod autodiff;
37mod cfg;
38mod cfg_accessible;
39mod cfg_eval;
40mod compile_error;
41mod concat;
42mod concat_bytes;
43mod concat_idents;
44mod define_opaque;
45mod derive;
46mod deriving;
47mod edition_panic;
48mod env;
49mod errors;
50mod format;
51mod format_foreign;
52mod global_allocator;
53mod log_syntax;
54mod pattern_type;
55mod source_util;
56mod test;
57mod trace_macros;
58
59pub mod asm;
60pub mod cmdline_attrs;
61pub mod contracts;
62pub mod proc_macro_harness;
63pub mod standard_library_imports;
64pub mod test_harness;
65pub mod util;
66
67rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
68
69pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
70    let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
71    macro register_bang($($name:ident: $f:expr,)*) {
72        $(register(sym::$name, SyntaxExtensionKind::LegacyBang(Arc::new($f as MacroExpanderFn)));)*
73    }
74    macro register_attr($($name:ident: $f:expr,)*) {
75        $(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Arc::new($f)));)*
76    }
77    macro register_derive($($name:ident: $f:expr,)*) {
78        $(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Arc::new(BuiltinDerive($f))));)*
79    }
80
81    register_bang! {
82        // tidy-alphabetical-start
83        asm: asm::expand_asm,
84        assert: assert::expand_assert,
85        cfg: cfg::expand_cfg,
86        column: source_util::expand_column,
87        compile_error: compile_error::expand_compile_error,
88        concat: concat::expand_concat,
89        concat_bytes: concat_bytes::expand_concat_bytes,
90        concat_idents: concat_idents::expand_concat_idents,
91        const_format_args: format::expand_format_args,
92        core_panic: edition_panic::expand_panic,
93        env: env::expand_env,
94        file: source_util::expand_file,
95        format_args: format::expand_format_args,
96        format_args_nl: format::expand_format_args_nl,
97        global_asm: asm::expand_global_asm,
98        include: source_util::expand_include,
99        include_bytes: source_util::expand_include_bytes,
100        include_str: source_util::expand_include_str,
101        line: source_util::expand_line,
102        log_syntax: log_syntax::expand_log_syntax,
103        module_path: source_util::expand_mod,
104        naked_asm: asm::expand_naked_asm,
105        option_env: env::expand_option_env,
106        pattern_type: pattern_type::expand,
107        std_panic: edition_panic::expand_panic,
108        stringify: source_util::expand_stringify,
109        trace_macros: trace_macros::expand_trace_macros,
110        unreachable: edition_panic::expand_unreachable,
111        // tidy-alphabetical-end
112    }
113
114    register_attr! {
115        alloc_error_handler: alloc_error_handler::expand,
116        autodiff: autodiff::expand,
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    }
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    }
142
143    let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
144    register(sym::quote, SyntaxExtensionKind::Bang(Arc::new(BangProcMacro { client })));
145    let requires = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandRequires));
146    register(sym::contracts_requires, requires);
147    let ensures = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandEnsures));
148    register(sym::contracts_ensures, ensures);
149}