@@ -14,7 +14,7 @@ use rustc_ast::{NodeId, DUMMY_NODE_ID};
1414use rustc_ast_pretty:: pprust;
1515use rustc_attr:: { self as attr, TransparencyError } ;
1616use rustc_data_structures:: fx:: FxHashMap ;
17- use rustc_errors:: { Applicability , Diagnostic , DiagnosticBuilder } ;
17+ use rustc_errors:: { Applicability , Diagnostic , DiagnosticBuilder , ErrorGuaranteed } ;
1818use rustc_feature:: Features ;
1919use rustc_lint_defs:: builtin:: {
2020 RUST_2021_INCOMPATIBLE_OR_PATTERNS , SEMICOLON_IN_EXPRESSIONS_FROM_MACROS ,
@@ -25,6 +25,7 @@ use rustc_session::parse::ParseSess;
2525use rustc_session:: Session ;
2626use rustc_span:: edition:: Edition ;
2727use rustc_span:: hygiene:: Transparency ;
28+ use rustc_span:: source_map:: SourceMap ;
2829use rustc_span:: symbol:: { kw, sym, Ident , MacroRulesNormalizedIdent } ;
2930use rustc_span:: Span ;
3031
@@ -345,7 +346,7 @@ fn expand_macro<'cx>(
345346 if !def_span. is_dummy ( ) && !cx. source_map ( ) . is_imported ( def_span) {
346347 err. span_label ( cx. source_map ( ) . guess_head_span ( def_span) , "when calling this macro" ) ;
347348 }
348-
349+ annotate_doc_comment ( & mut err , sess . source_map ( ) , span ) ;
349350 // Check whether there's a missing comma in this macro call, like `println!("{}" a);`
350351 if let Some ( ( arg, comma_span) ) = arg. add_comma ( ) {
351352 for lhs in lhses {
@@ -453,7 +454,10 @@ pub fn compile_declarative_macro(
453454 Failure ( token, msg) => {
454455 let s = parse_failure_msg ( & token) ;
455456 let sp = token. span . substitute_dummy ( def. span ) ;
456- sess. parse_sess . span_diagnostic . struct_span_err ( sp, & s) . span_label ( sp, msg) . emit ( ) ;
457+ let mut err = sess. parse_sess . span_diagnostic . struct_span_err ( sp, & s) ;
458+ err. span_label ( sp, msg) ;
459+ annotate_doc_comment ( & mut err, sess. source_map ( ) , sp) ;
460+ err. emit ( ) ;
457461 return dummy_syn_ext ( ) ;
458462 }
459463 Error ( sp, msg) => {
@@ -590,6 +594,20 @@ pub fn compile_declarative_macro(
590594 ( mk_syn_ext ( expander) , rule_spans)
591595}
592596
597+ fn annotate_doc_comment (
598+ err : & mut DiagnosticBuilder < ' _ , ErrorGuaranteed > ,
599+ sm : & SourceMap ,
600+ span : Span ,
601+ ) {
602+ if let Ok ( src) = sm. span_to_snippet ( span) {
603+ if src. starts_with ( "///" ) || src. starts_with ( "/**" ) {
604+ err. span_label ( span, "outer doc comments expand to `#[doc = \" ...\" ]`, which is what this macro attempted to match" ) ;
605+ } else if src. starts_with ( "//!" ) || src. starts_with ( "/*!" ) {
606+ err. span_label ( span, "inner doc comments expand to `#![doc = \" ...\" ]`, which is what this macro attempted to match" ) ;
607+ }
608+ }
609+ }
610+
593611fn check_lhs_nt_follows ( sess : & ParseSess , def : & ast:: Item , lhs : & mbe:: TokenTree ) -> bool {
594612 // lhs is going to be like TokenTree::Delimited(...), where the
595613 // entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
0 commit comments