Skip to content

Commit 22c5c83

Browse files
authored
Rollup merge of #100844 - evopen:migrate-diag, r=davidtwco
migrate rustc_query_system to use SessionDiagnostic issues: * variable list is not supported in fluent * ~~cannot have two sub diagnostic with the same tag (eg. 2 .note or 2 .help)~~ allow multiple tag with SessionSubdiagnostic derive
2 parents 2af2cda + 7ce59eb commit 22c5c83

File tree

10 files changed

+475
-804
lines changed

10 files changed

+475
-804
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
query_system_reentrant = internal compiler error: re-entrant incremental verify failure, suppressing message
2+
3+
query_system_increment_compilation = internal compiler error: encountered incremental compilation error with {$dep_node}
4+
.help = This is a known issue with the compiler. Run {$run_cmd} to allow your project to compile
5+
6+
query_system_increment_compilation_note1 = Please follow the instructions below to create a bug report with the provided information
7+
query_system_increment_compilation_note2 = See <https://github.com/rust-lang/rust/issues/84970> for more information
8+
9+
query_system_cycle = cycle detected when {$stack_bottom}
10+
11+
query_system_cycle_usage = cycle used when {$usage}
12+
13+
query_system_cycle_stack_single = ...which immediately requires {$stack_bottom} again
14+
15+
query_system_cycle_stack_multiple = ...which again requires {$stack_bottom}, completing the cycle
16+
17+
query_system_cycle_recursive_ty_alias = type aliases cannot be recursive
18+
query_system_cycle_recursive_ty_alias_help1 = consider using a struct, enum, or union instead to break the cycle
19+
query_system_cycle_recursive_ty_alias_help2 = see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information
20+
21+
query_system_cycle_recursive_trait_alias = trait aliases cannot be recursive
22+
23+
query_system_cycle_which_requires = ...which requires {$desc}...
24+
25+
query_system_query_overflow = queries overflow the depth limit!

compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ fluent_messages! {
5050
passes => "../locales/en-US/passes.ftl",
5151
plugin_impl => "../locales/en-US/plugin_impl.ftl",
5252
privacy => "../locales/en-US/privacy.ftl",
53+
query_system => "../locales/en-US/query_system.ftl",
5354
save_analysis => "../locales/en-US/save_analysis.ftl",
5455
ty_utils => "../locales/en-US/ty_utils.ftl",
5556
typeck => "../locales/en-US/typeck.ftl",

compiler/rustc_macros/src/diagnostics/subdiagnostic.rs

+249-445
Large diffs are not rendered by default.
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use rustc_errors::AddSubdiagnostic;
2+
use rustc_span::Span;
3+
4+
pub struct CycleStack {
5+
pub span: Span,
6+
pub desc: String,
7+
}
8+
9+
impl AddSubdiagnostic for CycleStack {
10+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
11+
diag.span_note(self.span, &format!("...which requires {}...", self.desc));
12+
}
13+
}
14+
15+
#[derive(SessionSubdiagnostic)]
16+
pub enum StackCount {
17+
#[note(query_system::cycle_stack_single)]
18+
Single,
19+
#[note(query_system::cycle_stack_multiple)]
20+
Multiple,
21+
}
22+
23+
#[derive(SessionSubdiagnostic)]
24+
pub enum Alias {
25+
#[note(query_system::cycle_recursive_ty_alias)]
26+
#[help(query_system::cycle_recursive_ty_alias_help1)]
27+
#[help(query_system::cycle_recursive_ty_alias_help2)]
28+
Ty,
29+
#[note(query_system::cycle_recursive_trait_alias)]
30+
Trait,
31+
}
32+
33+
#[derive(SessionSubdiagnostic)]
34+
#[note(query_system::cycle_usage)]
35+
pub struct CycleUsage {
36+
#[primary_span]
37+
pub span: Span,
38+
pub usage: String,
39+
}
40+
41+
#[derive(SessionDiagnostic)]
42+
#[diag(query_system::cycle, code = "E0391")]
43+
pub struct Cycle {
44+
#[primary_span]
45+
pub span: Span,
46+
pub stack_bottom: String,
47+
#[subdiagnostic]
48+
pub cycle_stack: Vec<CycleStack>,
49+
#[subdiagnostic]
50+
pub stack_count: StackCount,
51+
#[subdiagnostic]
52+
pub alias: Option<Alias>,
53+
#[subdiagnostic]
54+
pub cycle_usage: Option<CycleUsage>,
55+
}
56+
57+
#[derive(SessionDiagnostic)]
58+
#[diag(query_system::reentrant)]
59+
pub struct Reentrant;
60+
61+
#[derive(SessionDiagnostic)]
62+
#[diag(query_system::increment_compilation)]
63+
#[help]
64+
#[note(query_system::increment_compilation_note1)]
65+
#[note(query_system::increment_compilation_note2)]
66+
pub struct IncrementCompilation {
67+
pub run_cmd: String,
68+
pub dep_node: String,
69+
}
70+
71+
#[derive(SessionDiagnostic)]
72+
#[diag(query_system::query_overflow)]
73+
pub struct QueryOverflow;

compiler/rustc_query_system/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#![feature(min_specialization)]
66
#![feature(extern_types)]
77
#![allow(rustc::potential_query_instability)]
8+
// #![deny(rustc::untranslatable_diagnostic)]
9+
#![deny(rustc::diagnostic_outside_of_impl)]
810

911
#[macro_use]
1012
extern crate tracing;
@@ -15,5 +17,6 @@ extern crate rustc_macros;
1517

