Rollup merge of #152041 - JonathanBrouwer:query_system_convert, r=jdonszelmann

Convert to inline diagnostics in `rustc_query_system`

For https://github.com/rust-lang/rust/issues/151366#event-22181360642

r? @jdonszelmann (or anyone else who feels like it)
This commit is contained in:
Jonathan Brouwer 2026-02-03 18:54:51 +01:00 committed by GitHub
commit 7997b11cfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 42 additions and 69 deletions

View file

@ -3807,7 +3807,6 @@ dependencies = [
"rustc_pattern_analysis",
"rustc_privacy",
"rustc_public",
"rustc_query_system",
"rustc_resolve",
"rustc_session",
"rustc_span",
@ -4566,7 +4565,6 @@ dependencies = [
"rustc_data_structures",
"rustc_errors",
"rustc_feature",
"rustc_fluent_macro",
"rustc_hashes",
"rustc_hir",
"rustc_index",

View file

@ -41,7 +41,6 @@ rustc_passes = { path = "../rustc_passes" }
rustc_pattern_analysis = { path = "../rustc_pattern_analysis" }
rustc_privacy = { path = "../rustc_privacy" }
rustc_public = { path = "../rustc_public", features = ["rustc_internal"] }
rustc_query_system = { path = "../rustc_query_system" }
rustc_resolve = { path = "../rustc_resolve" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }

View file

@ -137,7 +137,6 @@ pub static DEFAULT_LOCALE_RESOURCES: &[&str] = &[
rustc_passes::DEFAULT_LOCALE_RESOURCE,
rustc_pattern_analysis::DEFAULT_LOCALE_RESOURCE,
rustc_privacy::DEFAULT_LOCALE_RESOURCE,
rustc_query_system::DEFAULT_LOCALE_RESOURCE,
rustc_resolve::DEFAULT_LOCALE_RESOURCE,
rustc_session::DEFAULT_LOCALE_RESOURCE,
rustc_trait_selection::DEFAULT_LOCALE_RESOURCE,

View file

@ -31,7 +31,7 @@ impl<'a> DiagnosticDerive<'a> {
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
};
messages.borrow_mut().push(message.clone());
let message = message.diag_message(variant);
let message = message.diag_message(Some(variant));
let init = quote! {
let mut diag = rustc_errors::Diag::new(
@ -97,7 +97,7 @@ impl<'a> LintDiagnosticDerive<'a> {
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
};
messages.borrow_mut().push(message.clone());
let message = message.diag_message(variant);
let message = message.diag_message(Some(variant));
let primary_message = quote! {
diag.primary_message(#message);
};

View file

@ -456,7 +456,7 @@ impl DiagnosticDeriveVariantBuilder {
applicability.set_once(quote! { #static_applicability }, span);
}
let message = slug.diag_message(variant);
let message = slug.diag_message(Some(variant));
let applicability = applicability
.value()
.unwrap_or_else(|| quote! { rustc_errors::Applicability::Unspecified });
@ -487,7 +487,7 @@ impl DiagnosticDeriveVariantBuilder {
variant: &VariantInfo<'_>,
) -> TokenStream {
let fn_name = format_ident!("span_{}", kind);
let message = message.diag_message(variant);
let message = message.diag_message(Some(variant));
quote! {
diag.#fn_name(
#field_binding,
@ -504,7 +504,7 @@ impl DiagnosticDeriveVariantBuilder {
message: Message,
variant: &VariantInfo<'_>,
) -> TokenStream {
let message = message.diag_message(variant);
let message = message.diag_message(Some(variant));
quote! {
diag.#kind(#message);
}

View file

@ -14,13 +14,18 @@ pub(crate) enum Message {
}
impl Message {
pub(crate) fn diag_message(&self, variant: &VariantInfo<'_>) -> TokenStream {
/// Get the diagnostic message for this diagnostic
/// The passed `variant` is used to check whether all variables in the message are used.
/// For subdiagnostics, we cannot check this.
pub(crate) fn diag_message(&self, variant: Option<&VariantInfo<'_>>) -> TokenStream {
match self {
Message::Slug(slug) => {
quote! { crate::fluent_generated::#slug }
}
Message::Inline(message_span, message) => {
verify_fluent_message(*message_span, &message, variant);
if let Some(variant) = variant {
verify_fluent_message(*message_span, &message, variant);
}
quote! { rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed(#message)) }
}
}

View file

@ -75,7 +75,7 @@ impl SubdiagnosticDerive {
has_subdiagnostic: false,
is_enum,
};
builder.into_tokens(variant).unwrap_or_else(|v| v.to_compile_error())
builder.into_tokens().unwrap_or_else(|v| v.to_compile_error())
});
quote! {
@ -497,10 +497,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
}
}
pub(crate) fn into_tokens(
&mut self,
variant: &VariantInfo<'_>,
) -> Result<TokenStream, DiagnosticDeriveError> {
pub(crate) fn into_tokens(&mut self) -> Result<TokenStream, DiagnosticDeriveError> {
let kind_slugs = self.identify_kind()?;
let kind_stats: KindsStatistics = kind_slugs.iter().map(|(kind, _slug)| kind).collect();
@ -538,7 +535,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
let mut calls = TokenStream::new();
for (kind, slug) in kind_slugs {
let message = format_ident!("__message");
let message_stream = slug.diag_message(variant);
let message_stream = slug.diag_message(None);
calls.extend(quote! { let #message = #diag.eagerly_translate(#message_stream); });
let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind);

View file

@ -11,7 +11,6 @@ rustc_ast = { path = "../rustc_ast" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_feature = { path = "../rustc_feature" }
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }

View file

@ -1,30 +0,0 @@
query_system_cycle = cycle detected when {$stack_bottom}
.note = see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
query_system_cycle_recursive_trait_alias = trait aliases cannot be recursive
query_system_cycle_recursive_ty_alias = type aliases cannot be recursive
query_system_cycle_recursive_ty_alias_help1 = consider using a struct, enum, or union instead to break the cycle
query_system_cycle_recursive_ty_alias_help2 = see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information
query_system_cycle_stack_middle = ...which requires {$desc}...
query_system_cycle_stack_multiple = ...which again requires {$stack_bottom}, completing the cycle
query_system_cycle_stack_single = ...which immediately requires {$stack_bottom} again
query_system_cycle_usage = cycle used when {$usage}
query_system_increment_compilation = internal compiler error: encountered incremental compilation error with {$dep_node}
query_system_increment_compilation_note1 = please follow the instructions below to create a bug report with the provided information
query_system_increment_compilation_note2 = for incremental compilation bugs, having a reproduction is vital
query_system_increment_compilation_note3 = an ideal reproduction consists of the code before and some patch that then triggers the bug when applied and compiled again
query_system_increment_compilation_note4 = as a workaround, you can run {$run_cmd} to allow your project to compile
query_system_overflow_note = query depth increased by {$depth} when {$desc}
query_system_query_overflow = queries overflow the depth limit!
.help = consider increasing the recursion limit by adding a `#![recursion_limit = "{$suggested_limit}"]` attribute to your crate (`{$crate_name}`)
query_system_reentrant = internal compiler error: reentrant incremental verify failure, suppressing message

View file

@ -4,7 +4,7 @@ use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::{Span, Symbol};
#[derive(Subdiagnostic)]
#[note(query_system_cycle_stack_middle)]
#[note("...which requires {$desc}...")]
pub(crate) struct CycleStack {
#[primary_span]
pub span: Span,
@ -13,24 +13,26 @@ pub(crate) struct CycleStack {
#[derive(Subdiagnostic)]
pub(crate) enum StackCount {
#[note(query_system_cycle_stack_single)]
#[note("...which immediately requires {$stack_bottom} again")]
Single,
#[note(query_system_cycle_stack_multiple)]
#[note("...which again requires {$stack_bottom}, completing the cycle")]
Multiple,
}
#[derive(Subdiagnostic)]
pub(crate) enum Alias {
#[note(query_system_cycle_recursive_ty_alias)]
#[help(query_system_cycle_recursive_ty_alias_help1)]
#[help(query_system_cycle_recursive_ty_alias_help2)]
#[note("type aliases cannot be recursive")]
#[help("consider using a struct, enum, or union instead to break the cycle")]
#[help(
"see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information"
)]
Ty,
#[note(query_system_cycle_recursive_trait_alias)]
#[note("trait aliases cannot be recursive")]
Trait,
}
#[derive(Subdiagnostic)]
#[note(query_system_cycle_usage)]
#[note("cycle used when {$usage}")]
pub(crate) struct CycleUsage {
#[primary_span]
pub span: Span,
@ -38,7 +40,7 @@ pub(crate) struct CycleUsage {
}
#[derive(Diagnostic)]
#[diag(query_system_cycle, code = E0391)]
#[diag("cycle detected when {$stack_bottom}", code = E0391)]
pub(crate) struct Cycle {
#[primary_span]
pub span: Span,
@ -51,28 +53,34 @@ pub(crate) struct Cycle {
pub alias: Option<Alias>,
#[subdiagnostic]
pub cycle_usage: Option<CycleUsage>,
#[note]
#[note(
"see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information"
)]
pub note_span: (),
}
#[derive(Diagnostic)]
#[diag(query_system_reentrant)]
#[diag("internal compiler error: reentrant incremental verify failure, suppressing message")]
pub(crate) struct Reentrant;
#[derive(Diagnostic)]
#[diag(query_system_increment_compilation)]
#[note(query_system_increment_compilation_note1)]
#[note(query_system_increment_compilation_note2)]
#[note(query_system_increment_compilation_note3)]
#[note(query_system_increment_compilation_note4)]
#[diag("internal compiler error: encountered incremental compilation error with {$dep_node}")]
#[note("please follow the instructions below to create a bug report with the provided information")]
#[note("for incremental compilation bugs, having a reproduction is vital")]
#[note(
"an ideal reproduction consists of the code before and some patch that then triggers the bug when applied and compiled again"
)]
#[note("as a workaround, you can run {$run_cmd} to allow your project to compile")]
pub(crate) struct IncrementCompilation {
pub run_cmd: String,
pub dep_node: String,
}
#[derive(Diagnostic)]
#[help]
#[diag(query_system_query_overflow)]
#[help(
"consider increasing the recursion limit by adding a `#![recursion_limit = \"{$suggested_limit}\"]` attribute to your crate (`{$crate_name}`)"
)]
#[diag("queries overflow the depth limit!")]
pub struct QueryOverflow {
#[primary_span]
pub span: Span,
@ -83,7 +91,7 @@ pub struct QueryOverflow {
}
#[derive(Subdiagnostic)]
#[note(query_system_overflow_note)]
#[note("query depth increased by {$depth} when {$desc}")]
pub struct QueryOverflowNote {
pub desc: String,
pub depth: usize,

View file

@ -14,5 +14,3 @@ mod values;
pub use error::{QueryOverflow, QueryOverflowNote};
pub use values::Value;
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }