about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorYuanheng Li <520dhh@gmail.com>2022-08-21 21:37:05 +0800
committerLi Yuanheng <520dhh@gmail.com>2022-08-31 19:33:51 +0800
commitb8075e4550a3ec9110930b41e09e77266482e54f (patch)
tree7c00a0bc626ab45b7937332a03f2b5ece4f3e4e1 /compiler
parent12e4fd0755d7d976d4ee0f2004dc938290752ff7 (diff)
downloadrust-b8075e4550a3ec9110930b41e09e77266482e54f.tar.gz
rust-b8075e4550a3ec9110930b41e09e77266482e54f.zip
migrate rustc_query_system to use SessionDiagnostic
with manual impl SessionDiagnostic
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_error_messages/locales/en-US/query_system.ftl21
-rw-r--r--compiler/rustc_error_messages/src/lib.rs1
-rw-r--r--compiler/rustc_query_system/src/error.rs60
-rw-r--r--compiler/rustc_query_system/src/lib.rs3
-rw-r--r--compiler/rustc_query_system/src/query/job.rs55
-rw-r--r--compiler/rustc_query_system/src/query/plumbing.rs14
6 files changed, 108 insertions, 46 deletions
diff --git a/compiler/rustc_error_messages/locales/en-US/query_system.ftl b/compiler/rustc_error_messages/locales/en-US/query_system.ftl
new file mode 100644
index 00000000000..2d8c7a65122
--- /dev/null
+++ b/compiler/rustc_error_messages/locales/en-US/query_system.ftl
@@ -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
diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs
index d1ac326a72c..601fa4c72f0 100644
--- a/compiler/rustc_error_messages/src/lib.rs
+++ b/compiler/rustc_error_messages/src/lib.rs
@@ -52,6 +52,7 @@ fluent_messages! {
     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};
diff --git a/compiler/rustc_query_system/src/error.rs b/compiler/rustc_query_system/src/error.rs
new file mode 100644
index 00000000000..2ae6d10b828
--- /dev/null
+++ b/compiler/rustc_query_system/src/error.rs
@@ -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,
+}
diff --git a/compiler/rustc_query_system/src/lib.rs b/compiler/rustc_query_system/src/lib.rs
index 68284dcaa0b..da775893a2b 100644
--- a/compiler/rustc_query_system/src/lib.rs
+++ b/compiler/rustc_query_system/src/lib.rs
@@ -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 @@ extern crate rustc_macros;
 
 pub mod cache;
 pub mod dep_graph;
+mod error;
 pub mod ich;
 pub mod query;
diff --git a/compiler/rustc_query_system/src/query/job.rs b/compiler/rustc_query_system/src/query/job.rs
index 6d2aff38172..718ec14db36 100644
--- a/compiler/rustc_query_system/src/query/job.rs
+++ b/compiler/rustc_query_system/src/query/job.rs
@@ -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));
-    }
-
-    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
-            .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");
-        }
+        cycle_diag.upper_stack_info.push((span, query.description.to_owned()));
     }
 
-    if let Some((span, query)) = usage {
-        err.span_note(query.default_span(span), &format!("cycle used when {}", 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;
     }
 
-    err
+    cycle_diag.into_diagnostic(&sess.parse_sess)
 }
 
 pub fn print_query_stack<CTX: QueryContext>(
diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs
index 6ae9147ff77..e97411b777b 100644
--- a/compiler/rustc_query_system/src/query/plumbing.rs
+++ b/compiler/rustc_query_system/src/query/plumbing.rs
@@ -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);
     }