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