1
Fork 0

errors: only eagerly translate subdiagnostics

Subdiagnostics don't need to be lazily translated, they can always be
eagerly translated. Eager translation is slightly more complex as we need
to have a `DiagCtxt` available to perform the translation, which involves
slightly more threading of that context.

This slight increase in complexity should enable later simplifications -
like passing `DiagCtxt` into `AddToDiagnostic` and moving Fluent messages
into the diagnostic structs rather than having them in separate files
(working on that was what led to this change).

Signed-off-by: David Wood <david@davidtw.co>
This commit is contained in:
David Wood 2024-02-14 14:17:27 +00:00
parent bb89df6903
commit b80fc5d4e8
No known key found for this signature in database
43 changed files with 532 additions and 388 deletions

View file

@ -7,7 +7,7 @@ use crate::mbe::{
use rustc_ast::token::{self, Token, TokenKind};
use rustc_ast::tokenstream::TokenStream;
use rustc_ast_pretty::pprust;
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, DiagnosticMessage};
use rustc_errors::{Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder, DiagnosticMessage};
use rustc_parse::parser::{Parser, Recovery};
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::Ident;
@ -58,7 +58,7 @@ pub(super) fn failed_to_match_macro<'cx>(
err.span_label(cx.source_map().guess_head_span(def_span), "when calling this macro");
}
annotate_doc_comment(&mut err, sess.source_map(), span);
annotate_doc_comment(cx.sess.dcx(), &mut err, sess.source_map(), span);
if let Some(span) = remaining_matcher.span() {
err.span_note(span, format!("while trying to match {remaining_matcher}"));
@ -311,12 +311,17 @@ enum ExplainDocComment {
},
}
pub(super) fn annotate_doc_comment(err: &mut Diagnostic, sm: &SourceMap, span: Span) {
pub(super) fn annotate_doc_comment(
dcx: &DiagCtxt,
err: &mut Diagnostic,
sm: &SourceMap,
span: Span,
) {
if let Ok(src) = sm.span_to_snippet(span) {
if src.starts_with("///") || src.starts_with("/**") {
err.subdiagnostic(ExplainDocComment::Outer { span });
err.subdiagnostic(dcx, ExplainDocComment::Outer { span });
} else if src.starts_with("//!") || src.starts_with("/*!") {
err.subdiagnostic(ExplainDocComment::Inner { span });
err.subdiagnostic(dcx, ExplainDocComment::Inner { span });
}
}
}

View file

@ -454,7 +454,7 @@ pub fn compile_declarative_macro(
let sp = token.span.substitute_dummy(def.span);
let mut err = sess.dcx().struct_span_err(sp, s);
err.span_label(sp, msg);
annotate_doc_comment(&mut err, sess.source_map(), sp);
annotate_doc_comment(sess.dcx(), &mut err, sess.source_map(), sp);
err.emit();
return dummy_syn_ext();
}