diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 5486e7ed77079..ff6009706dc68 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use std::rc::Rc; use std::sync::Arc; -use std::{iter, mem}; +use std::{iter, mem, slice}; use rustc_ast::mut_visit::*; use rustc_ast::tokenstream::TokenStream; @@ -2228,18 +2228,35 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { // Run attributes through the attribute parser // since they will not be detected after macro expansion. fn check_attributes(&self, attrs: &[ast::Attribute], call: &ast::MacCall) { - AttributeParser::parse_limited_all( - self.cx.sess, - attrs, - None, - AstTarget::MacroCall, - call.span(), - self.cx.current_expansion.lint_node_id, - Some(self.cx.ecfg.features), - ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed }, - Some(self.cx.resolver.registered_attr_tools()), - Some(self.cx.resolver.registered_lint_tools()), - ); + let parse = |group| { + AttributeParser::parse_limited_all( + self.cx.sess, + group, + None, + AstTarget::MacroCall, + call.span(), + self.cx.current_expansion.lint_node_id, + Some(self.cx.ecfg.features), + ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed }, + Some(self.cx.resolver.registered_attr_tools()), + Some(self.cx.resolver.registered_lint_tools()), + ) + }; + + let mut doc_attrs = vec![]; + for attr in attrs { + if matches!(attr.kind, AttrKind::DocComment(..)) { + doc_attrs.push(attr.clone()); + continue; + } + parse(slice::from_ref(attr)); + } + + // Parse all doc comments together in a group, to prevent a group of + // unused doc comments from emitting multiple lints + if !doc_attrs.is_empty() { + parse(doc_attrs.as_slice()); + } } fn expand_cfg_true( diff --git a/tests/ui/attributes/attr-on-mac-call.rs b/tests/ui/attributes/attr-on-mac-call.rs index da57853f4bddd..514542994d4b1 100644 --- a/tests/ui/attributes/attr-on-mac-call.rs +++ b/tests/ui/attributes/attr-on-mac-call.rs @@ -151,4 +151,12 @@ fn main() { }] //~^^^ ERROR attribute value must be a literal unreachable!(); + + #[deprecated] + //~^ WARN cannot be used on + //~| WARN previously accepted + #[deprecated] + //~^ WARN cannot be used on + //~| WARN previously accepted + unreachable!(); } diff --git a/tests/ui/attributes/attr-on-mac-call.stderr b/tests/ui/attributes/attr-on-mac-call.stderr index d51f3ef574688..816e58608f949 100644 --- a/tests/ui/attributes/attr-on-mac-call.stderr +++ b/tests/ui/attributes/attr-on-mac-call.stderr @@ -436,6 +436,30 @@ LL | unreachable!(); = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion -error: aborting due to 8 previous errors; 30 warnings emitted +warning: the `deprecated` attribute cannot be used on macro calls + --> $DIR/attr-on-mac-call.rs:155:7 + | +LL | #[deprecated] + | ^^^^^^^^^^ +... +LL | unreachable!(); + | + = help: the `deprecated` attribute can be applied to associated consts, associated types, constants, crates, data types, enum variants, foreign statics, functions, inherent impl blocks, macro defs, modules, statics, struct fields, traits, type aliases, and use statements + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion + +warning: the `deprecated` attribute cannot be used on macro calls + --> $DIR/attr-on-mac-call.rs:158:7 + | +LL | #[deprecated] + | ^^^^^^^^^^ +... +LL | unreachable!(); + | + = help: the `deprecated` attribute can be applied to associated consts, associated types, constants, crates, data types, enum variants, foreign statics, functions, inherent impl blocks, macro defs, modules, statics, struct fields, traits, type aliases, and use statements + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: attributes on a macro invocation apply to the invocation itself and are not forwarded to its expansion + +error: aborting due to 8 previous errors; 32 warnings emitted For more information about this error, try `rustc --explain E0658`.