rustc_builtin_macros/
proc_macro_harness.rs

1use std::{mem, slice};
2
3use rustc_ast::visit::{self, Visitor};
4use rustc_ast::{self as ast, HasNodeId, NodeId, attr};
5use rustc_ast_pretty::pprust;
6use rustc_attr_parsing::AttributeParser;
7use rustc_errors::DiagCtxtHandle;
8use rustc_expand::base::{ExtCtxt, ResolverExpand};
9use rustc_expand::expand::{AstFragment, ExpansionConfig};
10use rustc_feature::Features;
11use rustc_hir::attrs::AttributeKind;
12use rustc_session::Session;
13use rustc_span::hygiene::AstPass;
14use rustc_span::source_map::SourceMap;
15use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
16use smallvec::smallvec;
17use thin_vec::{ThinVec, thin_vec};
18
19use crate::errors;
20
21struct ProcMacroDerive {
22    id: NodeId,
23    trait_name: Symbol,
24    function_ident: Ident,
25    span: Span,
26    attrs: ThinVec<Symbol>,
27}
28
29struct ProcMacroDef {
30    id: NodeId,
31    function_ident: Ident,
32    span: Span,
33}
34
35enum ProcMacro {
36    Derive(ProcMacroDerive),
37    Attr(ProcMacroDef),
38    Bang(ProcMacroDef),
39}
40
41struct CollectProcMacros<'a> {
42    macros: Vec<ProcMacro>,
43    in_root: bool,
44    dcx: DiagCtxtHandle<'a>,
45    session: &'a Session,
46    source_map: &'a SourceMap,
47    is_proc_macro_crate: bool,
48    is_test_crate: bool,
49}
50
51pub fn inject(
52    krate: &mut ast::Crate,
53    sess: &Session,
54    features: &Features,
55    resolver: &mut dyn ResolverExpand,
56    is_proc_macro_crate: bool,
57    has_proc_macro_decls: bool,
58    is_test_crate: bool,
59    dcx: DiagCtxtHandle<'_>,
60) {
61    let ecfg = ExpansionConfig::default(sym::proc_macro, features);
62    let mut cx = ExtCtxt::new(sess, ecfg, resolver, None);
63
64    let mut collect = CollectProcMacros {
65        macros: Vec::new(),
66        in_root: true,
67        dcx,
68        session: sess,
69        source_map: sess.source_map(),
70        is_proc_macro_crate,
71        is_test_crate,
72    };
73
74    if has_proc_macro_decls || is_proc_macro_crate {
75        visit::walk_crate(&mut collect, krate);
76    }
77    let macros = collect.macros;
78
79    if !is_proc_macro_crate {
80        return;
81    }
82
83    if is_test_crate {
84        return;
85    }
86
87    let decls = mk_decls(&mut cx, &macros);
88    krate.items.push(decls);
89}
90
91impl<'a> CollectProcMacros<'a> {
92    fn check_not_pub_in_root(&self, vis: &ast::Visibility, sp: Span) {
93        if self.is_proc_macro_crate && self.in_root && vis.kind.is_pub() {
94            self.dcx.emit_err(errors::ProcMacro { span: sp });
95        }
96    }
97
98    fn collect_custom_derive(
99        &mut self,
100        item: &'a ast::Item,
101        function_ident: Ident,
102        attr: &'a ast::Attribute,
103    ) {
104        let Some(rustc_hir::Attribute::Parsed(AttributeKind::ProcMacroDerive {
105            trait_name,
106            helper_attrs,
107            ..
108        })) = AttributeParser::parse_limited(
109            self.session,
110            slice::from_ref(attr),
111            sym::proc_macro_derive,
112            item.span,
113            item.node_id(),
114            None,
115        )
116        else {
117            return;
118        };
119
120        if self.in_root && item.vis.kind.is_pub() {
121            self.macros.push(ProcMacro::Derive(ProcMacroDerive {
122                id: item.id,
123                span: item.span,
124                trait_name,
125                function_ident,
126                attrs: helper_attrs,
127            }));
128        } else {
129            let msg = if !self.in_root {
130                "functions tagged with `#[proc_macro_derive]` must \
131                 currently reside in the root of the crate"
132            } else {
133                "functions tagged with `#[proc_macro_derive]` must be `pub`"
134            };
135            self.dcx.span_err(self.source_map.guess_head_span(item.span), msg);
136        }
137    }
138
139    fn collect_attr_proc_macro(&mut self, item: &'a ast::Item, function_ident: Ident) {
140        if self.in_root && item.vis.kind.is_pub() {
141            self.macros.push(ProcMacro::Attr(ProcMacroDef {
142                id: item.id,
143                span: item.span,
144                function_ident,
145            }));
146        } else {
147            let msg = if !self.in_root {
148                "functions tagged with `#[proc_macro_attribute]` must \
149                 currently reside in the root of the crate"
150            } else {
151                "functions tagged with `#[proc_macro_attribute]` must be `pub`"
152            };
153            self.dcx.span_err(self.source_map.guess_head_span(item.span), msg);
154        }
155    }
156
157    fn collect_bang_proc_macro(&mut self, item: &'a ast::Item, function_ident: Ident) {
158        if self.in_root && item.vis.kind.is_pub() {
159            self.macros.push(ProcMacro::Bang(ProcMacroDef {
160                id: item.id,
161                span: item.span,
162                function_ident,
163            }));
164        } else {
165            let msg = if !self.in_root {
166                "functions tagged with `#[proc_macro]` must \
167                 currently reside in the root of the crate"
168            } else {
169                "functions tagged with `#[proc_macro]` must be `pub`"
170            };
171            self.dcx.span_err(self.source_map.guess_head_span(item.span), msg);
172        }
173    }
174}
175
176impl<'a> Visitor<'a> for CollectProcMacros<'a> {
177    fn visit_item(&mut self, item: &'a ast::Item) {
178        if let ast::ItemKind::MacroDef(..) = item.kind {
179            if self.is_proc_macro_crate && attr::contains_name(&item.attrs, sym::macro_export) {
180                self.dcx.emit_err(errors::ExportMacroRules {
181                    span: self.source_map.guess_head_span(item.span),
182                });
183            }
184        }
185
186        let mut found_attr: Option<&'a ast::Attribute> = None;
187
188        for attr in &item.attrs {
189            if attr.is_proc_macro_attr() {
190                if let Some(prev_attr) = found_attr {
191                    let prev_item = prev_attr.get_normal_item();
192                    let item = attr.get_normal_item();
193                    let path_str = pprust::path_to_string(&item.path);
194                    let msg = if item.path.segments[0].ident.name
195                        == prev_item.path.segments[0].ident.name
196                    {
197                        format!(
198                            "only one `#[{path_str}]` attribute is allowed on any given function",
199                        )
200                    } else {
201                        format!(
202                            "`#[{}]` and `#[{}]` attributes cannot both be applied
203                            to the same function",
204                            path_str,
205                            pprust::path_to_string(&prev_item.path),
206                        )
207                    };
208
209                    self.dcx
210                        .struct_span_err(attr.span, msg)
211                        .with_span_label(prev_attr.span, "previous attribute here")
212                        .emit();
213
214                    return;
215                }
216
217                found_attr = Some(attr);
218            }
219        }
220
221        let Some(attr) = found_attr else {
222            self.check_not_pub_in_root(&item.vis, self.source_map.guess_head_span(item.span));
223            let prev_in_root = mem::replace(&mut self.in_root, false);
224            visit::walk_item(self, item);
225            self.in_root = prev_in_root;
226            return;
227        };
228
229        // Make sure we're checking a bare function. If we're not then we're
230        // just not interested any further in this item.
231        let fn_ident = if let ast::ItemKind::Fn(fn_) = &item.kind {
232            fn_.ident
233        } else {
234            self.dcx
235                .create_err(errors::AttributeOnlyBeUsedOnBareFunctions {
236                    span: attr.span,
237                    path: &pprust::path_to_string(&attr.get_normal_item().path),
238                })
239                .emit();
240            return;
241        };
242
243        if self.is_test_crate {
244            return;
245        }
246
247        if !self.is_proc_macro_crate {
248            self.dcx
249                .create_err(errors::AttributeOnlyUsableWithCrateType {
250                    span: attr.span,
251                    path: &pprust::path_to_string(&attr.get_normal_item().path),
252                })
253                .emit();
254            return;
255        }
256
257        // Try to locate a `#[proc_macro_derive]` attribute.
258        if attr.has_name(sym::proc_macro_derive) {
259            self.collect_custom_derive(item, fn_ident, attr);
260        } else if attr.has_name(sym::proc_macro_attribute) {
261            self.collect_attr_proc_macro(item, fn_ident);
262        } else if attr.has_name(sym::proc_macro) {
263            self.collect_bang_proc_macro(item, fn_ident);
264        };
265
266        let prev_in_root = mem::replace(&mut self.in_root, false);
267        visit::walk_item(self, item);
268        self.in_root = prev_in_root;
269    }
270}
271
272// Creates a new module which looks like:
273//
274//      const _: () = {
275//          extern crate proc_macro;
276//
277//          use proc_macro::bridge::client::ProcMacro;
278//
279//          #[rustc_proc_macro_decls]
280//          #[used]
281//          #[allow(deprecated)]
282//          static DECLS: &[ProcMacro] = &[
283//              ProcMacro::custom_derive($name_trait1, &[], ::$name1);
284//              ProcMacro::custom_derive($name_trait2, &["attribute_name"], ::$name2);
285//              // ...
286//          ];
287//      }
288fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> Box<ast::Item> {
289    let expn_id = cx.resolver.expansion_for_ast_pass(
290        DUMMY_SP,
291        AstPass::ProcMacroHarness,
292        &[sym::rustc_attrs, sym::proc_macro_internals],
293        None,
294    );
295    let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
296
297    let proc_macro = Ident::new(sym::proc_macro, span);
298    let krate = cx.item(span, ast::AttrVec::new(), ast::ItemKind::ExternCrate(None, proc_macro));
299
300    let bridge = Ident::new(sym::bridge, span);
301    let client = Ident::new(sym::client, span);
302    let proc_macro_ty = Ident::new(sym::ProcMacro, span);
303    let custom_derive = Ident::new(sym::custom_derive, span);
304    let attr = Ident::new(sym::attr, span);
305    let bang = Ident::new(sym::bang, span);
306
307    // We add NodeIds to 'resolver.proc_macros' in the order
308    // that we generate expressions. The position of each NodeId
309    // in the 'proc_macros' Vec corresponds to its position
310    // in the static array that will be generated
311    let decls = macros
312        .iter()
313        .map(|m| {
314            let harness_span = span;
315            let span = match m {
316                ProcMacro::Derive(m) => m.span,
317                ProcMacro::Attr(m) | ProcMacro::Bang(m) => m.span,
318            };
319            let local_path = |cx: &ExtCtxt<'_>, ident| cx.expr_path(cx.path(span, vec![ident]));
320            let proc_macro_ty_method_path = |cx: &ExtCtxt<'_>, method| {
321                cx.expr_path(cx.path(
322                    span.with_ctxt(harness_span.ctxt()),
323                    vec![proc_macro, bridge, client, proc_macro_ty, method],
324                ))
325            };
326            match m {
327                ProcMacro::Derive(cd) => {
328                    cx.resolver.declare_proc_macro(cd.id);
329                    // The call needs to use `harness_span` so that the const stability checker
330                    // accepts it.
331                    cx.expr_call(
332                        harness_span,
333                        proc_macro_ty_method_path(cx, custom_derive),
334                        thin_vec![
335                            cx.expr_str(span, cd.trait_name),
336                            cx.expr_array_ref(
337                                span,
338                                cd.attrs
339                                    .iter()
340                                    .map(|&s| cx.expr_str(span, s))
341                                    .collect::<ThinVec<_>>(),
342                            ),
343                            local_path(cx, cd.function_ident),
344                        ],
345                    )
346                }
347                ProcMacro::Attr(ca) | ProcMacro::Bang(ca) => {
348                    cx.resolver.declare_proc_macro(ca.id);
349                    let ident = match m {
350                        ProcMacro::Attr(_) => attr,
351                        ProcMacro::Bang(_) => bang,
352                        ProcMacro::Derive(_) => unreachable!(),
353                    };
354
355                    // The call needs to use `harness_span` so that the const stability checker
356                    // accepts it.
357                    cx.expr_call(
358                        harness_span,
359                        proc_macro_ty_method_path(cx, ident),
360                        thin_vec![
361                            cx.expr_str(span, ca.function_ident.name),
362                            local_path(cx, ca.function_ident),
363                        ],
364                    )
365                }
366            }
367        })
368        .collect();
369
370    let mut decls_static = cx.item_static(
371        span,
372        Ident::new(sym::_DECLS, span),
373        cx.ty_ref(
374            span,
375            cx.ty(
376                span,
377                ast::TyKind::Slice(
378                    cx.ty_path(cx.path(span, vec![proc_macro, bridge, client, proc_macro_ty])),
379                ),
380            ),
381            None,
382            ast::Mutability::Not,
383        ),
384        ast::Mutability::Not,
385        cx.expr_array_ref(span, decls),
386    );
387    decls_static.attrs.extend([
388        cx.attr_word(sym::rustc_proc_macro_decls, span),
389        cx.attr_word(sym::used, span),
390        cx.attr_nested_word(sym::allow, sym::deprecated, span),
391    ]);
392
393    let block = cx.expr_block(
394        cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
395    );
396
397    let anon_constant = cx.item_const(
398        span,
399        Ident::new(kw::Underscore, span),
400        cx.ty(span, ast::TyKind::Tup(ThinVec::new())),
401        block,
402    );
403
404    // Integrate the new item into existing module structures.
405    let items = AstFragment::Items(smallvec![anon_constant]);
406    cx.monotonic_expander().fully_expand_fragment(items).make_items().pop().unwrap()
407}