use subdiagnostic for message

This commit is contained in:
Michael Goulet 2022-07-08 03:21:49 +00:00
parent 2a973e2abc
commit d2e5a929b9
3 changed files with 23 additions and 3 deletions

View File

@ -0,0 +1,5 @@
expand-explain-doc-comment-outer =
outer doc comments expand to `#[doc = "..."]`, which is what this macro attempted to match
expand-explain-doc-comment-inner =
inner doc comments expand to `#![doc = "..."]`, which is what this macro attempted to match

View File

@ -33,11 +33,12 @@ pub use unic_langid::{langid, LanguageIdentifier};
fluent_messages! {
borrowck => "../locales/en-US/borrowck.ftl",
builtin_macros => "../locales/en-US/builtin_macros.ftl",
const_eval => "../locales/en-US/const_eval.ftl",
expand => "../locales/en-US/expand.ftl",
lint => "../locales/en-US/lint.ftl",
parser => "../locales/en-US/parser.ftl",
privacy => "../locales/en-US/privacy.ftl",
typeck => "../locales/en-US/typeck.ftl",
const_eval => "../locales/en-US/const_eval.ftl",
}
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};

View File

@ -594,6 +594,20 @@ pub fn compile_declarative_macro(
(mk_syn_ext(expander), rule_spans)
}
#[derive(SessionSubdiagnostic)]
enum ExplainDocComment {
#[label(expand::explain_doc_comment_inner)]
Inner {
#[primary_span]
span: Span,
},
#[label(expand::explain_doc_comment_outer)]
Outer {
#[primary_span]
span: Span,
},
}
fn annotate_doc_comment(
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
sm: &SourceMap,
@ -601,9 +615,9 @@ fn annotate_doc_comment(
) {
if let Ok(src) = sm.span_to_snippet(span) {
if src.starts_with("///") || src.starts_with("/**") {
err.span_label(span, "outer doc comments expand to `#[doc = \"...\"]`, which is what this macro attempted to match");
err.subdiagnostic(ExplainDocComment::Outer { span });
} else if src.starts_with("//!") || src.starts_with("/*!") {
err.span_label(span, "inner doc comments expand to `#![doc = \"...\"]`, which is what this macro attempted to match");
err.subdiagnostic(ExplainDocComment::Inner { span });
}
}
}