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