]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_expand/src/base.rs
New upstream version 1.59.0+dfsg1
[rustc.git] / compiler / rustc_expand / src / base.rs
CommitLineData
e74abb32 1use crate::expand::{self, AstFragment, Invocation};
6a06907d 2use crate::module::DirOwnership;
e74abb32 3
94222f64 4use rustc_ast::attr::MarkedAttrs;
74b04a01 5use rustc_ast::ptr::P;
5869c6ff 6use rustc_ast::token::{self, Nonterminal};
cdc7bbd5 7use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream};
74b04a01 8use rustc_ast::visit::{AssocCtxt, Visitor};
6a06907d
XL
9use rustc_ast::{self as ast, AstLike, Attribute, Item, NodeId, PatKind};
10use rustc_attr::{self as attr, Deprecation, Stability};
dfeec247
XL
11use rustc_data_structures::fx::FxHashMap;
12use rustc_data_structures::sync::{self, Lrc};
c295e0f8 13use rustc_errors::{Applicability, DiagnosticBuilder, ErrorReported};
6a06907d
XL
14use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;
15use rustc_lint_defs::BuiltinLintDiagnostics;
f035d41b 16use rustc_parse::{self, nt_to_tokenstream, parser, MACRO_ARGUMENTS};
3dfed10e 17use rustc_session::{parse::ParseSess, Limit, Session};
c295e0f8 18use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
dfeec247 19use rustc_span::edition::Edition;
136023e0 20use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
dfeec247
XL
21use rustc_span::source_map::SourceMap;
22use rustc_span::symbol::{kw, sym, Ident, Symbol};
23use rustc_span::{FileName, MultiSpan, Span, DUMMY_SP};
24use smallvec::{smallvec, SmallVec};
1a4d82fc 25
dfeec247 26use std::default::Default;
ff7c6d11 27use std::iter;
9e0c209e 28use std::path::PathBuf;
1a4d82fc
JJ
29use std::rc::Rc;
30
dfeec247 31crate use rustc_span::hygiene::MacroKind;
223e47cc 32
136023e0
XL
33// When adding new variants, make sure to
34// adjust the `visit_*` / `flat_map_*` calls in `InvocationCollector`
35// to use `assign_id!`
dfeec247 36#[derive(Debug, Clone)]
85aaf69f
SL
37pub enum Annotatable {
38 Item(P<ast::Item>),
dfeec247
XL
39 TraitItem(P<ast::AssocItem>),
40 ImplItem(P<ast::AssocItem>),
83c7162d 41 ForeignItem(P<ast::ForeignItem>),
0531ce1d
XL
42 Stmt(P<ast::Stmt>),
43 Expr(P<ast::Expr>),
e1599b0c 44 Arm(ast::Arm),
6a06907d
XL
45 ExprField(ast::ExprField),
46 PatField(ast::PatField),
e1599b0c
XL
47 GenericParam(ast::GenericParam),
48 Param(ast::Param),
6a06907d 49 FieldDef(ast::FieldDef),
e1599b0c 50 Variant(ast::Variant),
a2a8927a 51 Crate(ast::Crate),
85aaf69f
SL
52}
53
cdc7bbd5
XL
54impl Annotatable {
55 pub fn span(&self) -> Span {
85aaf69f 56 match *self {
cdc7bbd5
XL
57 Annotatable::Item(ref item) => item.span,
58 Annotatable::TraitItem(ref trait_item) => trait_item.span,
59 Annotatable::ImplItem(ref impl_item) => impl_item.span,
60 Annotatable::ForeignItem(ref foreign_item) => foreign_item.span,
61 Annotatable::Stmt(ref stmt) => stmt.span,
62 Annotatable::Expr(ref expr) => expr.span,
63 Annotatable::Arm(ref arm) => arm.span,
64 Annotatable::ExprField(ref field) => field.span,
65 Annotatable::PatField(ref fp) => fp.pat.span,
66 Annotatable::GenericParam(ref gp) => gp.ident.span,
67 Annotatable::Param(ref p) => p.span,
68 Annotatable::FieldDef(ref sf) => sf.span,
69 Annotatable::Variant(ref v) => v.span,
a2a8927a 70 Annotatable::Crate(ref c) => c.span,
85aaf69f
SL
71 }
72 }
73
cdc7bbd5 74 pub fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
85aaf69f 75 match self {
9fa01778
XL
76 Annotatable::Item(item) => item.visit_attrs(f),
77 Annotatable::TraitItem(trait_item) => trait_item.visit_attrs(f),
78 Annotatable::ImplItem(impl_item) => impl_item.visit_attrs(f),
79 Annotatable::ForeignItem(foreign_item) => foreign_item.visit_attrs(f),
80 Annotatable::Stmt(stmt) => stmt.visit_attrs(f),
81 Annotatable::Expr(expr) => expr.visit_attrs(f),
e1599b0c 82 Annotatable::Arm(arm) => arm.visit_attrs(f),
6a06907d
XL
83 Annotatable::ExprField(field) => field.visit_attrs(f),
84 Annotatable::PatField(fp) => fp.visit_attrs(f),
e1599b0c
XL
85 Annotatable::GenericParam(gp) => gp.visit_attrs(f),
86 Annotatable::Param(p) => p.visit_attrs(f),
6a06907d 87 Annotatable::FieldDef(sf) => sf.visit_attrs(f),
e1599b0c 88 Annotatable::Variant(v) => v.visit_attrs(f),
a2a8927a 89 Annotatable::Crate(c) => c.visit_attrs(f),
85aaf69f
SL
90 }
91 }
6a06907d 92
e1599b0c
XL
93 pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
94 match self {
95 Annotatable::Item(item) => visitor.visit_item(item),
74b04a01
XL
96 Annotatable::TraitItem(item) => visitor.visit_assoc_item(item, AssocCtxt::Trait),
97 Annotatable::ImplItem(item) => visitor.visit_assoc_item(item, AssocCtxt::Impl),
e1599b0c
XL
98 Annotatable::ForeignItem(foreign_item) => visitor.visit_foreign_item(foreign_item),
99 Annotatable::Stmt(stmt) => visitor.visit_stmt(stmt),
100 Annotatable::Expr(expr) => visitor.visit_expr(expr),
101 Annotatable::Arm(arm) => visitor.visit_arm(arm),
6a06907d
XL
102 Annotatable::ExprField(field) => visitor.visit_expr_field(field),
103 Annotatable::PatField(fp) => visitor.visit_pat_field(fp),
e1599b0c
XL
104 Annotatable::GenericParam(gp) => visitor.visit_generic_param(gp),
105 Annotatable::Param(p) => visitor.visit_param(p),
6a06907d 106 Annotatable::FieldDef(sf) => visitor.visit_field_def(sf),
e1599b0c 107 Annotatable::Variant(v) => visitor.visit_variant(v),
a2a8927a 108 Annotatable::Crate(c) => visitor.visit_crate(c),
cc61c64b
XL
109 }
110 }
111
cdc7bbd5 112 pub fn into_nonterminal(self) -> Nonterminal {
5869c6ff 113 match self {
74b04a01
XL
114 Annotatable::Item(item) => token::NtItem(item),
115 Annotatable::TraitItem(item) | Annotatable::ImplItem(item) => {
116 token::NtItem(P(item.and_then(ast::AssocItem::into_item)))
117 }
118 Annotatable::ForeignItem(item) => {
119 token::NtItem(P(item.and_then(ast::ForeignItem::into_item)))
120 }
121 Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
122 Annotatable::Expr(expr) => token::NtExpr(expr),
123 Annotatable::Arm(..)
6a06907d
XL
124 | Annotatable::ExprField(..)
125 | Annotatable::PatField(..)
74b04a01
XL
126 | Annotatable::GenericParam(..)
127 | Annotatable::Param(..)
6a06907d 128 | Annotatable::FieldDef(..)
a2a8927a
XL
129 | Annotatable::Variant(..)
130 | Annotatable::Crate(..) => panic!("unexpected annotatable"),
5869c6ff
XL
131 }
132 }
133
134 crate fn into_tokens(self, sess: &ParseSess) -> TokenStream {
cdc7bbd5 135 nt_to_tokenstream(&self.into_nonterminal(), sess, CanSynthesizeMissingTokens::No)
74b04a01
XL
136 }
137
85aaf69f
SL
138 pub fn expect_item(self) -> P<ast::Item> {
139 match self {
140 Annotatable::Item(i) => i,
dfeec247 141 _ => panic!("expected Item"),
85aaf69f
SL
142 }
143 }
144
74b04a01 145 pub fn expect_trait_item(self) -> P<ast::AssocItem> {
85aaf69f 146 match self {
74b04a01 147 Annotatable::TraitItem(i) => i,
dfeec247 148 _ => panic!("expected Item"),
85aaf69f
SL
149 }
150 }
151
74b04a01 152 pub fn expect_impl_item(self) -> P<ast::AssocItem> {
85aaf69f 153 match self {
74b04a01 154 Annotatable::ImplItem(i) => i,
dfeec247 155 _ => panic!("expected Item"),
85aaf69f
SL
156 }
157 }
ff7c6d11 158
74b04a01 159 pub fn expect_foreign_item(self) -> P<ast::ForeignItem> {
83c7162d 160 match self {
74b04a01 161 Annotatable::ForeignItem(i) => i,
dfeec247 162 _ => panic!("expected foreign item"),
83c7162d
XL
163 }
164 }
165
0531ce1d
XL
166 pub fn expect_stmt(self) -> ast::Stmt {
167 match self {
168 Annotatable::Stmt(stmt) => stmt.into_inner(),
169 _ => panic!("expected statement"),
170 }
171 }
172
173 pub fn expect_expr(self) -> P<ast::Expr> {
174 match self {
175 Annotatable::Expr(expr) => expr,
176 _ => panic!("expected expression"),
177 }
178 }
179
e1599b0c
XL
180 pub fn expect_arm(self) -> ast::Arm {
181 match self {
182 Annotatable::Arm(arm) => arm,
dfeec247 183 _ => panic!("expected match arm"),
e1599b0c
XL
184 }
185 }
186
6a06907d 187 pub fn expect_expr_field(self) -> ast::ExprField {
e1599b0c 188 match self {
6a06907d 189 Annotatable::ExprField(field) => field,
dfeec247 190 _ => panic!("expected field"),
e1599b0c
XL
191 }
192 }
193
6a06907d 194 pub fn expect_pat_field(self) -> ast::PatField {
e1599b0c 195 match self {
6a06907d 196 Annotatable::PatField(fp) => fp,
dfeec247 197 _ => panic!("expected field pattern"),
e1599b0c
XL
198 }
199 }
200
201 pub fn expect_generic_param(self) -> ast::GenericParam {
202 match self {
203 Annotatable::GenericParam(gp) => gp,
dfeec247 204 _ => panic!("expected generic parameter"),
e1599b0c
XL
205 }
206 }
207
208 pub fn expect_param(self) -> ast::Param {
209 match self {
210 Annotatable::Param(param) => param,
dfeec247 211 _ => panic!("expected parameter"),
e1599b0c
XL
212 }
213 }
214
6a06907d 215 pub fn expect_field_def(self) -> ast::FieldDef {
e1599b0c 216 match self {
6a06907d 217 Annotatable::FieldDef(sf) => sf,
dfeec247 218 _ => panic!("expected struct field"),
e1599b0c
XL
219 }
220 }
221
222 pub fn expect_variant(self) -> ast::Variant {
223 match self {
224 Annotatable::Variant(v) => v,
dfeec247 225 _ => panic!("expected variant"),
e1599b0c
XL
226 }
227 }
a2a8927a
XL
228
229 pub fn expect_crate(self) -> ast::Crate {
230 match self {
231 Annotatable::Crate(krate) => krate,
232 _ => panic!("expected krate"),
233 }
234 }
85aaf69f
SL
235}
236
ba9703b0
XL
237/// Result of an expansion that may need to be retried.
238/// Consider using this for non-`MultiItemModifier` expanders as well.
239pub enum ExpandResult<T, U> {
240 /// Expansion produced a result (possibly dummy).
241 Ready(T),
242 /// Expansion could not produce a result and needs to be retried.
fc512014 243 Retry(U),
ba9703b0
XL
244}
245
246// `meta_item` is the attribute, and `item` is the item being modified.
85aaf69f 247pub trait MultiItemModifier {
dfeec247
XL
248 fn expand(
249 &self,
250 ecx: &mut ExtCtxt<'_>,
251 span: Span,
252 meta_item: &ast::MetaItem,
253 item: Annotatable,
ba9703b0 254 ) -> ExpandResult<Vec<Annotatable>, Annotatable>;
85aaf69f
SL
255}
256
ba9703b0 257impl<F> MultiItemModifier for F
dfeec247 258where
ba9703b0 259 F: Fn(&mut ExtCtxt<'_>, Span, &ast::MetaItem, Annotatable) -> Vec<Annotatable>,
85aaf69f 260{
dfeec247
XL
261 fn expand(
262 &self,
263 ecx: &mut ExtCtxt<'_>,
264 span: Span,
265 meta_item: &ast::MetaItem,
266 item: Annotatable,
ba9703b0
XL
267 ) -> ExpandResult<Vec<Annotatable>, Annotatable> {
268 ExpandResult::Ready(self(ecx, span, meta_item, item))
85aaf69f
SL
269 }
270}
271
9e0c209e 272pub trait ProcMacro {
ba9703b0
XL
273 fn expand<'cx>(
274 &self,
275 ecx: &'cx mut ExtCtxt<'_>,
276 span: Span,
277 ts: TokenStream,
278 ) -> Result<TokenStream, ErrorReported>;
9e0c209e
SL
279}
280
281impl<F> ProcMacro for F
dfeec247
XL
282where
283 F: Fn(TokenStream) -> TokenStream,
9e0c209e 284{
ba9703b0
XL
285 fn expand<'cx>(
286 &self,
287 _ecx: &'cx mut ExtCtxt<'_>,
288 _span: Span,
289 ts: TokenStream,
290 ) -> Result<TokenStream, ErrorReported> {
9e0c209e 291 // FIXME setup implicit context in TLS before calling self.
1b1a35ee 292 Ok(self(ts))
9e0c209e
SL
293 }
294}
295
296pub trait AttrProcMacro {
dfeec247
XL
297 fn expand<'cx>(
298 &self,
299 ecx: &'cx mut ExtCtxt<'_>,
300 span: Span,
301 annotation: TokenStream,
302 annotated: TokenStream,
ba9703b0 303 ) -> Result<TokenStream, ErrorReported>;
9e0c209e
SL
304}
305
306impl<F> AttrProcMacro for F
dfeec247
XL
307where
308 F: Fn(TokenStream, TokenStream) -> TokenStream,
9e0c209e 309{
dfeec247
XL
310 fn expand<'cx>(
311 &self,
312 _ecx: &'cx mut ExtCtxt<'_>,
313 _span: Span,
314 annotation: TokenStream,
315 annotated: TokenStream,
ba9703b0 316 ) -> Result<TokenStream, ErrorReported> {
9e0c209e 317 // FIXME setup implicit context in TLS before calling self.
1b1a35ee 318 Ok(self(annotation, annotated))
9e0c209e
SL
319 }
320}
321
1a4d82fc
JJ
322/// Represents a thing that maps token trees to Macro Results
323pub trait TTMacroExpander {
a1dfa0c6
XL
324 fn expand<'cx>(
325 &self,
9fa01778 326 ecx: &'cx mut ExtCtxt<'_>,
a1dfa0c6
XL
327 span: Span,
328 input: TokenStream,
dfeec247 329 ) -> Box<dyn MacResult + 'cx>;
223e47cc
LB
330}
331
1a4d82fc 332pub type MacroExpanderFn =
dfeec247 333 for<'cx> fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> Box<dyn MacResult + 'cx>;
223e47cc 334
1a4d82fc 335impl<F> TTMacroExpander for F
dfeec247
XL
336where
337 F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> Box<dyn MacResult + 'cx>,
1a4d82fc 338{
a1dfa0c6
XL
339 fn expand<'cx>(
340 &self,
9fa01778 341 ecx: &'cx mut ExtCtxt<'_>,
a1dfa0c6 342 span: Span,
1b1a35ee 343 input: TokenStream,
dfeec247 344 ) -> Box<dyn MacResult + 'cx> {
1b1a35ee 345 self(ecx, span, input)
1a4d82fc 346 }
223e47cc
LB
347}
348
c34b1796 349// Use a macro because forwarding to a simple function has type system issues
9346a6ac 350macro_rules! make_stmts_default {
c34b1796 351 ($me:expr) => {
dfeec247
XL
352 $me.make_expr().map(|e| {
353 smallvec![ast::Stmt {
354 id: ast::DUMMY_NODE_ID,
355 span: e.span,
356 kind: ast::StmtKind::Expr(e),
357 }]
358 })
359 };
c34b1796
AL
360}
361
1a4d82fc 362/// The result of a macro expansion. The return values of the various
c34b1796 363/// methods are spliced into the AST at the callsite of the macro.
1a4d82fc 364pub trait MacResult {
9fa01778 365 /// Creates an expression.
1a4d82fc
JJ
366 fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
367 None
368 }
94222f64 369
9fa01778 370 /// Creates zero or more items.
0bf4aa26 371 fn make_items(self: Box<Self>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
1a4d82fc
JJ
372 None
373 }
223e47cc 374
9fa01778 375 /// Creates zero or more impl items.
74b04a01 376 fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
1a4d82fc
JJ
377 None
378 }
223e47cc 379
9fa01778 380 /// Creates zero or more trait items.
74b04a01 381 fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
3157f602
XL
382 None
383 }
384
9fa01778 385 /// Creates zero or more items in an `extern {}` block
74b04a01 386 fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[P<ast::ForeignItem>; 1]>> {
dfeec247
XL
387 None
388 }
83c7162d 389
9fa01778 390 /// Creates a pattern.
1a4d82fc
JJ
391 fn make_pat(self: Box<Self>) -> Option<P<ast::Pat>> {
392 None
393 }
223e47cc 394
9fa01778 395 /// Creates zero or more statements.
1a4d82fc
JJ
396 ///
397 /// By default this attempts to create an expression statement,
398 /// returning None if that fails.
0bf4aa26 399 fn make_stmts(self: Box<Self>) -> Option<SmallVec<[ast::Stmt; 1]>> {
9346a6ac 400 make_stmts_default!(self)
1a4d82fc 401 }
e9174d1e
SL
402
403 fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
404 None
405 }
e1599b0c
XL
406
407 fn make_arms(self: Box<Self>) -> Option<SmallVec<[ast::Arm; 1]>> {
408 None
409 }
410
6a06907d 411 fn make_expr_fields(self: Box<Self>) -> Option<SmallVec<[ast::ExprField; 1]>> {
e1599b0c
XL
412 None
413 }
414
6a06907d 415 fn make_pat_fields(self: Box<Self>) -> Option<SmallVec<[ast::PatField; 1]>> {
e1599b0c
XL
416 None
417 }
418
419 fn make_generic_params(self: Box<Self>) -> Option<SmallVec<[ast::GenericParam; 1]>> {
420 None
421 }
422
423 fn make_params(self: Box<Self>) -> Option<SmallVec<[ast::Param; 1]>> {
424 None
425 }
426
6a06907d 427 fn make_field_defs(self: Box<Self>) -> Option<SmallVec<[ast::FieldDef; 1]>> {
e1599b0c
XL
428 None
429 }
430
431 fn make_variants(self: Box<Self>) -> Option<SmallVec<[ast::Variant; 1]>> {
432 None
433 }
a2a8927a
XL
434
435 fn make_crate(self: Box<Self>) -> Option<ast::Crate> {
436 // Fn-like macros cannot produce a crate.
437 unreachable!()
438 }
1a4d82fc 439}
223e47cc 440
c34b1796
AL
441macro_rules! make_MacEager {
442 ( $( $fld:ident: $t:ty, )* ) => {
443 /// `MacResult` implementation for the common case where you've already
444 /// built each form of AST that you might return.
445 #[derive(Default)]
446 pub struct MacEager {
447 $(
448 pub $fld: Option<$t>,
449 )*
450 }
451
452 impl MacEager {
453 $(
8faf50e0 454 pub fn $fld(v: $t) -> Box<dyn MacResult> {
d9579d0f 455 Box::new(MacEager {
c34b1796
AL
456 $fld: Some(v),
457 ..Default::default()
d9579d0f 458 })
c34b1796
AL
459 }
460 )*
1a4d82fc
JJ
461 }
462 }
463}
c34b1796
AL
464
465make_MacEager! {
466 expr: P<ast::Expr>,
467 pat: P<ast::Pat>,
0bf4aa26 468 items: SmallVec<[P<ast::Item>; 1]>,
74b04a01
XL
469 impl_items: SmallVec<[P<ast::AssocItem>; 1]>,
470 trait_items: SmallVec<[P<ast::AssocItem>; 1]>,
471 foreign_items: SmallVec<[P<ast::ForeignItem>; 1]>,
0bf4aa26 472 stmts: SmallVec<[ast::Stmt; 1]>,
e9174d1e 473 ty: P<ast::Ty>,
1a4d82fc 474}
c34b1796
AL
475
476impl MacResult for MacEager {
477 fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
478 self.expr
1a4d82fc 479 }
c34b1796 480
0bf4aa26 481 fn make_items(self: Box<Self>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
c34b1796 482 self.items
1a4d82fc 483 }
223e47cc 484
74b04a01 485 fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
c34b1796 486 self.impl_items
1a4d82fc 487 }
223e47cc 488
74b04a01 489 fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
3157f602
XL
490 self.trait_items
491 }
492
74b04a01 493 fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[P<ast::ForeignItem>; 1]>> {
83c7162d
XL
494 self.foreign_items
495 }
496
0bf4aa26 497 fn make_stmts(self: Box<Self>) -> Option<SmallVec<[ast::Stmt; 1]>> {
9346a6ac
AL
498 match self.stmts.as_ref().map_or(0, |s| s.len()) {
499 0 => make_stmts_default!(self),
500 _ => self.stmts,
c34b1796
AL
501 }
502 }
503
504 fn make_pat(self: Box<Self>) -> Option<P<ast::Pat>> {
505 if let Some(p) = self.pat {
506 return Some(p);
507 }
508 if let Some(e) = self.expr {
e74abb32 509 if let ast::ExprKind::Lit(_) = e.kind {
c34b1796
AL
510 return Some(P(ast::Pat {
511 id: ast::DUMMY_NODE_ID,
512 span: e.span,
e74abb32 513 kind: PatKind::Lit(e),
3dfed10e 514 tokens: None,
c34b1796
AL
515 }));
516 }
517 }
518 None
1a4d82fc 519 }
e9174d1e
SL
520
521 fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
522 self.ty
523 }
1a4d82fc 524}
223e47cc 525
1a4d82fc
JJ
526/// Fill-in macro expansion result, to allow compilation to continue
527/// after hitting errors.
c34b1796 528#[derive(Copy, Clone)]
1a4d82fc 529pub struct DummyResult {
0731742a
XL
530 is_error: bool,
531 span: Span,
223e47cc
LB
532}
533
1a4d82fc 534impl DummyResult {
9fa01778 535 /// Creates a default MacResult that can be anything.
1a4d82fc
JJ
536 ///
537 /// Use this as a return value after hitting any errors and
538 /// calling `span_err`.
dfeec247 539 pub fn any(span: Span) -> Box<dyn MacResult + 'static> {
e1599b0c 540 Box::new(DummyResult { is_error: true, span })
0731742a
XL
541 }
542
543 /// Same as `any`, but must be a valid fragment, not error.
dfeec247 544 pub fn any_valid(span: Span) -> Box<dyn MacResult + 'static> {
e1599b0c 545 Box::new(DummyResult { is_error: false, span })
1a4d82fc
JJ
546 }
547
548 /// A plain dummy expression.
0731742a 549 pub fn raw_expr(sp: Span, is_error: bool) -> P<ast::Expr> {
1a4d82fc
JJ
550 P(ast::Expr {
551 id: ast::DUMMY_NODE_ID,
e74abb32 552 kind: if is_error { ast::ExprKind::Err } else { ast::ExprKind::Tup(Vec::new()) },
1a4d82fc 553 span: sp,
dfeec247 554 attrs: ast::AttrVec::new(),
f9f354fc 555 tokens: None,
1a4d82fc
JJ
556 })
557 }
558
559 /// A plain dummy pattern.
560 pub fn raw_pat(sp: Span) -> ast::Pat {
3dfed10e 561 ast::Pat { id: ast::DUMMY_NODE_ID, kind: PatKind::Wild, span: sp, tokens: None }
1a4d82fc
JJ
562 }
563
0731742a
XL
564 /// A plain dummy type.
565 pub fn raw_ty(sp: Span, is_error: bool) -> P<ast::Ty> {
e9174d1e
SL
566 P(ast::Ty {
567 id: ast::DUMMY_NODE_ID,
e74abb32 568 kind: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(Vec::new()) },
dfeec247 569 span: sp,
1b1a35ee 570 tokens: None,
e9174d1e
SL
571 })
572 }
1a4d82fc
JJ
573}
574
575impl MacResult for DummyResult {
576 fn make_expr(self: Box<DummyResult>) -> Option<P<ast::Expr>> {
0731742a 577 Some(DummyResult::raw_expr(self.span, self.is_error))
1a4d82fc 578 }
e9174d1e 579
1a4d82fc
JJ
580 fn make_pat(self: Box<DummyResult>) -> Option<P<ast::Pat>> {
581 Some(P(DummyResult::raw_pat(self.span)))
582 }
e9174d1e 583
0bf4aa26 584 fn make_items(self: Box<DummyResult>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
e1599b0c 585 Some(SmallVec::new())
1a4d82fc 586 }
e9174d1e 587
74b04a01 588 fn make_impl_items(self: Box<DummyResult>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
e1599b0c 589 Some(SmallVec::new())
1a4d82fc 590 }
e9174d1e 591
74b04a01 592 fn make_trait_items(self: Box<DummyResult>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
e1599b0c 593 Some(SmallVec::new())
3157f602
XL
594 }
595
74b04a01 596 fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[P<ast::ForeignItem>; 1]>> {
e1599b0c 597 Some(SmallVec::new())
83c7162d
XL
598 }
599
0bf4aa26 600 fn make_stmts(self: Box<DummyResult>) -> Option<SmallVec<[ast::Stmt; 1]>> {
b7449926 601 Some(smallvec![ast::Stmt {
3157f602 602 id: ast::DUMMY_NODE_ID,
e74abb32 603 kind: ast::StmtKind::Expr(DummyResult::raw_expr(self.span, self.is_error)),
3157f602 604 span: self.span,
b7449926 605 }])
3157f602
XL
606 }
607
608 fn make_ty(self: Box<DummyResult>) -> Option<P<ast::Ty>> {
0731742a 609 Some(DummyResult::raw_ty(self.span, self.is_error))
1a4d82fc 610 }
e1599b0c
XL
611
612 fn make_arms(self: Box<DummyResult>) -> Option<SmallVec<[ast::Arm; 1]>> {
dfeec247 613 Some(SmallVec::new())
e1599b0c
XL
614 }
615
6a06907d 616 fn make_expr_fields(self: Box<DummyResult>) -> Option<SmallVec<[ast::ExprField; 1]>> {
e1599b0c
XL
617 Some(SmallVec::new())
618 }
619
6a06907d 620 fn make_pat_fields(self: Box<DummyResult>) -> Option<SmallVec<[ast::PatField; 1]>> {
e1599b0c
XL
621 Some(SmallVec::new())
622 }
623
624 fn make_generic_params(self: Box<DummyResult>) -> Option<SmallVec<[ast::GenericParam; 1]>> {
625 Some(SmallVec::new())
626 }
627
628 fn make_params(self: Box<DummyResult>) -> Option<SmallVec<[ast::Param; 1]>> {
629 Some(SmallVec::new())
630 }
631
6a06907d 632 fn make_field_defs(self: Box<DummyResult>) -> Option<SmallVec<[ast::FieldDef; 1]>> {
e1599b0c
XL
633 Some(SmallVec::new())
634 }
635
636 fn make_variants(self: Box<DummyResult>) -> Option<SmallVec<[ast::Variant; 1]>> {
637 Some(SmallVec::new())
638 }
1a4d82fc
JJ
639}
640
dc9dc135
XL
641/// A syntax extension kind.
642pub enum SyntaxExtensionKind {
643 /// A token-based function-like macro.
644 Bang(
645 /// An expander with signature TokenStream -> TokenStream.
646 Box<dyn ProcMacro + sync::Sync + sync::Send>,
647 ),
648
649 /// An AST-based function-like macro.
650 LegacyBang(
651 /// An expander with signature TokenStream -> AST.
652 Box<dyn TTMacroExpander + sync::Sync + sync::Send>,
653 ),
654
655 /// A token-based attribute macro.
656 Attr(
657 /// An expander with signature (TokenStream, TokenStream) -> TokenStream.
658 /// The first TokenSteam is the attribute itself, the second is the annotated item.
659 /// The produced TokenSteam replaces the input TokenSteam.
660 Box<dyn AttrProcMacro + sync::Sync + sync::Send>,
661 ),
662
663 /// An AST-based attribute macro.
664 LegacyAttr(
665 /// An expander with signature (AST, AST) -> AST.
666 /// The first AST fragment is the attribute itself, the second is the annotated item.
667 /// The produced AST fragment replaces the input AST fragment.
668 Box<dyn MultiItemModifier + sync::Sync + sync::Send>,
669 ),
670
671 /// A trivial attribute "macro" that does nothing,
672 /// only keeps the attribute and marks it as inert,
673 /// thus making it ineligible for further expansion.
94222f64 674 NonMacroAttr,
9e0c209e 675
dc9dc135
XL
676 /// A token-based derive macro.
677 Derive(
678 /// An expander with signature TokenStream -> TokenStream (not yet).
679 /// The produced TokenSteam is appended to the input TokenSteam.
680 Box<dyn MultiItemModifier + sync::Sync + sync::Send>,
681 ),
682
683 /// An AST-based derive macro.
684 LegacyDerive(
685 /// An expander with signature AST -> AST.
686 /// The produced AST fragment is appended to the input AST fragment.
687 Box<dyn MultiItemModifier + sync::Sync + sync::Send>,
688 ),
689}
690
691/// A struct representing a macro definition in "lowered" form ready for expansion.
692pub struct SyntaxExtension {
693 /// A syntax extension kind.
694 pub kind: SyntaxExtensionKind,
416331ca
XL
695 /// Span of the macro definition.
696 pub span: Span,
f035d41b 697 /// List of unstable features that are treated as stable inside this macro.
dc9dc135
XL
698 pub allow_internal_unstable: Option<Lrc<[Symbol]>>,
699 /// Suppresses the `unsafe_code` lint for code produced by this macro.
700 pub allow_internal_unsafe: bool,
701 /// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`) for this macro.
702 pub local_inner_macros: bool,
416331ca
XL
703 /// The macro's stability info.
704 pub stability: Option<Stability>,
705 /// The macro's deprecation info.
706 pub deprecation: Option<Deprecation>,
dc9dc135
XL
707 /// Names of helper attributes registered by this macro.
708 pub helper_attrs: Vec<Symbol>,
709 /// Edition of the crate in which this macro is defined.
710 pub edition: Edition,
416331ca
XL
711 /// Built-in macros have a couple of special properties like availability
712 /// in `#[no_implicit_prelude]` modules, so we have to keep this flag.
5869c6ff 713 pub builtin_name: Option<Symbol>,
dc9dc135
XL
714}
715
8bb4bdeb 716impl SyntaxExtension {
9fa01778 717 /// Returns which kind of macro calls this syntax extension.
dc9dc135
XL
718 pub fn macro_kind(&self) -> MacroKind {
719 match self.kind {
dfeec247
XL
720 SyntaxExtensionKind::Bang(..) | SyntaxExtensionKind::LegacyBang(..) => MacroKind::Bang,
721 SyntaxExtensionKind::Attr(..)
722 | SyntaxExtensionKind::LegacyAttr(..)
94222f64 723 | SyntaxExtensionKind::NonMacroAttr => MacroKind::Attr,
dfeec247
XL
724 SyntaxExtensionKind::Derive(..) | SyntaxExtensionKind::LegacyDerive(..) => {
725 MacroKind::Derive
726 }
7cac9316
XL
727 }
728 }
94b46f34 729
dc9dc135
XL
730 /// Constructs a syntax extension with default properties.
731 pub fn default(kind: SyntaxExtensionKind, edition: Edition) -> SyntaxExtension {
732 SyntaxExtension {
416331ca 733 span: DUMMY_SP,
dc9dc135
XL
734 allow_internal_unstable: None,
735 allow_internal_unsafe: false,
736 local_inner_macros: false,
416331ca
XL
737 stability: None,
738 deprecation: None,
dc9dc135
XL
739 helper_attrs: Vec::new(),
740 edition,
5869c6ff 741 builtin_name: None,
dc9dc135
XL
742 kind,
743 }
744 }
745
e1599b0c
XL
746 /// Constructs a syntax extension with the given properties
747 /// and other properties converted from attributes.
748 pub fn new(
3dfed10e 749 sess: &Session,
e1599b0c
XL
750 kind: SyntaxExtensionKind,
751 span: Span,
752 helper_attrs: Vec<Symbol>,
753 edition: Edition,
f9f354fc 754 name: Symbol,
e1599b0c
XL
755 attrs: &[ast::Attribute],
756 ) -> SyntaxExtension {
6a06907d
XL
757 let allow_internal_unstable =
758 attr::allow_internal_unstable(sess, &attrs).collect::<Vec<Symbol>>();
e1599b0c
XL
759
760 let mut local_inner_macros = false;
3dfed10e 761 if let Some(macro_export) = sess.find_by_name(attrs, sym::macro_export) {
e1599b0c
XL
762 if let Some(l) = macro_export.meta_item_list() {
763 local_inner_macros = attr::list_contains_name(&l, sym::local_inner_macros);
764 }
765 }
766
136023e0 767 let (builtin_name, helper_attrs) = sess
5869c6ff 768 .find_by_name(attrs, sym::rustc_builtin_macro)
136023e0
XL
769 .map(|attr| {
770 // Override `helper_attrs` passed above if it's a built-in macro,
771 // marking `proc_macro_derive` macros as built-in is not a realistic use case.
772 parse_macro_name_and_helper_attrs(sess.diagnostic(), attr, "built-in").map_or_else(
773 || (Some(name), Vec::new()),
774 |(name, helper_attrs)| (Some(name), helper_attrs),
775 )
776 })
777 .unwrap_or_else(|| (None, helper_attrs));
60c5eb7d 778 let (stability, const_stability) = attr::find_stability(&sess, attrs, span);
6a06907d 779 if let Some((_, sp)) = const_stability {
3dfed10e
XL
780 sess.parse_sess
781 .span_diagnostic
6a06907d
XL
782 .struct_span_err(sp, "macros cannot have const stability attributes")
783 .span_label(sp, "invalid const stability attribute")
784 .span_label(
785 sess.source_map().guess_head_span(span),
786 "const stability attribute affects this macro",
787 )
788 .emit();
60c5eb7d 789 }
e1599b0c
XL
790
791 SyntaxExtension {
792 kind,
793 span,
6a06907d
XL
794 allow_internal_unstable: (!allow_internal_unstable.is_empty())
795 .then(|| allow_internal_unstable.into()),
3dfed10e 796 allow_internal_unsafe: sess.contains_name(attrs, sym::allow_internal_unsafe),
e1599b0c 797 local_inner_macros,
6a06907d 798 stability: stability.map(|(s, _)| s),
29967ef6 799 deprecation: attr::find_deprecation(&sess, attrs).map(|(d, _)| d),
e1599b0c
XL
800 helper_attrs,
801 edition,
5869c6ff 802 builtin_name,
e1599b0c
XL
803 }
804 }
805
416331ca 806 pub fn dummy_bang(edition: Edition) -> SyntaxExtension {
dfeec247
XL
807 fn expander<'cx>(
808 _: &'cx mut ExtCtxt<'_>,
809 span: Span,
810 _: TokenStream,
811 ) -> Box<dyn MacResult + 'cx> {
416331ca 812 DummyResult::any(span)
dc9dc135 813 }
416331ca 814 SyntaxExtension::default(SyntaxExtensionKind::LegacyBang(Box::new(expander)), edition)
dc9dc135
XL
815 }
816
416331ca 817 pub fn dummy_derive(edition: Edition) -> SyntaxExtension {
dfeec247
XL
818 fn expander(
819 _: &mut ExtCtxt<'_>,
820 _: Span,
821 _: &ast::MetaItem,
822 _: Annotatable,
823 ) -> Vec<Annotatable> {
416331ca
XL
824 Vec::new()
825 }
826 SyntaxExtension::default(SyntaxExtensionKind::Derive(Box::new(expander)), edition)
827 }
828
94222f64
XL
829 pub fn non_macro_attr(edition: Edition) -> SyntaxExtension {
830 SyntaxExtension::default(SyntaxExtensionKind::NonMacroAttr, edition)
416331ca
XL
831 }
832
f9f354fc
XL
833 pub fn expn_data(
834 &self,
136023e0 835 parent: LocalExpnId,
f9f354fc
XL
836 call_site: Span,
837 descr: Symbol,
838 macro_def_id: Option<DefId>,
136023e0 839 parent_module: Option<DefId>,
f9f354fc 840 ) -> ExpnData {
5869c6ff 841 ExpnData::new(
136023e0
XL
842 ExpnKind::Macro(self.macro_kind(), descr),
843 parent.to_expn_id(),
e1599b0c 844 call_site,
5869c6ff
XL
845 self.span,
846 self.allow_internal_unstable.clone(),
847 self.allow_internal_unsafe,
848 self.local_inner_macros,
849 self.edition,
f9f354fc 850 macro_def_id,
136023e0 851 parent_module,
5869c6ff 852 )
94b46f34 853 }
1a4d82fc
JJ
854}
855
416331ca
XL
856/// Error type that denotes indeterminacy.
857pub struct Indeterminate;
858
136023e0 859pub type DeriveResolutions = Vec<(ast::Path, Annotatable, Option<Lrc<SyntaxExtension>>)>;
cdc7bbd5 860
f035d41b 861pub trait ResolverExpand {
e1599b0c 862 fn next_node_id(&mut self) -> NodeId;
c295e0f8 863 fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId;
9e0c209e 864
416331ca 865 fn resolve_dollar_crates(&mut self);
136023e0
XL
866 fn visit_ast_fragment_with_placeholders(
867 &mut self,
868 expn_id: LocalExpnId,
869 fragment: &AstFragment,
870 );
5869c6ff 871 fn register_builtin_macro(&mut self, name: Symbol, ext: SyntaxExtensionKind);
9e0c209e 872
e1599b0c
XL
873 fn expansion_for_ast_pass(
874 &mut self,
875 call_site: Span,
876 pass: AstPass,
877 features: &[Symbol],
878 parent_module_id: Option<NodeId>,
136023e0 879 ) -> LocalExpnId;
e1599b0c 880
476ff2be 881 fn resolve_imports(&mut self);
0531ce1d 882
e1599b0c 883 fn resolve_macro_invocation(
dfeec247
XL
884 &mut self,
885 invoc: &Invocation,
136023e0 886 eager_expansion_root: LocalExpnId,
dfeec247 887 force: bool,
6a06907d 888 ) -> Result<Lrc<SyntaxExtension>, Indeterminate>;
b7449926 889
e74abb32 890 fn check_unused_macros(&mut self);
970d7e83 891
fc512014
XL
892 // Resolver interfaces for specific built-in macros.
893 /// Does `#[derive(...)]` attribute with the given `ExpnId` have built-in `Copy` inside it?
136023e0 894 fn has_derive_copy(&self, expn_id: LocalExpnId) -> bool;
6a06907d
XL
895 /// Resolve paths inside the `#[derive(...)]` attribute with the given `ExpnId`.
896 fn resolve_derives(
897 &mut self,
136023e0 898 expn_id: LocalExpnId,
6a06907d 899 force: bool,
cdc7bbd5 900 derive_paths: &dyn Fn() -> DeriveResolutions,
6a06907d
XL
901 ) -> Result<(), Indeterminate>;
902 /// Take resolutions for paths inside the `#[derive(...)]` attribute with the given `ExpnId`
903 /// back from resolver.
136023e0 904 fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<DeriveResolutions>;
fc512014 905 /// Path resolution logic for `#[cfg_accessible(path)]`.
136023e0
XL
906 fn cfg_accessible(
907 &mut self,
908 expn_id: LocalExpnId,
909 path: &ast::Path,
910 ) -> Result<bool, Indeterminate>;
17df50a5
XL
911
912 /// Decodes the proc-macro quoted span in the specified crate, with the specified id.
913 /// No caching is performed.
914 fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span;
94222f64
XL
915
916 /// The order of items in the HIR is unrelated to the order of
917 /// items in the AST. However, we generate proc macro harnesses
918 /// based on the AST order, and later refer to these harnesses
919 /// from the HIR. This field keeps track of the order in which
920 /// we generated proc macros harnesses, so that we can map
921 /// HIR proc macros items back to their harness items.
922 fn declare_proc_macro(&mut self, id: NodeId);
b7449926
XL
923}
924
6a06907d 925#[derive(Clone, Default)]
9e0c209e 926pub struct ModuleData {
6a06907d 927 /// Path to the module starting from the crate name, like `my_crate::foo::bar`.
f9f354fc 928 pub mod_path: Vec<Ident>,
6a06907d
XL
929 /// Stack of paths to files loaded by out-of-line module items,
930 /// used to detect and report recursive module inclusions.
931 pub file_path_stack: Vec<PathBuf>,
932 /// Directory to search child module files in,
933 /// often (but not necessarily) the parent of the top file path on the `file_path_stack`.
934 pub dir_path: PathBuf,
935}
936
937impl ModuleData {
938 pub fn with_dir_path(&self, dir_path: PathBuf) -> ModuleData {
939 ModuleData {
940 mod_path: self.mod_path.clone(),
941 file_path_stack: self.file_path_stack.clone(),
942 dir_path,
943 }
944 }
9e0c209e
SL
945}
946
947#[derive(Clone)]
948pub struct ExpansionData {
136023e0 949 pub id: LocalExpnId,
9e0c209e 950 pub depth: usize,
9e0c209e 951 pub module: Rc<ModuleData>,
6a06907d 952 pub dir_ownership: DirOwnership,
416331ca 953 pub prior_type_ascription: Option<(Span, bool)>,
136023e0
XL
954 /// Some parent node that is close to this macro call
955 pub lint_node_id: NodeId,
94222f64 956 pub is_trailing_mac: bool,
3157f602
XL
957}
958
6a06907d
XL
959type OnExternModLoaded<'a> =
960 Option<&'a dyn Fn(Ident, Vec<Attribute>, Vec<P<Item>>, Span) -> (Vec<Attribute>, Vec<P<Item>>)>;
961
1a4d82fc 962/// One of these is made during expansion and incrementally updated as we go;
7cac9316 963/// when a macro expansion occurs, the resulting nodes have the `backtrace()
e1599b0c 964/// -> expn_data` of their expansion context stored into their span.
1a4d82fc 965pub struct ExtCtxt<'a> {
3dfed10e 966 pub sess: &'a Session,
85aaf69f 967 pub ecfg: expand::ExpansionConfig<'a>,
f9f354fc 968 pub reduced_recursion_limit: Option<Limit>,
ff7c6d11 969 pub root_path: PathBuf,
f035d41b 970 pub resolver: &'a mut dyn ResolverExpand,
9e0c209e 971 pub current_expansion: ExpansionData,
fc512014
XL
972 /// Error recovery mode entered when expansion is stuck
973 /// (or during eager expansion, but that's a hack).
974 pub force_mode: bool,
b7449926 975 pub expansions: FxHashMap<Span, Vec<String>>,
ba9703b0 976 /// Called directly after having parsed an external `mod foo;` in expansion.
5869c6ff
XL
977 ///
978 /// `Ident` is the module name.
6a06907d 979 pub(super) extern_mod_loaded: OnExternModLoaded<'a>,
94222f64
XL
980 /// When we 'expand' an inert attribute, we leave it
981 /// in the AST, but insert it here so that we know
982 /// not to expand it again.
983 pub(super) expanded_inert_attrs: MarkedAttrs,
1a4d82fc
JJ
984}
985
986impl<'a> ExtCtxt<'a> {
dfeec247 987 pub fn new(
3dfed10e 988 sess: &'a Session,
dfeec247 989 ecfg: expand::ExpansionConfig<'a>,
f035d41b 990 resolver: &'a mut dyn ResolverExpand,
6a06907d 991 extern_mod_loaded: OnExternModLoaded<'a>,
dfeec247 992 ) -> ExtCtxt<'a> {
1a4d82fc 993 ExtCtxt {
3dfed10e 994 sess,
3b2f2976 995 ecfg,
ba9703b0 996 reduced_recursion_limit: None,
3b2f2976 997 resolver,
ba9703b0
XL
998 extern_mod_loaded,
999 root_path: PathBuf::new(),
9e0c209e 1000 current_expansion: ExpansionData {
136023e0 1001 id: LocalExpnId::ROOT,
9e0c209e 1002 depth: 0,
6a06907d
XL
1003 module: Default::default(),
1004 dir_ownership: DirOwnership::Owned { relative: None },
416331ca 1005 prior_type_ascription: None,
136023e0 1006 lint_node_id: ast::CRATE_NODE_ID,
94222f64 1007 is_trailing_mac: false,
9e0c209e 1008 },
fc512014 1009 force_mode: false,
b7449926 1010 expansions: FxHashMap::default(),
94222f64 1011 expanded_inert_attrs: MarkedAttrs::new(),
970d7e83
LB
1012 }
1013 }
1014
b039eaaf 1015 /// Returns a `Folder` for deeply expanding all macros in an AST node.
1a4d82fc 1016 pub fn expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
9e0c209e
SL
1017 expand::MacroExpander::new(self, false)
1018 }
1019
9fa01778
XL
1020 /// Returns a `Folder` that deeply expands all macros and assigns all `NodeId`s in an AST node.
1021 /// Once `NodeId`s are assigned, the node may not be expanded, removed, or otherwise modified.
9e0c209e
SL
1022 pub fn monotonic_expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
1023 expand::MacroExpander::new(self, true)
1a4d82fc 1024 }
e1599b0c 1025 pub fn new_parser_from_tts(&self, stream: TokenStream) -> parser::Parser<'a> {
3dfed10e 1026 rustc_parse::stream_to_parser(&self.sess.parse_sess, stream, MACRO_ARGUMENTS)
1a4d82fc 1027 }
dfeec247 1028 pub fn source_map(&self) -> &'a SourceMap {
3dfed10e 1029 self.sess.parse_sess.source_map()
dfeec247
XL
1030 }
1031 pub fn parse_sess(&self) -> &'a ParseSess {
3dfed10e 1032 &self.sess.parse_sess
dfeec247 1033 }
1a4d82fc 1034 pub fn call_site(&self) -> Span {
e1599b0c
XL
1035 self.current_expansion.id.expn_data().call_site
1036 }
1037
1038 /// Equivalent of `Span::def_site` from the proc macro API,
1039 /// except that the location is taken from the span passed as an argument.
1040 pub fn with_def_site_ctxt(&self, span: Span) -> Span {
136023e0 1041 span.with_def_site_ctxt(self.current_expansion.id.to_expn_id())
cc61c64b 1042 }
e1599b0c
XL
1043
1044 /// Equivalent of `Span::call_site` from the proc macro API,
1045 /// except that the location is taken from the span passed as an argument.
1046 pub fn with_call_site_ctxt(&self, span: Span) -> Span {
136023e0 1047 span.with_call_site_ctxt(self.current_expansion.id.to_expn_id())
970d7e83 1048 }
d9579d0f 1049
e74abb32
XL
1050 /// Equivalent of `Span::mixed_site` from the proc macro API,
1051 /// except that the location is taken from the span passed as an argument.
1052 pub fn with_mixed_site_ctxt(&self, span: Span) -> Span {
136023e0 1053 span.with_mixed_site_ctxt(self.current_expansion.id.to_expn_id())
e74abb32
XL
1054 }
1055
d9579d0f
AL
1056 /// Returns span for the macro which originally caused the current expansion to happen.
1057 ///
1058 /// Stops backtracing at include! boundary.
cc61c64b 1059 pub fn expansion_cause(&self) -> Option<Span> {
60c5eb7d 1060 self.current_expansion.id.expansion_cause()
1a4d82fc 1061 }
1a4d82fc 1062
dfeec247 1063 pub fn struct_span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'a> {
3dfed10e 1064 self.sess.parse_sess.span_diagnostic.struct_span_err(sp, msg)
9cc50fc6 1065 }
1a4d82fc
JJ
1066
1067 /// Emit `msg` attached to `sp`, without immediately stopping
1068 /// compilation.
1069 ///
1070 /// Compilation will be stopped in the near future (at the end of
1071 /// the macro expansion phase).
2c00a5a8 1072 pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
3dfed10e 1073 self.sess.parse_sess.span_diagnostic.span_err(sp, msg);
970d7e83 1074 }
2c00a5a8 1075 pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
3dfed10e 1076 self.sess.parse_sess.span_diagnostic.span_warn(sp, msg);
970d7e83 1077 }
2c00a5a8 1078 pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
3dfed10e 1079 self.sess.parse_sess.span_diagnostic.span_bug(sp, msg);
970d7e83 1080 }
ea8adc8c 1081 pub fn trace_macros_diag(&mut self) {
7cac9316 1082 for (sp, notes) in self.expansions.iter() {
3dfed10e 1083 let mut db = self.sess.parse_sess.span_diagnostic.span_note_diag(*sp, "trace_macro");
7cac9316
XL
1084 for note in notes {
1085 db.note(note);
1086 }
1087 db.emit();
1088 }
ea8adc8c
XL
1089 // Fixme: does this result in errors?
1090 self.expansions.clear();
7cac9316 1091 }
970d7e83 1092 pub fn bug(&self, msg: &str) -> ! {
3dfed10e 1093 self.sess.parse_sess.span_diagnostic.bug(msg);
970d7e83 1094 }
970d7e83 1095 pub fn trace_macros(&self) -> bool {
d9579d0f 1096 self.ecfg.trace_mac
970d7e83 1097 }
1a4d82fc 1098 pub fn set_trace_macros(&mut self, x: bool) {
d9579d0f 1099 self.ecfg.trace_mac = x
970d7e83 1100 }
f9f354fc 1101 pub fn std_path(&self, components: &[Symbol]) -> Vec<Ident> {
e1599b0c 1102 let def_site = self.with_def_site_ctxt(DUMMY_SP);
dc9dc135 1103 iter::once(Ident::new(kw::DollarCrate, def_site))
e1599b0c 1104 .chain(components.iter().map(|&s| Ident::with_dummy_span(s)))
ff7c6d11 1105 .collect()
85aaf69f 1106 }
6a06907d
XL
1107 pub fn def_site_path(&self, components: &[Symbol]) -> Vec<Ident> {
1108 let def_site = self.with_def_site_ctxt(DUMMY_SP);
1109 components.iter().map(|&s| Ident::new(s, def_site)).collect()
1110 }
7cac9316 1111
e74abb32 1112 pub fn check_unused_macros(&mut self) {
7cac9316
XL
1113 self.resolver.check_unused_macros();
1114 }
416331ca 1115
17df50a5 1116 /// Resolves a `path` mentioned inside Rust code, returning an absolute path.
416331ca 1117 ///
17df50a5 1118 /// This unifies the logic used for resolving `include_X!`.
416331ca 1119 ///
17df50a5 1120 /// FIXME: move this to `rustc_builtin_macros` and make it private.
e74abb32
XL
1121 pub fn resolve_path(
1122 &self,
1123 path: impl Into<PathBuf>,
1124 span: Span,
1125 ) -> Result<PathBuf, DiagnosticBuilder<'a>> {
416331ca
XL
1126 let path = path.into();
1127
1128 // Relative paths are resolved relative to the file in which they are found
1129 // after macro expansion (that is, they are unhygienic).
1130 if !path.is_absolute() {
1131 let callsite = span.source_callsite();
17df50a5
XL
1132 let mut result = match self.source_map().span_to_filename(callsite) {
1133 FileName::Real(name) => name
1134 .into_local_path()
1135 .expect("attempting to resolve a file path in an external file"),
416331ca 1136 FileName::DocTest(path, _) => path,
dfeec247
XL
1137 other => {
1138 return Err(self.struct_span_err(
1139 span,
17df50a5
XL
1140 &format!(
1141 "cannot resolve relative path in non-file source `{}`",
94222f64 1142 self.source_map().filename_for_diagnostics(&other)
17df50a5 1143 ),
dfeec247
XL
1144 ));
1145 }
416331ca
XL
1146 };
1147 result.pop();
1148 result.push(path);
e74abb32 1149 Ok(result)
416331ca 1150 } else {
e74abb32 1151 Ok(path)
416331ca
XL
1152 }
1153 }
223e47cc
LB
1154}
1155
9fa01778 1156/// Extracts a string literal from the macro expanded version of `expr`,
c295e0f8
XL
1157/// returning a diagnostic error of `err_msg` if `expr` is not a string literal.
1158/// The returned bool indicates whether an applicable suggestion has already been
1159/// added to the diagnostic to avoid emitting multiple suggestions. `Err(None)`
1160/// indicates that an ast error was encountered.
8faf50e0 1161pub fn expr_to_spanned_string<'a>(
9fa01778 1162 cx: &'a mut ExtCtxt<'_>,
e1599b0c 1163 expr: P<ast::Expr>,
8faf50e0 1164 err_msg: &str,
c295e0f8 1165) -> Result<(Symbol, ast::StrStyle, Span), Option<(DiagnosticBuilder<'a>, bool)>> {
e1599b0c
XL
1166 // Perform eager expansion on the expression.
1167 // We want to be able to handle e.g., `concat!("foo", "bar")`.
1168 let expr = cx.expander().fully_expand_fragment(AstFragment::Expr(expr)).make_expr();
1169
e74abb32
XL
1170 Err(match expr.kind {
1171 ast::ExprKind::Lit(ref l) => match l.kind {
e1599b0c 1172 ast::LitKind::Str(s, style) => return Ok((s, style, expr.span)),
c295e0f8
XL
1173 ast::LitKind::ByteStr(_) => {
1174 let mut err = cx.struct_span_err(l.span, err_msg);
1175 err.span_suggestion(
1176 expr.span.shrink_to_lo(),
1177 "consider removing the leading `b`",
1178 String::new(),
1179 Applicability::MaybeIncorrect,
1180 );
1181 Some((err, true))
1182 }
48663c56 1183 ast::LitKind::Err(_) => None,
c295e0f8 1184 _ => Some((cx.struct_span_err(l.span, err_msg), false)),
1a4d82fc 1185 },
0731742a 1186 ast::ExprKind::Err => None,
c295e0f8 1187 _ => Some((cx.struct_span_err(expr.span, err_msg), false)),
8faf50e0 1188 })
223e47cc
LB
1189}
1190
c295e0f8
XL
1191/// Extracts a string literal from the macro expanded version of `expr`,
1192/// emitting `err_msg` if `expr` is not a string literal. This does not stop
1193/// compilation on error, merely emits a non-fatal error and returns `None`.
dfeec247
XL
1194pub fn expr_to_string(
1195 cx: &mut ExtCtxt<'_>,
1196 expr: P<ast::Expr>,
1197 err_msg: &str,
1198) -> Option<(Symbol, ast::StrStyle)> {
8faf50e0 1199 expr_to_spanned_string(cx, expr, err_msg)
74b04a01 1200 .map_err(|err| {
c295e0f8 1201 err.map(|(mut err, _)| {
74b04a01
XL
1202 err.emit();
1203 })
1204 })
8faf50e0 1205 .ok()
e1599b0c 1206 .map(|(symbol, style, _)| (symbol, style))
9e0c209e
SL
1207}
1208
1a4d82fc
JJ
1209/// Non-fatally assert that `tts` is empty. Note that this function
1210/// returns even when `tts` is non-empty, macros that *need* to stop
1211/// compilation should call
1212/// `cx.parse_sess.span_diagnostic.abort_if_errors()` (this should be
1213/// done as rarely as possible).
dfeec247 1214pub fn check_zero_tts(cx: &ExtCtxt<'_>, sp: Span, tts: TokenStream, name: &str) {
9346a6ac 1215 if !tts.is_empty() {
c34b1796 1216 cx.span_err(sp, &format!("{} takes no arguments", name));
223e47cc
LB
1217 }
1218}
1219
ba9703b0
XL
1220/// Parse an expression. On error, emit it, advancing to `Eof`, and return `None`.
1221pub fn parse_expr(p: &mut parser::Parser<'_>) -> Option<P<ast::Expr>> {
1222 match p.parse_expr() {
1223 Ok(e) => return Some(e),
1224 Err(mut err) => err.emit(),
1225 }
1226 while p.token != token::Eof {
1227 p.bump();
1228 }
1229 None
1230}
1231
0531ce1d 1232/// Interpreting `tts` as a comma-separated sequence of expressions,
9fa01778 1233/// expect exactly one string literal, or emit an error and return `None`.
dfeec247
XL
1234pub fn get_single_str_from_tts(
1235 cx: &mut ExtCtxt<'_>,
1236 sp: Span,
1237 tts: TokenStream,
1238 name: &str,
1239) -> Option<String> {
1a4d82fc
JJ
1240 let mut p = cx.new_parser_from_tts(tts);
1241 if p.token == token::Eof {
c34b1796 1242 cx.span_err(sp, &format!("{} takes 1 argument", name));
dfeec247 1243 return None;
1a4d82fc 1244 }
ba9703b0 1245 let ret = parse_expr(&mut p)?;
0531ce1d
XL
1246 let _ = p.eat(&token::Comma);
1247
1a4d82fc 1248 if p.token != token::Eof {
c34b1796 1249 cx.span_err(sp, &format!("{} takes 1 argument", name));
1a4d82fc 1250 }
dfeec247 1251 expr_to_string(cx, ret, "argument must be a string literal").map(|(s, _)| s.to_string())
223e47cc
LB
1252}
1253
ba9703b0
XL
1254/// Extracts comma-separated expressions from `tts`.
1255/// On error, emit it, and return `None`.
dfeec247
XL
1256pub fn get_exprs_from_tts(
1257 cx: &mut ExtCtxt<'_>,
1258 sp: Span,
1259 tts: TokenStream,
1260) -> Option<Vec<P<ast::Expr>>> {
1a4d82fc
JJ
1261 let mut p = cx.new_parser_from_tts(tts);
1262 let mut es = Vec::new();
1263 while p.token != token::Eof {
ba9703b0 1264 let expr = parse_expr(&mut p)?;
e1599b0c
XL
1265
1266 // Perform eager expansion on the expression.
1267 // We want to be able to handle e.g., `concat!("foo", "bar")`.
1268 let expr = cx.expander().fully_expand_fragment(AstFragment::Expr(expr)).make_expr();
1269
9fa01778 1270 es.push(expr);
9cc50fc6 1271 if p.eat(&token::Comma) {
1a4d82fc
JJ
1272 continue;
1273 }
1274 if p.token != token::Eof {
1275 cx.span_err(sp, "expected token: `,`");
1276 return None;
223e47cc 1277 }
223e47cc 1278 }
1a4d82fc 1279 Some(es)
223e47cc 1280}
6a06907d 1281
136023e0
XL
1282pub fn parse_macro_name_and_helper_attrs(
1283 diag: &rustc_errors::Handler,
1284 attr: &Attribute,
1285 descr: &str,
1286) -> Option<(Symbol, Vec<Symbol>)> {
1287 // Once we've located the `#[proc_macro_derive]` attribute, verify
1288 // that it's of the form `#[proc_macro_derive(Foo)]` or
1289 // `#[proc_macro_derive(Foo, attributes(A, ..))]`
1290 let list = match attr.meta_item_list() {
1291 Some(list) => list,
1292 None => return None,
1293 };
1294 if list.len() != 1 && list.len() != 2 {
1295 diag.span_err(attr.span, "attribute must have either one or two arguments");
1296 return None;
1297 }
1298 let trait_attr = match list[0].meta_item() {
1299 Some(meta_item) => meta_item,
1300 _ => {
1301 diag.span_err(list[0].span(), "not a meta item");
1302 return None;
1303 }
1304 };
1305 let trait_ident = match trait_attr.ident() {
1306 Some(trait_ident) if trait_attr.is_word() => trait_ident,
1307 _ => {
1308 diag.span_err(trait_attr.span, "must only be one word");
1309 return None;
1310 }
1311 };
1312
1313 if !trait_ident.name.can_be_raw() {
1314 diag.span_err(
1315 trait_attr.span,
1316 &format!("`{}` cannot be a name of {} macro", trait_ident, descr),
1317 );
1318 }
1319
1320 let attributes_attr = list.get(1);
1321 let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr {
1322 if !attr.has_name(sym::attributes) {
1323 diag.span_err(attr.span(), "second argument must be `attributes`")
1324 }
1325 attr.meta_item_list()
1326 .unwrap_or_else(|| {
1327 diag.span_err(attr.span(), "attribute must be of form: `attributes(foo, bar)`");
1328 &[]
1329 })
1330 .iter()
1331 .filter_map(|attr| {
1332 let attr = match attr.meta_item() {
1333 Some(meta_item) => meta_item,
1334 _ => {
1335 diag.span_err(attr.span(), "not a meta item");
1336 return None;
1337 }
1338 };
1339
1340 let ident = match attr.ident() {
1341 Some(ident) if attr.is_word() => ident,
1342 _ => {
1343 diag.span_err(attr.span, "must only be one word");
1344 return None;
1345 }
1346 };
1347 if !ident.name.can_be_raw() {
1348 diag.span_err(
1349 attr.span,
1350 &format!("`{}` cannot be a name of derive helper attribute", ident),
1351 );
1352 }
1353
1354 Some(ident.name)
1355 })
1356 .collect()
1357 } else {
1358 Vec::new()
1359 };
1360
1361 Some((trait_ident.name, proc_attrs))
1362}
1363
6a06907d
XL
1364/// This nonterminal looks like some specific enums from
1365/// `proc-macro-hack` and `procedural-masquerade` crates.
1366/// We need to maintain some special pretty-printing behavior for them due to incorrect
1367/// asserts in old versions of those crates and their wide use in the ecosystem.
1368/// See issue #73345 for more details.
1369/// FIXME(#73933): Remove this eventually.
1370pub(crate) fn pretty_printing_compatibility_hack(nt: &Nonterminal, sess: &ParseSess) -> bool {
1371 let item = match nt {
1372 Nonterminal::NtItem(item) => item,
1373 Nonterminal::NtStmt(stmt) => match &stmt.kind {
1374 ast::StmtKind::Item(item) => item,
1375 _ => return false,
1376 },
1377 _ => return false,
1378 };
1379
1380 let name = item.ident.name;
1381 if name == sym::ProceduralMasqueradeDummyType {
1382 if let ast::ItemKind::Enum(enum_def, _) = &item.kind {
1383 if let [variant] = &*enum_def.variants {
1384 if variant.ident.name == sym::Input {
1385 sess.buffer_lint_with_diagnostic(
1386 &PROC_MACRO_BACK_COMPAT,
1387 item.ident.span,
1388 ast::CRATE_NODE_ID,
1389 "using `procedural-masquerade` crate",
1390 BuiltinLintDiagnostics::ProcMacroBackCompat(
1391 "The `procedural-masquerade` crate has been unnecessary since Rust 1.30.0. \
1392 Versions of this crate below 0.1.7 will eventually stop compiling.".to_string())
1393 );
1394 return true;
1395 }
1396 }
1397 }
1398 }
1399 false
1400}