|
1 | 1 | use crate::ast::{self, NodeId, Attribute, Name, PatKind};
|
2 |
| -use crate::attr::{HasAttrs, Stability, Deprecation}; |
| 2 | +use crate::attr::{self, HasAttrs, Stability, Deprecation}; |
3 | 3 | use crate::source_map::SourceMap;
|
4 | 4 | use crate::edition::Edition;
|
5 | 5 | use crate::ext::expand::{self, AstFragment, Invocation};
|
6 | 6 | use crate::ext::hygiene::{ExpnId, Transparency};
|
7 | 7 | use crate::mut_visit::{self, MutVisitor};
|
8 |
| -use crate::parse::{self, parser, DirectoryOwnership}; |
| 8 | +use crate::parse::{self, parser, ParseSess, DirectoryOwnership}; |
9 | 9 | use crate::parse::token;
|
10 | 10 | use crate::ptr::P;
|
11 | 11 | use crate::symbol::{kw, sym, Ident, Symbol};
|
@@ -601,6 +601,69 @@ impl SyntaxExtension {
|
601 | 601 | }
|
602 | 602 | }
|
603 | 603 |
|
| 604 | + /// Constructs a syntax extension with the given properties |
| 605 | + /// and other properties converted from attributes. |
| 606 | + pub fn new( |
| 607 | + sess: &ParseSess, |
| 608 | + kind: SyntaxExtensionKind, |
| 609 | + span: Span, |
| 610 | + helper_attrs: Vec<Symbol>, |
| 611 | + edition: Edition, |
| 612 | + name: Name, |
| 613 | + attrs: &[ast::Attribute], |
| 614 | + ) -> SyntaxExtension { |
| 615 | + let allow_internal_unstable = |
| 616 | + attr::find_by_name(attrs, sym::allow_internal_unstable).map(|attr| { |
| 617 | + attr.meta_item_list() |
| 618 | + .map(|list| { |
| 619 | + list.iter() |
| 620 | + .filter_map(|it| { |
| 621 | + let name = it.ident().map(|ident| ident.name); |
| 622 | + if name.is_none() { |
| 623 | + sess.span_diagnostic.span_err( |
| 624 | + it.span(), "allow internal unstable expects feature names" |
| 625 | + ) |
| 626 | + } |
| 627 | + name |
| 628 | + }) |
| 629 | + .collect::<Vec<Symbol>>() |
| 630 | + .into() |
| 631 | + }) |
| 632 | + .unwrap_or_else(|| { |
| 633 | + sess.span_diagnostic.span_warn( |
| 634 | + attr.span, |
| 635 | + "allow_internal_unstable expects list of feature names. In the future \ |
| 636 | + this will become a hard error. Please use `allow_internal_unstable(\ |
| 637 | + foo, bar)` to only allow the `foo` and `bar` features", |
| 638 | + ); |
| 639 | + vec![sym::allow_internal_unstable_backcompat_hack].into() |
| 640 | + }) |
| 641 | + }); |
| 642 | + |
| 643 | + let mut local_inner_macros = false; |
| 644 | + if let Some(macro_export) = attr::find_by_name(attrs, sym::macro_export) { |
| 645 | + if let Some(l) = macro_export.meta_item_list() { |
| 646 | + local_inner_macros = attr::list_contains_name(&l, sym::local_inner_macros); |
| 647 | + } |
| 648 | + } |
| 649 | + |
| 650 | + let is_builtin = attr::contains_name(attrs, sym::rustc_builtin_macro); |
| 651 | + |
| 652 | + SyntaxExtension { |
| 653 | + kind, |
| 654 | + span, |
| 655 | + allow_internal_unstable, |
| 656 | + allow_internal_unsafe: attr::contains_name(attrs, sym::allow_internal_unsafe), |
| 657 | + local_inner_macros, |
| 658 | + stability: attr::find_stability(&sess, attrs, span), |
| 659 | + deprecation: attr::find_deprecation(&sess, attrs, span), |
| 660 | + helper_attrs, |
| 661 | + edition, |
| 662 | + is_builtin, |
| 663 | + is_derive_copy: is_builtin && name == sym::Copy, |
| 664 | + } |
| 665 | + } |
| 666 | + |
604 | 667 | pub fn dummy_bang(edition: Edition) -> SyntaxExtension {
|
605 | 668 | fn expander<'cx>(_: &'cx mut ExtCtxt<'_>, span: Span, _: &[TokenTree])
|
606 | 669 | -> Box<dyn MacResult + 'cx> {
|
|
0 commit comments