Skip to content

Commit b8075e4

Browse files
committed
migrate rustc_query_system to use SessionDiagnostic
with manual impl SessionDiagnostic
1 parent 12e4fd0 commit b8075e4

File tree

6 files changed

+108
-46
lines changed

6 files changed

+108
-46
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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

compiler/rustc_error_messages/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fluent_messages! {
5252
ty_utils => "../locales/en-US/ty_utils.ftl",
5353
typeck => "../locales/en-US/typeck.ftl",
5454
mir_dataflow => "../locales/en-US/mir_dataflow.ftl",
55+
query_system => "../locales/en-US/query_system.ftl",
5556
}
5657

5758
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
2+
use rustc_session::SessionDiagnostic;
3+
use rustc_span::Span;
4+
5+
pub struct Cycle {
6+
pub span: Span,
7+
pub stack_bottom: String,
8+
pub upper_stack_info: Vec<(Span, String)>,
9+
pub recursive_ty_alias: bool,
10+
pub recursive_trait_alias: bool,
11+
pub cycle_usage: Option<(Span, String)>,
12+
}
13+
14+
impl SessionDiagnostic<'_> for Cycle {
15+
fn into_diagnostic(
16+
self,
17+
sess: &'_ rustc_session::parse::ParseSess,
18+
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
19+
let mut diag = sess.struct_err(rustc_errors::fluent::query_system::cycle);
20+
diag.set_span(self.span);
21+
diag.code(rustc_errors::DiagnosticId::Error("E0391".to_string()));
22+
let upper_stack_len = self.upper_stack_info.len();
23+
for (span, desc) in self.upper_stack_info.into_iter() {
24+
// FIXME: use fluent translation
25+
diag.span_note(span, &format!("...which requires {}...", desc));
26+
}
27+
diag.set_arg("stack_bottom", self.stack_bottom);
28+
if upper_stack_len == 0 {
29+
diag.note(rustc_errors::fluent::query_system::cycle_stack_single);
30+
} else {
31+
diag.note(rustc_errors::fluent::query_system::cycle_stack_multiple);
32+
}
33+
if self.recursive_trait_alias {
34+
diag.note(rustc_errors::fluent::query_system::cycle_recursive_trait_alias);
35+
} else if self.recursive_ty_alias {
36+
diag.note(rustc_errors::fluent::query_system::cycle_recursive_ty_alias);
37+
diag.help(rustc_errors::fluent::query_system::cycle_recursive_ty_alias_help1);
38+
diag.help(rustc_errors::fluent::query_system::cycle_recursive_ty_alias_help2);
39+
}
40+
if let Some((span, desc)) = self.cycle_usage {
41+
diag.set_arg("usage", desc);
42+
diag.span_note(span, rustc_errors::fluent::query_system::cycle_usage);
43+
}
44+
diag
45+
}
46+
}
47+
48+
#[derive(SessionDiagnostic)]
49+
#[diag(query_system::reentrant)]
50+
pub struct Reentrant;
51+
52+
#[derive(SessionDiagnostic)]
53+
#[diag(query_system::increment_compilation)]
54+
#[help]
55+
#[note(query_system::increment_compilation_note1)]
56+
#[note(query_system::increment_compilation_note2)]
57+
pub struct IncrementCompilation {
58+
pub run_cmd: String,
59+
pub dep_node: String,
60+
}

compiler/rustc_query_system/src/lib.rs

Lines changed: 3 additions & 0 deletions
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

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use crate::query::plumbing::CycleError;
22
use crate::query::{QueryContext, QueryStackFrame};
3-
use rustc_hir::def::DefKind;
43

54
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;
5+
use rustc_errors::{Diagnostic, DiagnosticBuilder, ErrorGuaranteed, Handler, Level};
6+
use rustc_hir::def::DefKind;
7+
use rustc_session::{Session, SessionDiagnostic};
108
use rustc_span::Span;
119

1210
use std::hash::Hash;
@@ -536,46 +534,29 @@ pub(crate) fn report_cycle<'a>(
536534
assert!(!stack.is_empty());
537535

538536
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);
537+
538+
let mut cycle_diag = crate::error::Cycle {
539+
span,
540+
upper_stack_info: Vec::with_capacity(stack.len() - 1),
541+
stack_bottom: stack[0].query.description.to_owned(),
542+
recursive_ty_alias: false,
543+
recursive_trait_alias: false,
544+
cycle_usage: usage.map(|(span, query)| (query.default_span(span), query.description)),
545+
};
541546

542547
for i in 1..stack.len() {
543548
let query = &stack[i].query;
544549
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-
}
550+
cycle_diag.upper_stack_info.push((span, query.description.to_owned()));
572551
}
573552

574-
if let Some((span, query)) = usage {
575-
err.span_note(query.default_span(span), &format!("cycle used when {}", query.description));
553+
if stack.iter().all(|entry| entry.query.def_kind == Some(DefKind::TyAlias)) {
554+
cycle_diag.recursive_ty_alias = true;
555+
} else if stack.iter().all(|entry| entry.query.def_kind == Some(DefKind::TraitAlias)) {
556+
cycle_diag.recursive_trait_alias = true;
576557
}
577558

578-
err
559+
cycle_diag.into_diagnostic(&sess.parse_sess)
579560
}
580561

581562
pub fn print_query_stack<CTX: QueryContext>(

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 5 additions & 9 deletions
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)