]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_expand/src/expand.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / compiler / rustc_expand / src / expand.rs
1 use crate::base::*;
2 use crate::config::StripUnconfigured;
3 use crate::configure;
4 use crate::hygiene::{ExpnData, ExpnKind, SyntaxContext};
5 use crate::mbe::macro_rules::annotate_err_with_kind;
6 use crate::module::{parse_external_mod, push_directory, Directory, DirectoryOwnership};
7 use crate::placeholders::{placeholder, PlaceholderExpander};
8 use crate::proc_macro::collect_derives;
9
10 use rustc_ast::mut_visit::*;
11 use rustc_ast::ptr::P;
12 use rustc_ast::token;
13 use rustc_ast::tokenstream::TokenStream;
14 use rustc_ast::visit::{self, AssocCtxt, Visitor};
15 use rustc_ast::{self as ast, AttrItem, Block, LitKind, NodeId, PatKind, Path};
16 use rustc_ast::{ItemKind, MacArgs, MacCallStmt, MacStmtStyle, StmtKind, Unsafe};
17 use rustc_ast_pretty::pprust;
18 use rustc_attr::{self as attr, is_builtin_attr, HasAttrs};
19 use rustc_data_structures::map_in_place::MapInPlace;
20 use rustc_data_structures::stack::ensure_sufficient_stack;
21 use rustc_errors::{Applicability, PResult};
22 use rustc_feature::Features;
23 use rustc_parse::parser::Parser;
24 use rustc_parse::validate_attr;
25 use rustc_session::lint::builtin::UNUSED_DOC_COMMENTS;
26 use rustc_session::lint::BuiltinLintDiagnostics;
27 use rustc_session::parse::{feature_err, ParseSess};
28 use rustc_session::Limit;
29 use rustc_span::symbol::{sym, Ident, Symbol};
30 use rustc_span::{ExpnId, FileName, Span, DUMMY_SP};
31
32 use smallvec::{smallvec, SmallVec};
33 use std::io::ErrorKind;
34 use std::ops::DerefMut;
35 use std::path::PathBuf;
36 use std::rc::Rc;
37 use std::{iter, mem, slice};
38
39 macro_rules! ast_fragments {
40 (
41 $($Kind:ident($AstTy:ty) {
42 $kind_name:expr;
43 $(one fn $mut_visit_ast:ident; fn $visit_ast:ident;)?
44 $(many fn $flat_map_ast_elt:ident; fn $visit_ast_elt:ident($($args:tt)*);)?
45 fn $make_ast:ident;
46 })*
47 ) => {
48 /// A fragment of AST that can be produced by a single macro expansion.
49 /// Can also serve as an input and intermediate result for macro expansion operations.
50 pub enum AstFragment {
51 OptExpr(Option<P<ast::Expr>>),
52 $($Kind($AstTy),)*
53 }
54
55 /// "Discriminant" of an AST fragment.
56 #[derive(Copy, Clone, PartialEq, Eq)]
57 pub enum AstFragmentKind {
58 OptExpr,
59 $($Kind,)*
60 }
61
62 impl AstFragmentKind {
63 pub fn name(self) -> &'static str {
64 match self {
65 AstFragmentKind::OptExpr => "expression",
66 $(AstFragmentKind::$Kind => $kind_name,)*
67 }
68 }
69
70 fn make_from<'a>(self, result: Box<dyn MacResult + 'a>) -> Option<AstFragment> {
71 match self {
72 AstFragmentKind::OptExpr =>
73 result.make_expr().map(Some).map(AstFragment::OptExpr),
74 $(AstFragmentKind::$Kind => result.$make_ast().map(AstFragment::$Kind),)*
75 }
76 }
77 }
78
79 impl AstFragment {
80 pub fn add_placeholders(&mut self, placeholders: &[NodeId]) {
81 if placeholders.is_empty() {
82 return;
83 }
84 match self {
85 $($(AstFragment::$Kind(ast) => ast.extend(placeholders.iter().flat_map(|id| {
86 // We are repeating through arguments with `many`, to do that we have to
87 // mention some macro variable from those arguments even if it's not used.
88 macro _repeating($flat_map_ast_elt) {}
89 placeholder(AstFragmentKind::$Kind, *id, None).$make_ast()
90 })),)?)*
91 _ => panic!("unexpected AST fragment kind")
92 }
93 }
94
95 pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
96 match self {
97 AstFragment::OptExpr(expr) => expr,
98 _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
99 }
100 }
101
102 $(pub fn $make_ast(self) -> $AstTy {
103 match self {
104 AstFragment::$Kind(ast) => ast,
105 _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
106 }
107 })*
108
109 pub fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
110 match self {
111 AstFragment::OptExpr(opt_expr) => {
112 visit_clobber(opt_expr, |opt_expr| {
113 if let Some(expr) = opt_expr {
114 vis.filter_map_expr(expr)
115 } else {
116 None
117 }
118 });
119 }
120 $($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
121 $($(AstFragment::$Kind(ast) =>
122 ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast)),)?)*
123 }
124 }
125
126 pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
127 match *self {
128 AstFragment::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
129 AstFragment::OptExpr(None) => {}
130 $($(AstFragment::$Kind(ref ast) => visitor.$visit_ast(ast),)?)*
131 $($(AstFragment::$Kind(ref ast) => for ast_elt in &ast[..] {
132 visitor.$visit_ast_elt(ast_elt, $($args)*);
133 })?)*
134 }
135 }
136 }
137
138 impl<'a> MacResult for crate::mbe::macro_rules::ParserAnyMacro<'a> {
139 $(fn $make_ast(self: Box<crate::mbe::macro_rules::ParserAnyMacro<'a>>)
140 -> Option<$AstTy> {
141 Some(self.make(AstFragmentKind::$Kind).$make_ast())
142 })*
143 }
144 }
145 }
146
147 ast_fragments! {
148 Expr(P<ast::Expr>) { "expression"; one fn visit_expr; fn visit_expr; fn make_expr; }
149 Pat(P<ast::Pat>) { "pattern"; one fn visit_pat; fn visit_pat; fn make_pat; }
150 Ty(P<ast::Ty>) { "type"; one fn visit_ty; fn visit_ty; fn make_ty; }
151 Stmts(SmallVec<[ast::Stmt; 1]>) {
152 "statement"; many fn flat_map_stmt; fn visit_stmt(); fn make_stmts;
153 }
154 Items(SmallVec<[P<ast::Item>; 1]>) {
155 "item"; many fn flat_map_item; fn visit_item(); fn make_items;
156 }
157 TraitItems(SmallVec<[P<ast::AssocItem>; 1]>) {
158 "trait item";
159 many fn flat_map_trait_item;
160 fn visit_assoc_item(AssocCtxt::Trait);
161 fn make_trait_items;
162 }
163 ImplItems(SmallVec<[P<ast::AssocItem>; 1]>) {
164 "impl item";
165 many fn flat_map_impl_item;
166 fn visit_assoc_item(AssocCtxt::Impl);
167 fn make_impl_items;
168 }
169 ForeignItems(SmallVec<[P<ast::ForeignItem>; 1]>) {
170 "foreign item";
171 many fn flat_map_foreign_item;
172 fn visit_foreign_item();
173 fn make_foreign_items;
174 }
175 Arms(SmallVec<[ast::Arm; 1]>) {
176 "match arm"; many fn flat_map_arm; fn visit_arm(); fn make_arms;
177 }
178 Fields(SmallVec<[ast::Field; 1]>) {
179 "field expression"; many fn flat_map_field; fn visit_field(); fn make_fields;
180 }
181 FieldPats(SmallVec<[ast::FieldPat; 1]>) {
182 "field pattern";
183 many fn flat_map_field_pattern;
184 fn visit_field_pattern();
185 fn make_field_patterns;
186 }
187 GenericParams(SmallVec<[ast::GenericParam; 1]>) {
188 "generic parameter";
189 many fn flat_map_generic_param;
190 fn visit_generic_param();
191 fn make_generic_params;
192 }
193 Params(SmallVec<[ast::Param; 1]>) {
194 "function parameter"; many fn flat_map_param; fn visit_param(); fn make_params;
195 }
196 StructFields(SmallVec<[ast::StructField; 1]>) {
197 "field";
198 many fn flat_map_struct_field;
199 fn visit_struct_field();
200 fn make_struct_fields;
201 }
202 Variants(SmallVec<[ast::Variant; 1]>) {
203 "variant"; many fn flat_map_variant; fn visit_variant(); fn make_variants;
204 }
205 }
206
207 impl AstFragmentKind {
208 crate fn dummy(self, span: Span) -> AstFragment {
209 self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
210 }
211
212 fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(
213 self,
214 items: I,
215 ) -> AstFragment {
216 let mut items = items.into_iter();
217 match self {
218 AstFragmentKind::Arms => {
219 AstFragment::Arms(items.map(Annotatable::expect_arm).collect())
220 }
221 AstFragmentKind::Fields => {
222 AstFragment::Fields(items.map(Annotatable::expect_field).collect())
223 }
224 AstFragmentKind::FieldPats => {
225 AstFragment::FieldPats(items.map(Annotatable::expect_field_pattern).collect())
226 }
227 AstFragmentKind::GenericParams => {
228 AstFragment::GenericParams(items.map(Annotatable::expect_generic_param).collect())
229 }
230 AstFragmentKind::Params => {
231 AstFragment::Params(items.map(Annotatable::expect_param).collect())
232 }
233 AstFragmentKind::StructFields => {
234 AstFragment::StructFields(items.map(Annotatable::expect_struct_field).collect())
235 }
236 AstFragmentKind::Variants => {
237 AstFragment::Variants(items.map(Annotatable::expect_variant).collect())
238 }
239 AstFragmentKind::Items => {
240 AstFragment::Items(items.map(Annotatable::expect_item).collect())
241 }
242 AstFragmentKind::ImplItems => {
243 AstFragment::ImplItems(items.map(Annotatable::expect_impl_item).collect())
244 }
245 AstFragmentKind::TraitItems => {
246 AstFragment::TraitItems(items.map(Annotatable::expect_trait_item).collect())
247 }
248 AstFragmentKind::ForeignItems => {
249 AstFragment::ForeignItems(items.map(Annotatable::expect_foreign_item).collect())
250 }
251 AstFragmentKind::Stmts => {
252 AstFragment::Stmts(items.map(Annotatable::expect_stmt).collect())
253 }
254 AstFragmentKind::Expr => AstFragment::Expr(
255 items.next().expect("expected exactly one expression").expect_expr(),
256 ),
257 AstFragmentKind::OptExpr => {
258 AstFragment::OptExpr(items.next().map(Annotatable::expect_expr))
259 }
260 AstFragmentKind::Pat | AstFragmentKind::Ty => {
261 panic!("patterns and types aren't annotatable")
262 }
263 }
264 }
265 }
266
267 pub struct Invocation {
268 pub kind: InvocationKind,
269 pub fragment_kind: AstFragmentKind,
270 pub expansion_data: ExpansionData,
271 }
272
273 pub enum InvocationKind {
274 Bang {
275 mac: ast::MacCall,
276 span: Span,
277 },
278 Attr {
279 attr: ast::Attribute,
280 item: Annotatable,
281 // Required for resolving derive helper attributes.
282 derives: Vec<Path>,
283 // We temporarily report errors for attribute macros placed after derives
284 after_derive: bool,
285 },
286 Derive {
287 path: Path,
288 item: Annotatable,
289 },
290 /// "Invocation" that contains all derives from an item,
291 /// broken into multiple `Derive` invocations when expanded.
292 /// FIXME: Find a way to remove it.
293 DeriveContainer {
294 derives: Vec<Path>,
295 item: Annotatable,
296 },
297 }
298
299 impl InvocationKind {
300 fn placeholder_visibility(&self) -> Option<ast::Visibility> {
301 // HACK: For unnamed fields placeholders should have the same visibility as the actual
302 // fields because for tuple structs/variants resolve determines visibilities of their
303 // constructor using these field visibilities before attributes on them are are expanded.
304 // The assumption is that the attribute expansion cannot change field visibilities,
305 // and it holds because only inert attributes are supported in this position.
306 match self {
307 InvocationKind::Attr { item: Annotatable::StructField(field), .. }
308 | InvocationKind::Derive { item: Annotatable::StructField(field), .. }
309 | InvocationKind::DeriveContainer { item: Annotatable::StructField(field), .. }
310 if field.ident.is_none() =>
311 {
312 Some(field.vis.clone())
313 }
314 _ => None,
315 }
316 }
317 }
318
319 impl Invocation {
320 pub fn span(&self) -> Span {
321 match &self.kind {
322 InvocationKind::Bang { span, .. } => *span,
323 InvocationKind::Attr { attr, .. } => attr.span,
324 InvocationKind::Derive { path, .. } => path.span,
325 InvocationKind::DeriveContainer { item, .. } => item.span(),
326 }
327 }
328 }
329
330 pub struct MacroExpander<'a, 'b> {
331 pub cx: &'a mut ExtCtxt<'b>,
332 monotonic: bool, // cf. `cx.monotonic_expander()`
333 }
334
335 impl<'a, 'b> MacroExpander<'a, 'b> {
336 pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
337 MacroExpander { cx, monotonic }
338 }
339
340 pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
341 let mut module = ModuleData {
342 mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
343 directory: match self.cx.source_map().span_to_unmapped_path(krate.span) {
344 FileName::Real(name) => name.into_local_path(),
345 other => PathBuf::from(other.to_string()),
346 },
347 };
348 module.directory.pop();
349 self.cx.root_path = module.directory.clone();
350 self.cx.current_expansion.module = Rc::new(module);
351
352 let orig_mod_span = krate.module.inner;
353
354 let krate_item = AstFragment::Items(smallvec![P(ast::Item {
355 attrs: krate.attrs,
356 span: krate.span,
357 kind: ast::ItemKind::Mod(krate.module),
358 ident: Ident::invalid(),
359 id: ast::DUMMY_NODE_ID,
360 vis: ast::Visibility {
361 span: krate.span.shrink_to_lo(),
362 kind: ast::VisibilityKind::Public,
363 tokens: None,
364 },
365 tokens: None,
366 })]);
367
368 match self.fully_expand_fragment(krate_item).make_items().pop().map(P::into_inner) {
369 Some(ast::Item { attrs, kind: ast::ItemKind::Mod(module), .. }) => {
370 krate.attrs = attrs;
371 krate.module = module;
372 }
373 None => {
374 // Resolution failed so we return an empty expansion
375 krate.attrs = vec![];
376 krate.module = ast::Mod {
377 inner: orig_mod_span,
378 unsafety: Unsafe::No,
379 items: vec![],
380 inline: true,
381 };
382 }
383 Some(ast::Item { span, kind, .. }) => {
384 krate.attrs = vec![];
385 krate.module = ast::Mod {
386 inner: orig_mod_span,
387 unsafety: Unsafe::No,
388 items: vec![],
389 inline: true,
390 };
391 self.cx.span_err(
392 span,
393 &format!(
394 "expected crate top-level item to be a module after macro expansion, found {} {}",
395 kind.article(), kind.descr()
396 ),
397 );
398 }
399 };
400 self.cx.trace_macros_diag();
401 krate
402 }
403
404 // Recursively expand all macro invocations in this AST fragment.
405 pub fn fully_expand_fragment(&mut self, input_fragment: AstFragment) -> AstFragment {
406 let orig_expansion_data = self.cx.current_expansion.clone();
407 self.cx.current_expansion.depth = 0;
408
409 // Collect all macro invocations and replace them with placeholders.
410 let (mut fragment_with_placeholders, mut invocations) =
411 self.collect_invocations(input_fragment, &[]);
412
413 // Optimization: if we resolve all imports now,
414 // we'll be able to immediately resolve most of imported macros.
415 self.resolve_imports();
416
417 // Resolve paths in all invocations and produce output expanded fragments for them, but
418 // do not insert them into our input AST fragment yet, only store in `expanded_fragments`.
419 // The output fragments also go through expansion recursively until no invocations are left.
420 // Unresolved macros produce dummy outputs as a recovery measure.
421 invocations.reverse();
422 let mut expanded_fragments = Vec::new();
423 let mut undetermined_invocations = Vec::new();
424 let (mut progress, mut force) = (false, !self.monotonic);
425 loop {
426 let (invoc, res) = if let Some(invoc) = invocations.pop() {
427 invoc
428 } else {
429 self.resolve_imports();
430 if undetermined_invocations.is_empty() {
431 break;
432 }
433 invocations = mem::take(&mut undetermined_invocations);
434 force = !mem::replace(&mut progress, false);
435 continue;
436 };
437
438 let res = match res {
439 Some(res) => res,
440 None => {
441 let eager_expansion_root = if self.monotonic {
442 invoc.expansion_data.id
443 } else {
444 orig_expansion_data.id
445 };
446 match self.cx.resolver.resolve_macro_invocation(
447 &invoc,
448 eager_expansion_root,
449 force,
450 ) {
451 Ok(res) => res,
452 Err(Indeterminate) => {
453 // Cannot resolve, will retry this invocation later.
454 undetermined_invocations.push((invoc, None));
455 continue;
456 }
457 }
458 }
459 };
460
461 let ExpansionData { depth, id: expn_id, .. } = invoc.expansion_data;
462 self.cx.current_expansion = invoc.expansion_data.clone();
463
464 // FIXME(jseyfried): Refactor out the following logic
465 let (expanded_fragment, new_invocations) = match res {
466 InvocationRes::Single(ext) => match self.expand_invoc(invoc, &ext.kind) {
467 ExpandResult::Ready(fragment) => self.collect_invocations(fragment, &[]),
468 ExpandResult::Retry(invoc, explanation) => {
469 if force {
470 // We are stuck, stop retrying and produce a dummy fragment.
471 let span = invoc.span();
472 self.cx.span_err(span, &explanation);
473 let fragment = invoc.fragment_kind.dummy(span);
474 self.collect_invocations(fragment, &[])
475 } else {
476 // Cannot expand, will retry this invocation later.
477 undetermined_invocations
478 .push((invoc, Some(InvocationRes::Single(ext))));
479 continue;
480 }
481 }
482 },
483 InvocationRes::DeriveContainer(_exts) => {
484 // FIXME: Consider using the derive resolutions (`_exts`) immediately,
485 // instead of enqueuing the derives to be resolved again later.
486 let (derives, item) = match invoc.kind {
487 InvocationKind::DeriveContainer { derives, item } => (derives, item),
488 _ => unreachable!(),
489 };
490 if !item.derive_allowed() {
491 self.error_derive_forbidden_on_non_adt(&derives, &item);
492 }
493
494 let mut item = self.fully_configure(item);
495 item.visit_attrs(|attrs| attrs.retain(|a| !a.has_name(sym::derive)));
496
497 let mut derive_placeholders = Vec::with_capacity(derives.len());
498 invocations.reserve(derives.len());
499 for path in derives {
500 let expn_id = ExpnId::fresh(None);
501 derive_placeholders.push(NodeId::placeholder_from_expn_id(expn_id));
502 invocations.push((
503 Invocation {
504 kind: InvocationKind::Derive { path, item: item.clone() },
505 fragment_kind: invoc.fragment_kind,
506 expansion_data: ExpansionData {
507 id: expn_id,
508 ..invoc.expansion_data.clone()
509 },
510 },
511 None,
512 ));
513 }
514 let fragment =
515 invoc.fragment_kind.expect_from_annotatables(::std::iter::once(item));
516 self.collect_invocations(fragment, &derive_placeholders)
517 }
518 };
519
520 progress = true;
521 if expanded_fragments.len() < depth {
522 expanded_fragments.push(Vec::new());
523 }
524 expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
525 invocations.extend(new_invocations.into_iter().rev());
526 }
527
528 self.cx.current_expansion = orig_expansion_data;
529
530 // Finally incorporate all the expanded macros into the input AST fragment.
531 let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
532 while let Some(expanded_fragments) = expanded_fragments.pop() {
533 for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
534 placeholder_expander
535 .add(NodeId::placeholder_from_expn_id(expn_id), expanded_fragment);
536 }
537 }
538 fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
539 fragment_with_placeholders
540 }
541
542 fn error_derive_forbidden_on_non_adt(&self, derives: &[Path], item: &Annotatable) {
543 let attr = self.cx.sess.find_by_name(item.attrs(), sym::derive);
544 let span = attr.map_or(item.span(), |attr| attr.span);
545 let mut err = rustc_errors::struct_span_err!(
546 self.cx.sess,
547 span,
548 E0774,
549 "`derive` may only be applied to structs, enums and unions",
550 );
551 if let Some(ast::Attribute { style: ast::AttrStyle::Inner, .. }) = attr {
552 let trait_list = derives.iter().map(|t| pprust::path_to_string(t)).collect::<Vec<_>>();
553 let suggestion = format!("#[derive({})]", trait_list.join(", "));
554 err.span_suggestion(
555 span,
556 "try an outer attribute",
557 suggestion,
558 // We don't 𝑘𝑛𝑜𝑤 that the following item is an ADT
559 Applicability::MaybeIncorrect,
560 );
561 }
562 err.emit();
563 }
564
565 fn resolve_imports(&mut self) {
566 if self.monotonic {
567 self.cx.resolver.resolve_imports();
568 }
569 }
570
571 /// Collects all macro invocations reachable at this time in this AST fragment, and replace
572 /// them with "placeholders" - dummy macro invocations with specially crafted `NodeId`s.
573 /// Then call into resolver that builds a skeleton ("reduced graph") of the fragment and
574 /// prepares data for resolving paths of macro invocations.
575 fn collect_invocations(
576 &mut self,
577 mut fragment: AstFragment,
578 extra_placeholders: &[NodeId],
579 ) -> (AstFragment, Vec<(Invocation, Option<InvocationRes>)>) {
580 // Resolve `$crate`s in the fragment for pretty-printing.
581 self.cx.resolver.resolve_dollar_crates();
582
583 let invocations = {
584 let mut collector = InvocationCollector {
585 cfg: StripUnconfigured { sess: &self.cx.sess, features: self.cx.ecfg.features },
586 cx: self.cx,
587 invocations: Vec::new(),
588 monotonic: self.monotonic,
589 };
590 fragment.mut_visit_with(&mut collector);
591 fragment.add_placeholders(extra_placeholders);
592 collector.invocations
593 };
594
595 if self.monotonic {
596 self.cx
597 .resolver
598 .visit_ast_fragment_with_placeholders(self.cx.current_expansion.id, &fragment);
599 }
600
601 (fragment, invocations)
602 }
603
604 fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
605 let mut cfg = StripUnconfigured { sess: &self.cx.sess, features: self.cx.ecfg.features };
606 // Since the item itself has already been configured by the InvocationCollector,
607 // we know that fold result vector will contain exactly one element
608 match item {
609 Annotatable::Item(item) => Annotatable::Item(cfg.flat_map_item(item).pop().unwrap()),
610 Annotatable::TraitItem(item) => {
611 Annotatable::TraitItem(cfg.flat_map_trait_item(item).pop().unwrap())
612 }
613 Annotatable::ImplItem(item) => {
614 Annotatable::ImplItem(cfg.flat_map_impl_item(item).pop().unwrap())
615 }
616 Annotatable::ForeignItem(item) => {
617 Annotatable::ForeignItem(cfg.flat_map_foreign_item(item).pop().unwrap())
618 }
619 Annotatable::Stmt(stmt) => {
620 Annotatable::Stmt(stmt.map(|stmt| cfg.flat_map_stmt(stmt).pop().unwrap()))
621 }
622 Annotatable::Expr(mut expr) => Annotatable::Expr({
623 cfg.visit_expr(&mut expr);
624 expr
625 }),
626 Annotatable::Arm(arm) => Annotatable::Arm(cfg.flat_map_arm(arm).pop().unwrap()),
627 Annotatable::Field(field) => {
628 Annotatable::Field(cfg.flat_map_field(field).pop().unwrap())
629 }
630 Annotatable::FieldPat(fp) => {
631 Annotatable::FieldPat(cfg.flat_map_field_pattern(fp).pop().unwrap())
632 }
633 Annotatable::GenericParam(param) => {
634 Annotatable::GenericParam(cfg.flat_map_generic_param(param).pop().unwrap())
635 }
636 Annotatable::Param(param) => {
637 Annotatable::Param(cfg.flat_map_param(param).pop().unwrap())
638 }
639 Annotatable::StructField(sf) => {
640 Annotatable::StructField(cfg.flat_map_struct_field(sf).pop().unwrap())
641 }
642 Annotatable::Variant(v) => Annotatable::Variant(cfg.flat_map_variant(v).pop().unwrap()),
643 }
644 }
645
646 fn error_recursion_limit_reached(&mut self) {
647 let expn_data = self.cx.current_expansion.id.expn_data();
648 let suggested_limit = self.cx.ecfg.recursion_limit * 2;
649 self.cx
650 .struct_span_err(
651 expn_data.call_site,
652 &format!("recursion limit reached while expanding `{}`", expn_data.kind.descr()),
653 )
654 .help(&format!(
655 "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
656 suggested_limit, self.cx.ecfg.crate_name,
657 ))
658 .emit();
659 self.cx.trace_macros_diag();
660 }
661
662 /// A macro's expansion does not fit in this fragment kind.
663 /// For example, a non-type macro in a type position.
664 fn error_wrong_fragment_kind(&mut self, kind: AstFragmentKind, mac: &ast::MacCall, span: Span) {
665 let msg = format!(
666 "non-{kind} macro in {kind} position: {path}",
667 kind = kind.name(),
668 path = pprust::path_to_string(&mac.path),
669 );
670 self.cx.span_err(span, &msg);
671 self.cx.trace_macros_diag();
672 }
673
674 fn expand_invoc(
675 &mut self,
676 invoc: Invocation,
677 ext: &SyntaxExtensionKind,
678 ) -> ExpandResult<AstFragment, Invocation> {
679 let recursion_limit =
680 self.cx.reduced_recursion_limit.unwrap_or(self.cx.ecfg.recursion_limit);
681 if !recursion_limit.value_within_limit(self.cx.current_expansion.depth) {
682 if self.cx.reduced_recursion_limit.is_none() {
683 self.error_recursion_limit_reached();
684 }
685
686 // Reduce the recursion limit by half each time it triggers.
687 self.cx.reduced_recursion_limit = Some(recursion_limit / 2);
688
689 return ExpandResult::Ready(invoc.fragment_kind.dummy(invoc.span()));
690 }
691
692 let (fragment_kind, span) = (invoc.fragment_kind, invoc.span());
693 ExpandResult::Ready(match invoc.kind {
694 InvocationKind::Bang { mac, .. } => match ext {
695 SyntaxExtensionKind::Bang(expander) => {
696 let tok_result = match expander.expand(self.cx, span, mac.args.inner_tokens()) {
697 Err(_) => return ExpandResult::Ready(fragment_kind.dummy(span)),
698 Ok(ts) => ts,
699 };
700 self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
701 }
702 SyntaxExtensionKind::LegacyBang(expander) => {
703 let prev = self.cx.current_expansion.prior_type_ascription;
704 self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
705 let tok_result = expander.expand(self.cx, span, mac.args.inner_tokens());
706 let result = if let Some(result) = fragment_kind.make_from(tok_result) {
707 result
708 } else {
709 self.error_wrong_fragment_kind(fragment_kind, &mac, span);
710 fragment_kind.dummy(span)
711 };
712 self.cx.current_expansion.prior_type_ascription = prev;
713 result
714 }
715 _ => unreachable!(),
716 },
717 InvocationKind::Attr { attr, mut item, derives, after_derive } => match ext {
718 SyntaxExtensionKind::Attr(expander) => {
719 self.gate_proc_macro_input(&item);
720 self.gate_proc_macro_attr_item(span, &item);
721 let tokens = item.into_tokens(&self.cx.sess.parse_sess);
722 let attr_item = attr.unwrap_normal_item();
723 if let MacArgs::Eq(..) = attr_item.args {
724 self.cx.span_err(span, "key-value macro attributes are not supported");
725 }
726 let inner_tokens = attr_item.args.inner_tokens();
727 let tok_result = match expander.expand(self.cx, span, inner_tokens, tokens) {
728 Err(_) => return ExpandResult::Ready(fragment_kind.dummy(span)),
729 Ok(ts) => ts,
730 };
731 self.parse_ast_fragment(tok_result, fragment_kind, &attr_item.path, span)
732 }
733 SyntaxExtensionKind::LegacyAttr(expander) => {
734 match validate_attr::parse_meta(&self.cx.sess.parse_sess, &attr) {
735 Ok(meta) => {
736 let items = match expander.expand(self.cx, span, &meta, item) {
737 ExpandResult::Ready(items) => items,
738 ExpandResult::Retry(item, explanation) => {
739 // Reassemble the original invocation for retrying.
740 return ExpandResult::Retry(
741 Invocation {
742 kind: InvocationKind::Attr {
743 attr,
744 item,
745 derives,
746 after_derive,
747 },
748 ..invoc
749 },
750 explanation,
751 );
752 }
753 };
754 fragment_kind.expect_from_annotatables(items)
755 }
756 Err(mut err) => {
757 err.emit();
758 fragment_kind.dummy(span)
759 }
760 }
761 }
762 SyntaxExtensionKind::NonMacroAttr { mark_used } => {
763 self.cx.sess.mark_attr_known(&attr);
764 if *mark_used {
765 self.cx.sess.mark_attr_used(&attr);
766 }
767 item.visit_attrs(|attrs| attrs.push(attr));
768 fragment_kind.expect_from_annotatables(iter::once(item))
769 }
770 _ => unreachable!(),
771 },
772 InvocationKind::Derive { path, item } => match ext {
773 SyntaxExtensionKind::Derive(expander)
774 | SyntaxExtensionKind::LegacyDerive(expander) => {
775 if !item.derive_allowed() {
776 return ExpandResult::Ready(fragment_kind.dummy(span));
777 }
778 if let SyntaxExtensionKind::Derive(..) = ext {
779 self.gate_proc_macro_input(&item);
780 }
781 let meta = ast::MetaItem { kind: ast::MetaItemKind::Word, span, path };
782 let items = match expander.expand(self.cx, span, &meta, item) {
783 ExpandResult::Ready(items) => items,
784 ExpandResult::Retry(item, explanation) => {
785 // Reassemble the original invocation for retrying.
786 return ExpandResult::Retry(
787 Invocation {
788 kind: InvocationKind::Derive { path: meta.path, item },
789 ..invoc
790 },
791 explanation,
792 );
793 }
794 };
795 fragment_kind.expect_from_annotatables(items)
796 }
797 _ => unreachable!(),
798 },
799 InvocationKind::DeriveContainer { .. } => unreachable!(),
800 })
801 }
802
803 fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
804 let kind = match item {
805 Annotatable::Item(_)
806 | Annotatable::TraitItem(_)
807 | Annotatable::ImplItem(_)
808 | Annotatable::ForeignItem(_) => return,
809 Annotatable::Stmt(_) => "statements",
810 Annotatable::Expr(_) => "expressions",
811 Annotatable::Arm(..)
812 | Annotatable::Field(..)
813 | Annotatable::FieldPat(..)
814 | Annotatable::GenericParam(..)
815 | Annotatable::Param(..)
816 | Annotatable::StructField(..)
817 | Annotatable::Variant(..) => panic!("unexpected annotatable"),
818 };
819 if self.cx.ecfg.proc_macro_hygiene() {
820 return;
821 }
822 feature_err(
823 &self.cx.sess.parse_sess,
824 sym::proc_macro_hygiene,
825 span,
826 &format!("custom attributes cannot be applied to {}", kind),
827 )
828 .emit();
829 }
830
831 fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
832 struct GateProcMacroInput<'a> {
833 parse_sess: &'a ParseSess,
834 }
835
836 impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
837 fn visit_item(&mut self, item: &'ast ast::Item) {
838 match &item.kind {
839 ast::ItemKind::Mod(module) if !module.inline => {
840 feature_err(
841 self.parse_sess,
842 sym::proc_macro_hygiene,
843 item.span,
844 "non-inline modules in proc macro input are unstable",
845 )
846 .emit();
847 }
848 _ => {}
849 }
850
851 visit::walk_item(self, item);
852 }
853
854 fn visit_mac(&mut self, _: &'ast ast::MacCall) {}
855 }
856
857 if !self.cx.ecfg.proc_macro_hygiene() {
858 annotatable
859 .visit_with(&mut GateProcMacroInput { parse_sess: &self.cx.sess.parse_sess });
860 }
861 }
862
863 fn parse_ast_fragment(
864 &mut self,
865 toks: TokenStream,
866 kind: AstFragmentKind,
867 path: &Path,
868 span: Span,
869 ) -> AstFragment {
870 let mut parser = self.cx.new_parser_from_tts(toks);
871 match parse_ast_fragment(&mut parser, kind) {
872 Ok(fragment) => {
873 ensure_complete_parse(&mut parser, path, kind.name(), span);
874 fragment
875 }
876 Err(mut err) => {
877 err.set_span(span);
878 annotate_err_with_kind(&mut err, kind, span);
879 err.emit();
880 self.cx.trace_macros_diag();
881 kind.dummy(span)
882 }
883 }
884 }
885 }
886
887 pub fn parse_ast_fragment<'a>(
888 this: &mut Parser<'a>,
889 kind: AstFragmentKind,
890 ) -> PResult<'a, AstFragment> {
891 Ok(match kind {
892 AstFragmentKind::Items => {
893 let mut items = SmallVec::new();
894 while let Some(item) = this.parse_item()? {
895 items.push(item);
896 }
897 AstFragment::Items(items)
898 }
899 AstFragmentKind::TraitItems => {
900 let mut items = SmallVec::new();
901 while let Some(item) = this.parse_trait_item()? {
902 items.extend(item);
903 }
904 AstFragment::TraitItems(items)
905 }
906 AstFragmentKind::ImplItems => {
907 let mut items = SmallVec::new();
908 while let Some(item) = this.parse_impl_item()? {
909 items.extend(item);
910 }
911 AstFragment::ImplItems(items)
912 }
913 AstFragmentKind::ForeignItems => {
914 let mut items = SmallVec::new();
915 while let Some(item) = this.parse_foreign_item()? {
916 items.extend(item);
917 }
918 AstFragment::ForeignItems(items)
919 }
920 AstFragmentKind::Stmts => {
921 let mut stmts = SmallVec::new();
922 // Won't make progress on a `}`.
923 while this.token != token::Eof && this.token != token::CloseDelim(token::Brace) {
924 if let Some(stmt) = this.parse_full_stmt()? {
925 stmts.push(stmt);
926 }
927 }
928 AstFragment::Stmts(stmts)
929 }
930 AstFragmentKind::Expr => AstFragment::Expr(this.parse_expr()?),
931 AstFragmentKind::OptExpr => {
932 if this.token != token::Eof {
933 AstFragment::OptExpr(Some(this.parse_expr()?))
934 } else {
935 AstFragment::OptExpr(None)
936 }
937 }
938 AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
939 AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat(None)?),
940 AstFragmentKind::Arms
941 | AstFragmentKind::Fields
942 | AstFragmentKind::FieldPats
943 | AstFragmentKind::GenericParams
944 | AstFragmentKind::Params
945 | AstFragmentKind::StructFields
946 | AstFragmentKind::Variants => panic!("unexpected AST fragment kind"),
947 })
948 }
949
950 pub fn ensure_complete_parse<'a>(
951 this: &mut Parser<'a>,
952 macro_path: &Path,
953 kind_name: &str,
954 span: Span,
955 ) {
956 if this.token != token::Eof {
957 let token = pprust::token_to_string(&this.token);
958 let msg = format!("macro expansion ignores token `{}` and any following", token);
959 // Avoid emitting backtrace info twice.
960 let def_site_span = this.token.span.with_ctxt(SyntaxContext::root());
961 let mut err = this.struct_span_err(def_site_span, &msg);
962 err.span_label(span, "caused by the macro expansion here");
963 let msg = format!(
964 "the usage of `{}!` is likely invalid in {} context",
965 pprust::path_to_string(macro_path),
966 kind_name,
967 );
968 err.note(&msg);
969 let semi_span = this.sess.source_map().next_point(span);
970
971 let semi_full_span = semi_span.to(this.sess.source_map().next_point(semi_span));
972 match this.sess.source_map().span_to_snippet(semi_full_span) {
973 Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
974 err.span_suggestion(
975 semi_span,
976 "you might be missing a semicolon here",
977 ";".to_owned(),
978 Applicability::MaybeIncorrect,
979 );
980 }
981 _ => {}
982 }
983 err.emit();
984 }
985 }
986
987 struct InvocationCollector<'a, 'b> {
988 cx: &'a mut ExtCtxt<'b>,
989 cfg: StripUnconfigured<'a>,
990 invocations: Vec<(Invocation, Option<InvocationRes>)>,
991 monotonic: bool,
992 }
993
994 impl<'a, 'b> InvocationCollector<'a, 'b> {
995 fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
996 // Expansion data for all the collected invocations is set upon their resolution,
997 // with exception of the derive container case which is not resolved and can get
998 // its expansion data immediately.
999 let expn_data = match &kind {
1000 InvocationKind::DeriveContainer { item, .. } => Some(ExpnData {
1001 parent: self.cx.current_expansion.id,
1002 ..ExpnData::default(
1003 ExpnKind::Macro(MacroKind::Attr, sym::derive),
1004 item.span(),
1005 self.cx.sess.parse_sess.edition,
1006 None,
1007 )
1008 }),
1009 _ => None,
1010 };
1011 let expn_id = ExpnId::fresh(expn_data);
1012 let vis = kind.placeholder_visibility();
1013 self.invocations.push((
1014 Invocation {
1015 kind,
1016 fragment_kind,
1017 expansion_data: ExpansionData {
1018 id: expn_id,
1019 depth: self.cx.current_expansion.depth + 1,
1020 ..self.cx.current_expansion.clone()
1021 },
1022 },
1023 None,
1024 ));
1025 placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis)
1026 }
1027
1028 fn collect_bang(
1029 &mut self,
1030 mac: ast::MacCall,
1031 span: Span,
1032 kind: AstFragmentKind,
1033 ) -> AstFragment {
1034 self.collect(kind, InvocationKind::Bang { mac, span })
1035 }
1036
1037 fn collect_attr(
1038 &mut self,
1039 attr: Option<ast::Attribute>,
1040 derives: Vec<Path>,
1041 item: Annotatable,
1042 kind: AstFragmentKind,
1043 after_derive: bool,
1044 ) -> AstFragment {
1045 self.collect(
1046 kind,
1047 match attr {
1048 Some(attr) => InvocationKind::Attr { attr, item, derives, after_derive },
1049 None => InvocationKind::DeriveContainer { derives, item },
1050 },
1051 )
1052 }
1053
1054 fn find_attr_invoc(
1055 &self,
1056 attrs: &mut Vec<ast::Attribute>,
1057 after_derive: &mut bool,
1058 ) -> Option<ast::Attribute> {
1059 let attr = attrs
1060 .iter()
1061 .position(|a| {
1062 if a.has_name(sym::derive) {
1063 *after_derive = true;
1064 }
1065 !self.cx.sess.is_attr_known(a) && !is_builtin_attr(a)
1066 })
1067 .map(|i| attrs.remove(i));
1068 if let Some(attr) = &attr {
1069 if !self.cx.ecfg.custom_inner_attributes()
1070 && attr.style == ast::AttrStyle::Inner
1071 && !attr.has_name(sym::test)
1072 {
1073 feature_err(
1074 &self.cx.sess.parse_sess,
1075 sym::custom_inner_attributes,
1076 attr.span,
1077 "non-builtin inner attributes are unstable",
1078 )
1079 .emit();
1080 }
1081 }
1082 attr
1083 }
1084
1085 /// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
1086 fn classify_item(
1087 &mut self,
1088 item: &mut impl HasAttrs,
1089 ) -> (Option<ast::Attribute>, Vec<Path>, /* after_derive */ bool) {
1090 let (mut attr, mut traits, mut after_derive) = (None, Vec::new(), false);
1091
1092 item.visit_attrs(|mut attrs| {
1093 attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
1094 traits = collect_derives(&mut self.cx, &mut attrs);
1095 });
1096
1097 (attr, traits, after_derive)
1098 }
1099
1100 /// Alternative to `classify_item()` that ignores `#[derive]` so invocations fallthrough
1101 /// to the unused-attributes lint (making it an error on statements and expressions
1102 /// is a breaking change)
1103 fn classify_nonitem(
1104 &mut self,
1105 nonitem: &mut impl HasAttrs,
1106 ) -> (Option<ast::Attribute>, /* after_derive */ bool) {
1107 let (mut attr, mut after_derive) = (None, false);
1108
1109 nonitem.visit_attrs(|mut attrs| {
1110 attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
1111 });
1112
1113 (attr, after_derive)
1114 }
1115
1116 fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
1117 self.cfg.configure(node)
1118 }
1119
1120 // Detect use of feature-gated or invalid attributes on macro invocations
1121 // since they will not be detected after macro expansion.
1122 fn check_attributes(&mut self, attrs: &[ast::Attribute]) {
1123 let features = self.cx.ecfg.features.unwrap();
1124 for attr in attrs.iter() {
1125 rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
1126 validate_attr::check_meta(&self.cx.sess.parse_sess, attr);
1127
1128 // macros are expanded before any lint passes so this warning has to be hardcoded
1129 if attr.has_name(sym::derive) {
1130 self.cx
1131 .parse_sess()
1132 .span_diagnostic
1133 .struct_span_warn(attr.span, "`#[derive]` does nothing on macro invocations")
1134 .note("this may become a hard error in a future release")
1135 .emit();
1136 }
1137
1138 if attr.doc_str().is_some() {
1139 self.cx.sess.parse_sess.buffer_lint_with_diagnostic(
1140 &UNUSED_DOC_COMMENTS,
1141 attr.span,
1142 ast::CRATE_NODE_ID,
1143 "unused doc comment",
1144 BuiltinLintDiagnostics::UnusedDocComment(attr.span),
1145 );
1146 }
1147 }
1148 }
1149 }
1150
1151 impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1152 fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
1153 self.cfg.configure_expr(expr);
1154 visit_clobber(expr.deref_mut(), |mut expr| {
1155 self.cfg.configure_expr_kind(&mut expr.kind);
1156
1157 // ignore derives so they remain unused
1158 let (attr, after_derive) = self.classify_nonitem(&mut expr);
1159
1160 if let Some(ref attr_value) = attr {
1161 // Collect the invoc regardless of whether or not attributes are permitted here
1162 // expansion will eat the attribute so it won't error later.
1163 self.cfg.maybe_emit_expr_attr_err(attr_value);
1164
1165 // AstFragmentKind::Expr requires the macro to emit an expression.
1166 return self
1167 .collect_attr(
1168 attr,
1169 vec![],
1170 Annotatable::Expr(P(expr)),
1171 AstFragmentKind::Expr,
1172 after_derive,
1173 )
1174 .make_expr()
1175 .into_inner();
1176 }
1177
1178 if let ast::ExprKind::MacCall(mac) = expr.kind {
1179 self.check_attributes(&expr.attrs);
1180 self.collect_bang(mac, expr.span, AstFragmentKind::Expr).make_expr().into_inner()
1181 } else {
1182 ensure_sufficient_stack(|| noop_visit_expr(&mut expr, self));
1183 expr
1184 }
1185 });
1186 }
1187
1188 fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
1189 let mut arm = configure!(self, arm);
1190
1191 let (attr, traits, after_derive) = self.classify_item(&mut arm);
1192 if attr.is_some() || !traits.is_empty() {
1193 return self
1194 .collect_attr(
1195 attr,
1196 traits,
1197 Annotatable::Arm(arm),
1198 AstFragmentKind::Arms,
1199 after_derive,
1200 )
1201 .make_arms();
1202 }
1203
1204 noop_flat_map_arm(arm, self)
1205 }
1206
1207 fn flat_map_field(&mut self, field: ast::Field) -> SmallVec<[ast::Field; 1]> {
1208 let mut field = configure!(self, field);
1209
1210 let (attr, traits, after_derive) = self.classify_item(&mut field);
1211 if attr.is_some() || !traits.is_empty() {
1212 return self
1213 .collect_attr(
1214 attr,
1215 traits,
1216 Annotatable::Field(field),
1217 AstFragmentKind::Fields,
1218 after_derive,
1219 )
1220 .make_fields();
1221 }
1222
1223 noop_flat_map_field(field, self)
1224 }
1225
1226 fn flat_map_field_pattern(&mut self, fp: ast::FieldPat) -> SmallVec<[ast::FieldPat; 1]> {
1227 let mut fp = configure!(self, fp);
1228
1229 let (attr, traits, after_derive) = self.classify_item(&mut fp);
1230 if attr.is_some() || !traits.is_empty() {
1231 return self
1232 .collect_attr(
1233 attr,
1234 traits,
1235 Annotatable::FieldPat(fp),
1236 AstFragmentKind::FieldPats,
1237 after_derive,
1238 )
1239 .make_field_patterns();
1240 }
1241
1242 noop_flat_map_field_pattern(fp, self)
1243 }
1244
1245 fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
1246 let mut p = configure!(self, p);
1247
1248 let (attr, traits, after_derive) = self.classify_item(&mut p);
1249 if attr.is_some() || !traits.is_empty() {
1250 return self
1251 .collect_attr(
1252 attr,
1253 traits,
1254 Annotatable::Param(p),
1255 AstFragmentKind::Params,
1256 after_derive,
1257 )
1258 .make_params();
1259 }
1260
1261 noop_flat_map_param(p, self)
1262 }
1263
1264 fn flat_map_struct_field(&mut self, sf: ast::StructField) -> SmallVec<[ast::StructField; 1]> {
1265 let mut sf = configure!(self, sf);
1266
1267 let (attr, traits, after_derive) = self.classify_item(&mut sf);
1268 if attr.is_some() || !traits.is_empty() {
1269 return self
1270 .collect_attr(
1271 attr,
1272 traits,
1273 Annotatable::StructField(sf),
1274 AstFragmentKind::StructFields,
1275 after_derive,
1276 )
1277 .make_struct_fields();
1278 }
1279
1280 noop_flat_map_struct_field(sf, self)
1281 }
1282
1283 fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
1284 let mut variant = configure!(self, variant);
1285
1286 let (attr, traits, after_derive) = self.classify_item(&mut variant);
1287 if attr.is_some() || !traits.is_empty() {
1288 return self
1289 .collect_attr(
1290 attr,
1291 traits,
1292 Annotatable::Variant(variant),
1293 AstFragmentKind::Variants,
1294 after_derive,
1295 )
1296 .make_variants();
1297 }
1298
1299 noop_flat_map_variant(variant, self)
1300 }
1301
1302 fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
1303 let expr = configure!(self, expr);
1304 expr.filter_map(|mut expr| {
1305 self.cfg.configure_expr_kind(&mut expr.kind);
1306
1307 // Ignore derives so they remain unused.
1308 let (attr, after_derive) = self.classify_nonitem(&mut expr);
1309
1310 if let Some(ref attr_value) = attr {
1311 self.cfg.maybe_emit_expr_attr_err(attr_value);
1312
1313 return self
1314 .collect_attr(
1315 attr,
1316 vec![],
1317 Annotatable::Expr(P(expr)),
1318 AstFragmentKind::OptExpr,
1319 after_derive,
1320 )
1321 .make_opt_expr()
1322 .map(|expr| expr.into_inner());
1323 }
1324
1325 if let ast::ExprKind::MacCall(mac) = expr.kind {
1326 self.check_attributes(&expr.attrs);
1327 self.collect_bang(mac, expr.span, AstFragmentKind::OptExpr)
1328 .make_opt_expr()
1329 .map(|expr| expr.into_inner())
1330 } else {
1331 Some({
1332 noop_visit_expr(&mut expr, self);
1333 expr
1334 })
1335 }
1336 })
1337 }
1338
1339 fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
1340 self.cfg.configure_pat(pat);
1341 match pat.kind {
1342 PatKind::MacCall(_) => {}
1343 _ => return noop_visit_pat(pat, self),
1344 }
1345
1346 visit_clobber(pat, |mut pat| match mem::replace(&mut pat.kind, PatKind::Wild) {
1347 PatKind::MacCall(mac) => {
1348 self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat()
1349 }
1350 _ => unreachable!(),
1351 });
1352 }
1353
1354 fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
1355 let mut stmt = configure!(self, stmt);
1356
1357 // we'll expand attributes on expressions separately
1358 if !stmt.is_expr() {
1359 let (attr, derives, after_derive) = if stmt.is_item() {
1360 self.classify_item(&mut stmt)
1361 } else {
1362 // ignore derives on non-item statements so it falls through
1363 // to the unused-attributes lint
1364 let (attr, after_derive) = self.classify_nonitem(&mut stmt);
1365 (attr, vec![], after_derive)
1366 };
1367
1368 if attr.is_some() || !derives.is_empty() {
1369 return self
1370 .collect_attr(
1371 attr,
1372 derives,
1373 Annotatable::Stmt(P(stmt)),
1374 AstFragmentKind::Stmts,
1375 after_derive,
1376 )
1377 .make_stmts();
1378 }
1379 }
1380
1381 if let StmtKind::MacCall(mac) = stmt.kind {
1382 let MacCallStmt { mac, style, attrs } = mac.into_inner();
1383 self.check_attributes(&attrs);
1384 let mut placeholder =
1385 self.collect_bang(mac, stmt.span, AstFragmentKind::Stmts).make_stmts();
1386
1387 // If this is a macro invocation with a semicolon, then apply that
1388 // semicolon to the final statement produced by expansion.
1389 if style == MacStmtStyle::Semicolon {
1390 if let Some(stmt) = placeholder.pop() {
1391 placeholder.push(stmt.add_trailing_semicolon());
1392 }
1393 }
1394
1395 return placeholder;
1396 }
1397
1398 // The placeholder expander gives ids to statements, so we avoid folding the id here.
1399 let ast::Stmt { id, kind, span, tokens } = stmt;
1400 noop_flat_map_stmt_kind(kind, self)
1401 .into_iter()
1402 .map(|kind| ast::Stmt { id, kind, span, tokens: tokens.clone() })
1403 .collect()
1404 }
1405
1406 fn visit_block(&mut self, block: &mut P<Block>) {
1407 let old_directory_ownership = self.cx.current_expansion.directory_ownership;
1408 self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
1409 noop_visit_block(block, self);
1410 self.cx.current_expansion.directory_ownership = old_directory_ownership;
1411 }
1412
1413 fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
1414 let mut item = configure!(self, item);
1415
1416 let (attr, traits, after_derive) = self.classify_item(&mut item);
1417 if attr.is_some() || !traits.is_empty() {
1418 return self
1419 .collect_attr(
1420 attr,
1421 traits,
1422 Annotatable::Item(item),
1423 AstFragmentKind::Items,
1424 after_derive,
1425 )
1426 .make_items();
1427 }
1428
1429 let mut attrs = mem::take(&mut item.attrs); // We do this to please borrowck.
1430 let ident = item.ident;
1431 let span = item.span;
1432
1433 match item.kind {
1434 ast::ItemKind::MacCall(..) => {
1435 item.attrs = attrs;
1436 self.check_attributes(&item.attrs);
1437 item.and_then(|item| match item.kind {
1438 ItemKind::MacCall(mac) => self
1439 .collect(AstFragmentKind::Items, InvocationKind::Bang { mac, span })
1440 .make_items(),
1441 _ => unreachable!(),
1442 })
1443 }
1444 ast::ItemKind::Mod(ref mut old_mod @ ast::Mod { .. }) if ident != Ident::invalid() => {
1445 let sess = &self.cx.sess.parse_sess;
1446 let orig_ownership = self.cx.current_expansion.directory_ownership;
1447 let mut module = (*self.cx.current_expansion.module).clone();
1448
1449 let pushed = &mut false; // Record `parse_external_mod` pushing so we can pop.
1450 let dir = Directory { ownership: orig_ownership, path: module.directory };
1451 let Directory { ownership, path } = if old_mod.inline {
1452 // Inline `mod foo { ... }`, but we still need to push directories.
1453 item.attrs = attrs;
1454 push_directory(&self.cx.sess, ident, &item.attrs, dir)
1455 } else {
1456 // We have an outline `mod foo;` so we need to parse the file.
1457 let (new_mod, dir) = parse_external_mod(
1458 &self.cx.sess,
1459 ident,
1460 span,
1461 old_mod.unsafety,
1462 dir,
1463 &mut attrs,
1464 pushed,
1465 );
1466
1467 let krate = ast::Crate {
1468 span: new_mod.inner,
1469 module: new_mod,
1470 attrs,
1471 proc_macros: vec![],
1472 };
1473 if let Some(extern_mod_loaded) = self.cx.extern_mod_loaded {
1474 extern_mod_loaded(&krate);
1475 }
1476
1477 *old_mod = krate.module;
1478 item.attrs = krate.attrs;
1479 // File can have inline attributes, e.g., `#![cfg(...)]` & co. => Reconfigure.
1480 item = match self.configure(item) {
1481 Some(node) => node,
1482 None => {
1483 if *pushed {
1484 sess.included_mod_stack.borrow_mut().pop();
1485 }
1486 return Default::default();
1487 }
1488 };
1489 dir
1490 };
1491
1492 // Set the module info before we flat map.
1493 self.cx.current_expansion.directory_ownership = ownership;
1494 module.directory = path;
1495 module.mod_path.push(ident);
1496 let orig_module =
1497 mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
1498
1499 let result = noop_flat_map_item(item, self);
1500
1501 // Restore the module info.
1502 self.cx.current_expansion.module = orig_module;
1503 self.cx.current_expansion.directory_ownership = orig_ownership;
1504 if *pushed {
1505 sess.included_mod_stack.borrow_mut().pop();
1506 }
1507 result
1508 }
1509 _ => {
1510 item.attrs = attrs;
1511 noop_flat_map_item(item, self)
1512 }
1513 }
1514 }
1515
1516 fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
1517 let mut item = configure!(self, item);
1518
1519 let (attr, traits, after_derive) = self.classify_item(&mut item);
1520 if attr.is_some() || !traits.is_empty() {
1521 return self
1522 .collect_attr(
1523 attr,
1524 traits,
1525 Annotatable::TraitItem(item),
1526 AstFragmentKind::TraitItems,
1527 after_derive,
1528 )
1529 .make_trait_items();
1530 }
1531
1532 match item.kind {
1533 ast::AssocItemKind::MacCall(..) => {
1534 self.check_attributes(&item.attrs);
1535 item.and_then(|item| match item.kind {
1536 ast::AssocItemKind::MacCall(mac) => self
1537 .collect_bang(mac, item.span, AstFragmentKind::TraitItems)
1538 .make_trait_items(),
1539 _ => unreachable!(),
1540 })
1541 }
1542 _ => noop_flat_map_assoc_item(item, self),
1543 }
1544 }
1545
1546 fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
1547 let mut item = configure!(self, item);
1548
1549 let (attr, traits, after_derive) = self.classify_item(&mut item);
1550 if attr.is_some() || !traits.is_empty() {
1551 return self
1552 .collect_attr(
1553 attr,
1554 traits,
1555 Annotatable::ImplItem(item),
1556 AstFragmentKind::ImplItems,
1557 after_derive,
1558 )
1559 .make_impl_items();
1560 }
1561
1562 match item.kind {
1563 ast::AssocItemKind::MacCall(..) => {
1564 self.check_attributes(&item.attrs);
1565 item.and_then(|item| match item.kind {
1566 ast::AssocItemKind::MacCall(mac) => self
1567 .collect_bang(mac, item.span, AstFragmentKind::ImplItems)
1568 .make_impl_items(),
1569 _ => unreachable!(),
1570 })
1571 }
1572 _ => noop_flat_map_assoc_item(item, self),
1573 }
1574 }
1575
1576 fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
1577 match ty.kind {
1578 ast::TyKind::MacCall(_) => {}
1579 _ => return noop_visit_ty(ty, self),
1580 };
1581
1582 visit_clobber(ty, |mut ty| match mem::replace(&mut ty.kind, ast::TyKind::Err) {
1583 ast::TyKind::MacCall(mac) => {
1584 self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty()
1585 }
1586 _ => unreachable!(),
1587 });
1588 }
1589
1590 fn visit_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
1591 self.cfg.configure_foreign_mod(foreign_mod);
1592 noop_visit_foreign_mod(foreign_mod, self);
1593 }
1594
1595 fn flat_map_foreign_item(
1596 &mut self,
1597 mut foreign_item: P<ast::ForeignItem>,
1598 ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
1599 let (attr, traits, after_derive) = self.classify_item(&mut foreign_item);
1600
1601 if attr.is_some() || !traits.is_empty() {
1602 return self
1603 .collect_attr(
1604 attr,
1605 traits,
1606 Annotatable::ForeignItem(foreign_item),
1607 AstFragmentKind::ForeignItems,
1608 after_derive,
1609 )
1610 .make_foreign_items();
1611 }
1612
1613 match foreign_item.kind {
1614 ast::ForeignItemKind::MacCall(..) => {
1615 self.check_attributes(&foreign_item.attrs);
1616 foreign_item.and_then(|item| match item.kind {
1617 ast::ForeignItemKind::MacCall(mac) => self
1618 .collect_bang(mac, item.span, AstFragmentKind::ForeignItems)
1619 .make_foreign_items(),
1620 _ => unreachable!(),
1621 })
1622 }
1623 _ => noop_flat_map_foreign_item(foreign_item, self),
1624 }
1625 }
1626
1627 fn visit_item_kind(&mut self, item: &mut ast::ItemKind) {
1628 match item {
1629 ast::ItemKind::MacroDef(..) => {}
1630 _ => {
1631 self.cfg.configure_item_kind(item);
1632 noop_visit_item_kind(item, self);
1633 }
1634 }
1635 }
1636
1637 fn flat_map_generic_param(
1638 &mut self,
1639 param: ast::GenericParam,
1640 ) -> SmallVec<[ast::GenericParam; 1]> {
1641 let mut param = configure!(self, param);
1642
1643 let (attr, traits, after_derive) = self.classify_item(&mut param);
1644 if attr.is_some() || !traits.is_empty() {
1645 return self
1646 .collect_attr(
1647 attr,
1648 traits,
1649 Annotatable::GenericParam(param),
1650 AstFragmentKind::GenericParams,
1651 after_derive,
1652 )
1653 .make_generic_params();
1654 }
1655
1656 noop_flat_map_generic_param(param, self)
1657 }
1658
1659 fn visit_attribute(&mut self, at: &mut ast::Attribute) {
1660 // turn `#[doc(include="filename")]` attributes into `#[doc(include(file="filename",
1661 // contents="file contents")]` attributes
1662 if !self.cx.sess.check_name(at, sym::doc) {
1663 return noop_visit_attribute(at, self);
1664 }
1665
1666 if let Some(list) = at.meta_item_list() {
1667 if !list.iter().any(|it| it.has_name(sym::include)) {
1668 return noop_visit_attribute(at, self);
1669 }
1670
1671 let mut items = vec![];
1672
1673 for mut it in list {
1674 if !it.has_name(sym::include) {
1675 items.push({
1676 noop_visit_meta_list_item(&mut it, self);
1677 it
1678 });
1679 continue;
1680 }
1681
1682 if let Some(file) = it.value_str() {
1683 let err_count = self.cx.sess.parse_sess.span_diagnostic.err_count();
1684 self.check_attributes(slice::from_ref(at));
1685 if self.cx.sess.parse_sess.span_diagnostic.err_count() > err_count {
1686 // avoid loading the file if they haven't enabled the feature
1687 return noop_visit_attribute(at, self);
1688 }
1689
1690 let filename = match self.cx.resolve_path(&*file.as_str(), it.span()) {
1691 Ok(filename) => filename,
1692 Err(mut err) => {
1693 err.emit();
1694 continue;
1695 }
1696 };
1697
1698 match self.cx.source_map().load_file(&filename) {
1699 Ok(source_file) => {
1700 let src = source_file
1701 .src
1702 .as_ref()
1703 .expect("freshly loaded file should have a source");
1704 let src_interned = Symbol::intern(src.as_str());
1705
1706 let include_info = vec![
1707 ast::NestedMetaItem::MetaItem(attr::mk_name_value_item_str(
1708 Ident::with_dummy_span(sym::file),
1709 file,
1710 DUMMY_SP,
1711 )),
1712 ast::NestedMetaItem::MetaItem(attr::mk_name_value_item_str(
1713 Ident::with_dummy_span(sym::contents),
1714 src_interned,
1715 DUMMY_SP,
1716 )),
1717 ];
1718
1719 let include_ident = Ident::with_dummy_span(sym::include);
1720 let item = attr::mk_list_item(include_ident, include_info);
1721 items.push(ast::NestedMetaItem::MetaItem(item));
1722 }
1723 Err(e) => {
1724 let lit =
1725 it.meta_item().and_then(|item| item.name_value_literal()).unwrap();
1726
1727 if e.kind() == ErrorKind::InvalidData {
1728 self.cx
1729 .struct_span_err(
1730 lit.span,
1731 &format!("{} wasn't a utf-8 file", filename.display()),
1732 )
1733 .span_label(lit.span, "contains invalid utf-8")
1734 .emit();
1735 } else {
1736 let mut err = self.cx.struct_span_err(
1737 lit.span,
1738 &format!("couldn't read {}: {}", filename.display(), e),
1739 );
1740 err.span_label(lit.span, "couldn't read file");
1741
1742 err.emit();
1743 }
1744 }
1745 }
1746 } else {
1747 let mut err = self
1748 .cx
1749 .struct_span_err(it.span(), "expected path to external documentation");
1750
1751 // Check if the user erroneously used `doc(include(...))` syntax.
1752 let literal = it.meta_item_list().and_then(|list| {
1753 if list.len() == 1 {
1754 list[0].literal().map(|literal| &literal.kind)
1755 } else {
1756 None
1757 }
1758 });
1759
1760 let (path, applicability) = match &literal {
1761 Some(LitKind::Str(path, ..)) => {
1762 (path.to_string(), Applicability::MachineApplicable)
1763 }
1764 _ => (String::from("<path>"), Applicability::HasPlaceholders),
1765 };
1766
1767 err.span_suggestion(
1768 it.span(),
1769 "provide a file path with `=`",
1770 format!("include = \"{}\"", path),
1771 applicability,
1772 );
1773
1774 err.emit();
1775 }
1776 }
1777
1778 let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
1779 *at = ast::Attribute {
1780 kind: ast::AttrKind::Normal(AttrItem {
1781 path: meta.path,
1782 args: meta.kind.mac_args(meta.span),
1783 tokens: None,
1784 }),
1785 span: at.span,
1786 id: at.id,
1787 style: at.style,
1788 };
1789 } else {
1790 noop_visit_attribute(at, self)
1791 }
1792 }
1793
1794 fn visit_id(&mut self, id: &mut ast::NodeId) {
1795 if self.monotonic {
1796 debug_assert_eq!(*id, ast::DUMMY_NODE_ID);
1797 *id = self.cx.resolver.next_node_id()
1798 }
1799 }
1800
1801 fn visit_fn_decl(&mut self, mut fn_decl: &mut P<ast::FnDecl>) {
1802 self.cfg.configure_fn_decl(&mut fn_decl);
1803 noop_visit_fn_decl(fn_decl, self);
1804 }
1805 }
1806
1807 pub struct ExpansionConfig<'feat> {
1808 pub crate_name: String,
1809 pub features: Option<&'feat Features>,
1810 pub recursion_limit: Limit,
1811 pub trace_mac: bool,
1812 pub should_test: bool, // If false, strip `#[test]` nodes
1813 pub keep_macs: bool,
1814 pub span_debug: bool, // If true, use verbose debugging for `proc_macro::Span`
1815 pub proc_macro_backtrace: bool, // If true, show backtraces for proc-macro panics
1816 }
1817
1818 impl<'feat> ExpansionConfig<'feat> {
1819 pub fn default(crate_name: String) -> ExpansionConfig<'static> {
1820 ExpansionConfig {
1821 crate_name,
1822 features: None,
1823 recursion_limit: Limit::new(1024),
1824 trace_mac: false,
1825 should_test: false,
1826 keep_macs: false,
1827 span_debug: false,
1828 proc_macro_backtrace: false,
1829 }
1830 }
1831
1832 fn proc_macro_hygiene(&self) -> bool {
1833 self.features.map_or(false, |features| features.proc_macro_hygiene)
1834 }
1835 fn custom_inner_attributes(&self) -> bool {
1836 self.features.map_or(false, |features| features.custom_inner_attributes)
1837 }
1838 }