migrate rustc_query_system to use SessionDiagnostic

with manual impl SessionDiagnostic
This commit is contained in:
Yuanheng Li 2022-08-21 21:37:05 +08:00 committed by Li Yuanheng
parent 12e4fd0755
commit b8075e4550
6 changed files with 108 additions and 46 deletions

View File

@ -0,0 +1,21 @@
query_system_reentrant = internal compiler error: re-entrant incremental verify failure, suppressing message
query_system_increment_compilation = internal compiler error: encountered incremental compilation error with {$dep_node}
.help = This is a known issue with the compiler. Run {$run_cmd} to allow your project to compile
query_system_increment_compilation_note1 = Please follow the instructions below to create a bug report with the provided information
query_system_increment_compilation_note2 = See <https://github.com/rust-lang/rust/issues/84970> for more information
query_system_cycle = cycle detected when {$stack_bottom}
query_system_cycle_usage = cycle used when {$usage}
query_system_cycle_stack_single = ...which immediately requires {$stack_bottom} again
query_system_cycle_stack_multiple = ...which again requires {$stack_bottom}, completing the cycle
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_recursive_trait_alias = trait aliases cannot be recursive

View File

@ -52,6 +52,7 @@
ty_utils => "../locales/en-US/ty_utils.ftl",
typeck => "../locales/en-US/typeck.ftl",
mir_dataflow => "../locales/en-US/mir_dataflow.ftl",
query_system => "../locales/en-US/query_system.ftl",
}
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};

View File

@ -0,0 +1,60 @@
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
use rustc_session::SessionDiagnostic;
use rustc_span::Span;
pub struct Cycle {
pub span: Span,
pub stack_bottom: String,
pub upper_stack_info: Vec<(Span, String)>,
pub recursive_ty_alias: bool,
pub recursive_trait_alias: bool,
pub cycle_usage: Option<(Span, String)>,
}
impl SessionDiagnostic<'_> for Cycle {
fn into_diagnostic(
self,
sess: &'_ rustc_session::parse::ParseSess,
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(rustc_errors::fluent::query_system::cycle);
diag.set_span(self.span);
diag.code(rustc_errors::DiagnosticId::Error("E0391".to_string()));
let upper_stack_len = self.upper_stack_info.len();
for (span, desc) in self.upper_stack_info.into_iter() {
// FIXME: use fluent translation
diag.span_note(span, &format!("...which requires {}...", desc));
}
diag.set_arg("stack_bottom", self.stack_bottom);
if upper_stack_len == 0 {
diag.note(rustc_errors::fluent::query_system::cycle_stack_single);
} else {
diag.note(rustc_errors::fluent::query_system::cycle_stack_multiple);
}
if self.recursive_trait_alias {
diag.note(rustc_errors::fluent::query_system::cycle_recursive_trait_alias);
} else if self.recursive_ty_alias {
diag.note(rustc_errors::fluent::query_system::cycle_recursive_ty_alias);
diag.help(rustc_errors::fluent::query_system::cycle_recursive_ty_alias_help1);
diag.help(rustc_errors::fluent::query_system::cycle_recursive_ty_alias_help2);
}
if let Some((span, desc)) = self.cycle_usage {
diag.set_arg("usage", desc);
diag.span_note(span, rustc_errors::fluent::query_system::cycle_usage);
}
diag
}
}
#[derive(SessionDiagnostic)]
#[diag(query_system::reentrant)]
pub struct Reentrant;
#[derive(SessionDiagnostic)]
#[diag(query_system::increment_compilation)]
#[help]
#[note(query_system::increment_compilation_note1)]
#[note(query_system::increment_compilation_note2)]
pub struct IncrementCompilation {
pub run_cmd: String,
pub dep_node: String,
}

View File

@ -5,6 +5,8 @@
#![feature(min_specialization)]
#![feature(extern_types)]
#![allow(rustc::potential_query_instability)]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
#[macro_use]
extern crate tracing;
@ -15,5 +17,6 @@
pub mod cache;
pub mod dep_graph;
mod error;
pub mod ich;
pub mod query;

