|
| 1 | +use rustc_attr_data_structures::AttributeKind::MacroExport; |
| 2 | +use rustc_attr_data_structures::lints::AttributeLintKind; |
1 | 3 | use rustc_attr_data_structures::{AttributeKind, MacroUseArgs}; |
2 | 4 | use rustc_errors::DiagArgValue; |
3 | 5 | use rustc_feature::{AttributeTemplate, template}; |
4 | 6 | use rustc_span::{Span, Symbol, sym}; |
5 | 7 | use thin_vec::ThinVec; |
6 | 8 |
|
7 | | -use crate::attributes::{AcceptMapping, AttributeParser, NoArgsAttributeParser, OnDuplicate}; |
| 9 | +use crate::attributes::{ |
| 10 | + AcceptMapping, AttributeOrder, AttributeParser, NoArgsAttributeParser, OnDuplicate, |
| 11 | + SingleAttributeParser, |
| 12 | +}; |
8 | 13 | use crate::context::{AcceptContext, FinalizeContext, Stage}; |
9 | 14 | use crate::parser::ArgParser; |
10 | 15 | use crate::session_diagnostics; |
@@ -113,3 +118,47 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser { |
113 | 118 | Some(AttributeKind::MacroUse { span: self.first_span?, arguments: self.state }) |
114 | 119 | } |
115 | 120 | } |
| 121 | + |
| 122 | +pub(crate) struct MacroExportParser; |
| 123 | + |
| 124 | +impl<S: Stage> SingleAttributeParser<S> for crate::attributes::macro_attrs::MacroExportParser { |
| 125 | + const PATH: &[Symbol] = &[sym::macro_export]; |
| 126 | + const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost; |
| 127 | + const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn; |
| 128 | + const TEMPLATE: AttributeTemplate = template!(Word, List: "local_inner_macros"); |
| 129 | + |
| 130 | + fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { |
| 131 | + let suggestions = |
| 132 | + <Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "macro_export"); |
| 133 | + let local_inner_macros = match args { |
| 134 | + ArgParser::NoArgs => false, |
| 135 | + ArgParser::List(list) => { |
| 136 | + let Some(l) = list.single() else { |
| 137 | + let span = cx.attr_span; |
| 138 | + cx.emit_lint( |
| 139 | + AttributeLintKind::InvalidMacroExportArguments { suggestions }, |
| 140 | + span, |
| 141 | + ); |
| 142 | + return None; |
| 143 | + }; |
| 144 | + match l.meta_item().and_then(|i| i.path().word_sym()) { |
| 145 | + Some(sym::local_inner_macros) => true, |
| 146 | + _ => { |
| 147 | + let span = cx.attr_span; |
| 148 | + cx.emit_lint( |
| 149 | + AttributeLintKind::InvalidMacroExportArguments { suggestions }, |
| 150 | + span, |
| 151 | + ); |
| 152 | + return None; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + ArgParser::NameValue(_) => { |
| 157 | + let span = cx.attr_span; |
| 158 | + cx.emit_lint(AttributeLintKind::InvalidMacroExportArguments { suggestions }, span); |
| 159 | + return None; |
| 160 | + } |
| 161 | + }; |
| 162 | + Some(MacroExport { span: cx.attr_span, local_inner_macros }) |
| 163 | + } |
| 164 | +} |
0 commit comments