|
| 1 | +use rustc_attr_data_structures::{AttributeKind, MacroUseArgs}; |
| 2 | +use rustc_errors::DiagArgValue; |
| 3 | +use rustc_feature::{AttributeTemplate, template}; |
| 4 | +use rustc_span::{Ident, Span, Symbol, sym}; |
| 5 | +use thin_vec::ThinVec; |
| 6 | + |
| 7 | +use crate::attributes::{AcceptMapping, AttributeParser, NoArgsAttributeParser, OnDuplicate}; |
| 8 | +use crate::context::{AcceptContext, FinalizeContext, Stage}; |
| 9 | +use crate::parser::ArgParser; |
| 10 | +use crate::session_diagnostics; |
| 11 | + |
| 12 | +pub(crate) struct MacroEscapeParser; |
| 13 | +impl<S: Stage> NoArgsAttributeParser<S> for MacroEscapeParser { |
| 14 | + const PATH: &[Symbol] = &[sym::macro_escape]; |
| 15 | + const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn; |
| 16 | + const CREATE: fn(Span) -> AttributeKind = AttributeKind::MacroEscape; |
| 17 | +} |
| 18 | + |
| 19 | +/// `#[macro_use]` attributes can either: |
| 20 | +/// - Use all macros from a crate, if provided without arguments |
| 21 | +/// - Use specific macros from a crate, if provided with arguments `#[macro_use(macro1, macro2)]` |
| 22 | +/// A warning should be provided if an use all is combined with specific uses, or if multiple use-alls are used. |
| 23 | +#[derive(Default)] |
| 24 | +pub(crate) struct MacroUseParser { |
| 25 | + /// All specific imports found so far |
| 26 | + uses: ThinVec<Ident>, |
| 27 | + /// Span of the first `#[macro_use]` arguments without arguments, used for linting |
| 28 | + use_all: Option<Span>, |
| 29 | + /// Spans of all `#[macro_use]` arguments with arguments, used for linting |
| 30 | + uses_attr_spans: ThinVec<Span>, |
| 31 | + /// Span of the first `#[macro_use]` argument, used as the span for this attribute |
| 32 | + first_span: Option<Span>, |
| 33 | +} |
| 34 | + |
| 35 | +const MACRO_USE_TEMPLATE: AttributeTemplate = template!(Word, List: "name1, name2, ..."); |
| 36 | + |
| 37 | +impl<S: Stage> AttributeParser<S> for MacroUseParser { |
| 38 | + const ATTRIBUTES: AcceptMapping<Self, S> = &[( |
| 39 | + &[sym::macro_use], |
| 40 | + MACRO_USE_TEMPLATE, |
| 41 | + |group: &mut Self, cx: &mut AcceptContext<'_, '_, S>, args| { |
| 42 | + let span = cx.attr_span; |
| 43 | + group.first_span.get_or_insert(span); |
| 44 | + match args { |
| 45 | + ArgParser::NoArgs => { |
| 46 | + // If there is a `#[macro_use]` import already, give a warning |
| 47 | + if let Some(old_attr) = group.use_all.replace(span) { |
| 48 | + cx.warn_unused_duplicate(old_attr, span); |
| 49 | + } |
| 50 | + } |
| 51 | + ArgParser::List(list) => { |
| 52 | + let mut arguments = ThinVec::new(); |
| 53 | + |
| 54 | + if list.is_empty() { |
| 55 | + cx.warn_empty_attribute(list.span); |
| 56 | + return; |
| 57 | + } |
| 58 | + group.uses_attr_spans.push(cx.attr_span); |
| 59 | + |
| 60 | + for item in list.mixed() { |
| 61 | + let Some(item) = item.meta_item() else { |
| 62 | + cx.expected_identifier(item.span()); |
| 63 | + continue; |
| 64 | + }; |
| 65 | + if let Err(err_span) = item.args().no_args() { |
| 66 | + cx.expected_no_args(err_span); |
| 67 | + continue; |
| 68 | + } |
| 69 | + let Some(item) = item.path().word() else { |
| 70 | + cx.expected_identifier(item.span()); |
| 71 | + continue; |
| 72 | + }; |
| 73 | + arguments.push(item); |
| 74 | + } |
| 75 | + |
| 76 | + group.uses.extend(arguments); |
| 77 | + } |
| 78 | + ArgParser::NameValue(_) => { |
| 79 | + let suggestions = MACRO_USE_TEMPLATE.suggestions(false, sym::macro_use); |
| 80 | + cx.emit_err(session_diagnostics::IllFormedAttributeInputLint { |
| 81 | + num_suggestions: suggestions.len(), |
| 82 | + suggestions: DiagArgValue::StrListSepByAnd( |
| 83 | + suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(), |
| 84 | + ), |
| 85 | + span, |
| 86 | + }); |
| 87 | + return; |
| 88 | + } |
| 89 | + }; |
| 90 | + }, |
| 91 | + )]; |
| 92 | + |
| 93 | + fn finalize(self, cx: &mut FinalizeContext<'_, '_, S>) -> Option<AttributeKind> { |
| 94 | + let arguments = if let Some(use_all) = self.use_all { |
| 95 | + // If there is a `#[macro_use]` attribute, warn on all `#[macro_use(...)]` attributes since everything is already imported |
| 96 | + for specific_use in self.uses_attr_spans { |
| 97 | + cx.warn_unused_duplicate(use_all, specific_use); |
| 98 | + } |
| 99 | + MacroUseArgs::UseAll |
| 100 | + } else { |
| 101 | + MacroUseArgs::UseSpecific(self.uses) |
| 102 | + }; |
| 103 | + Some(AttributeKind::MacroUse { span: self.first_span?, arguments }) |
| 104 | + } |
| 105 | +} |
0 commit comments