View File

@ -1,12 +1,10 @@
use crate::query::plumbing::CycleError;
use crate::query::{QueryContext, QueryStackFrame};
use rustc_hir::def::DefKind;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{
struct_span_err, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, Handler, Level,
};
use rustc_session::Session;
use rustc_errors::{Diagnostic, DiagnosticBuilder, ErrorGuaranteed, Handler, Level};
use rustc_hir::def::DefKind;
use rustc_session::{Session, SessionDiagnostic};
use rustc_span::Span;
use std::hash::Hash;
@ -536,46 +534,29 @@ pub(crate) fn report_cycle<'a>(
assert!(!stack.is_empty());
let span = stack[0].query.default_span(stack[1 % stack.len()].span);
let mut err =
struct_span_err!(sess, span, E0391, "cycle detected when {}", stack[0].query.description);
let mut cycle_diag = crate::error::Cycle {
span,
upper_stack_info: Vec::with_capacity(stack.len() - 1),
stack_bottom: stack[0].query.description.to_owned(),
recursive_ty_alias: false,
recursive_trait_alias: false,
cycle_usage: usage.map(|(span, query)| (query.default_span(span), query.description)),
};
for i in 1..stack.len() {
let query = &stack[i].query;
let span = query.default_span(stack[(i + 1) % stack.len()].span);
err.span_note(span, &format!("...which requires {}...", query.description));
cycle_diag.upper_stack_info.push((span, query.description.to_owned()));
}
if stack.len() == 1 {
err.note(&format!("...which immediately requires {} again", stack[0].query.description));
} else {
err.note(&format!(
"...which again requires {}, completing the cycle",
stack[0].query.description
));
if stack.iter().all(|entry| entry.query.def_kind == Some(DefKind::TyAlias)) {
cycle_diag.recursive_ty_alias = true;
} else if stack.iter().all(|entry| entry.query.def_kind == Some(DefKind::TraitAlias)) {
cycle_diag.recursive_trait_alias = true;
}
if stack.iter().all(|entry| {
entry
.query
.def_kind
.map_or(false, |def_kind| matches!(def_kind, DefKind::TyAlias | DefKind::TraitAlias))
}) {
if stack.iter().all(|entry| {
entry.query.def_kind.map_or(false, |def_kind| matches!(def_kind, DefKind::TyAlias))
}) {
err.note("type aliases cannot be recursive");
err.help("consider using a struct, enum, or union instead to break the cycle");
err.help("see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information");
} else {
err.note("trait aliases cannot be recursive");
}
}
if let Some((span, query)) = usage {
err.span_note(query.default_span(span), &format!("cycle used when {}", query.description));
}
err
cycle_diag.into_diagnostic(&sess.parse_sess)
}
pub fn print_query_stack<CTX: QueryContext>(

View File

@ -618,16 +618,12 @@ fn incremental_verify_ich_cold(sess: &Session, dep_node: DebugArg<'_>, result: D
let old_in_panic = INSIDE_VERIFY_PANIC.with(|in_panic| in_panic.replace(true));
if old_in_panic {
sess.struct_err(
"internal compiler error: re-entrant incremental verify failure, suppressing message",
)
.emit();
sess.emit_err(crate::error::Reentrant);
} else {
sess.struct_err(&format!("internal compiler error: encountered incremental compilation error with {:?}", dep_node))
.help(&format!("This is a known issue with the compiler. Run {} to allow your project to compile", run_cmd))
.note("Please follow the instructions below to create a bug report with the provided information")
.note("See <https://github.com/rust-lang/rust/issues/84970> for more information")
.emit();
sess.emit_err(crate::error::IncrementCompilation {
run_cmd,
dep_node: format!("{:?}", dep_node),
});
panic!("Found unstable fingerprints for {:?}: {:?}", dep_node, result);
}