1618
pub mod cache;
1719
pub mod dep_graph;
20+
mod error;
1821
pub mod ich;
1922
pub mod query;

compiler/rustc_query_system/src/query/job.rs

+33-36
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
use crate::error::CycleStack;
12
use crate::query::plumbing::CycleError;
23
use crate::query::{QueryContext, QueryStackFrame};
3-
use rustc_hir::def::DefKind;
44

55
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_errors::{
7-
struct_span_err, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, Handler, Level,
8-
};
9-
use rustc_session::Session;
6+
use rustc_errors::{Diagnostic, DiagnosticBuilder, ErrorGuaranteed, Handler, Level};
7+
use rustc_hir::def::DefKind;
8+
use rustc_session::{Session, SessionDiagnostic};
109
use rustc_span::Span;
1110

1211
use std::hash::Hash;
@@ -536,46 +535,44 @@ pub(crate) fn report_cycle<'a>(
536535
assert!(!stack.is_empty());
537536

538537
let span = stack[0].query.default_span(stack[1 % stack.len()].span);
539-
let mut err =
540-
struct_span_err!(sess, span, E0391, "cycle detected when {}", stack[0].query.description);
538+
539+
let mut cycle_stack = Vec::new();
540+
541+
use crate::error::StackCount;
542+
let stack_count = if stack.len() == 1 { StackCount::Single } else { StackCount::Multiple };
541543

542544
for i in 1..stack.len() {
543545
let query = &stack[i].query;
544546
let span = query.default_span(stack[(i + 1) % stack.len()].span);
545-
err.span_note(span, &format!("...which requires {}...", query.description));
546-
}
547-
548-
if stack.len() == 1 {
549-
err.note(&format!("...which immediately requires {} again", stack[0].query.description));
550-
} else {
551-
err.note(&format!(
552-
"...which again requires {}, completing the cycle",
553-
stack[0].query.description
554-
));
555-
}
556-
557-
if stack.iter().all(|entry| {
558-
entry
559-
.query
560-
.def_kind
561-
.map_or(false, |def_kind| matches!(def_kind, DefKind::TyAlias | DefKind::TraitAlias))
562-
}) {
563-
if stack.iter().all(|entry| {
564-
entry.query.def_kind.map_or(false, |def_kind| matches!(def_kind, DefKind::TyAlias))
565-
}) {
566-
err.note("type aliases cannot be recursive");
567-
err.help("consider using a struct, enum, or union instead to break the cycle");
568-
err.help("see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information");
569-
} else {
570-
err.note("trait aliases cannot be recursive");
571-
}
547+
cycle_stack.push(CycleStack { span, desc: query.description.to_owned() });
572548
}
573549

550+
let mut cycle_usage = None;
574551
if let Some((span, query)) = usage {
575-
err.span_note(query.default_span(span), &format!("cycle used when {}", query.description));
552+
cycle_usage = Some(crate::error::CycleUsage {
553+
span: query.default_span(span),
554+
usage: query.description,
555+
});
576556
}
577557

578-
err
558+
let alias = if stack.iter().all(|entry| entry.query.def_kind == Some(DefKind::TyAlias)) {
559+
Some(crate::error::Alias::Ty)
560+
} else if stack.iter().all(|entry| entry.query.def_kind == Some(DefKind::TraitAlias)) {
561+
Some(crate::error::Alias::Trait)
562+
} else {
563+
None
564+
};
565+
566+
let cycle_diag = crate::error::Cycle {
567+
span,
568+
cycle_stack,
569+
stack_bottom: stack[0].query.description.to_owned(),
570+
alias,
571+
cycle_usage: cycle_usage,
572+
stack_count,
573+
};
574+
575+
cycle_diag.into_diagnostic(&sess.parse_sess)
579576
}
580577

581578
pub fn print_query_stack<CTX: QueryContext>(

compiler/rustc_query_system/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ pub trait QueryContext: HasDepContext {
125125
) -> R;
126126

127127
fn depth_limit_error(&self) {
128-
self.dep_context().sess().fatal("queries overflow the depth limit!");
128+
self.dep_context().sess().emit_fatal(crate::error::QueryOverflow);
129129
}
130130
}

compiler/rustc_query_system/src/query/plumbing.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -618,16 +618,12 @@ fn incremental_verify_ich_cold(sess: &Session, dep_node: DebugArg<'_>, result: D
618618
let old_in_panic = INSIDE_VERIFY_PANIC.with(|in_panic| in_panic.replace(true));
619619

620620
if old_in_panic {
621-
sess.struct_err(
622-
"internal compiler error: re-entrant incremental verify failure, suppressing message",
623-
)
624-
.emit();
621+
sess.emit_err(crate::error::Reentrant);
625622
} else {
626-
sess.struct_err(&format!("internal compiler error: encountered incremental compilation error with {:?}", dep_node))
627-
.help(&format!("This is a known issue with the compiler. Run {} to allow your project to compile", run_cmd))
628-
.note("Please follow the instructions below to create a bug report with the provided information")
629-
.note("See <https://github.com/rust-lang/rust/issues/84970> for more information")
630-
.emit();
623+
sess.emit_err(crate::error::IncrementCompilation {
624+
run_cmd,
625+
dep_node: format!("{:?}", dep_node),
626+
});
631627
panic!("Found unstable fingerprints for {:?}: {:?}", dep_node, result);
632628
}
633629

0 commit comments

Comments
 (0)