about summary refs log tree commit diff
path: root/compiler/rustc_query_system/src
diff options
context:
space:
mode:
authorYuanheng Li <520dhh@gmail.com>2022-08-23 11:19:38 +0800
committerLi Yuanheng <520dhh@gmail.com>2022-08-31 19:43:12 +0800
commitac638c1f5fca36484506415319ab254ad522a692 (patch)
tree28ee7295428515b4a854470e0cd9cc3dccd2be53 /compiler/rustc_query_system/src
parentd7e07c0b6b08e898a0720bf9c2fd2a22404bdfec (diff)
downloadrust-ac638c1f5fca36484506415319ab254ad522a692.tar.gz
rust-ac638c1f5fca36484506415319ab254ad522a692.zip
use derive proc macro to impl SessionDiagnostic
fixes `SessionSubdiagnostic` to accept multiple attributes
emitting list of fluent message remains unresolved
Diffstat (limited to 'compiler/rustc_query_system/src')
-rw-r--r--compiler/rustc_query_system/src/error.rs87
-rw-r--r--compiler/rustc_query_system/src/lib.rs2
-rw-r--r--compiler/rustc_query_system/src/query/job.rs43
3 files changed, 79 insertions, 53 deletions
diff --git a/compiler/rustc_query_system/src/error.rs b/compiler/rustc_query_system/src/error.rs
index 515a022e9d9..9b808afeef6 100644
--- a/compiler/rustc_query_system/src/error.rs
+++ b/compiler/rustc_query_system/src/error.rs
@@ -1,50 +1,59 @@
-use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
-use rustc_session::SessionDiagnostic;
+use rustc_errors::AddSubdiagnostic;
 use rustc_span::Span;
 
-pub struct Cycle {
+pub struct CycleStack {
     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)>,
+    pub desc: 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(#100717): 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
+impl AddSubdiagnostic for CycleStack {
+    fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
+        diag.span_note(self.span, &format!("...which requires {}...", self.desc));
     }
 }
 
+#[derive(SessionSubdiagnostic)]
+pub enum StackCount {
+    #[note(query_system::cycle_stack_single)]
+    Single,
+    #[note(query_system::cycle_stack_multiple)]
+    Multiple,
+}
+
+#[derive(SessionSubdiagnostic)]
+pub 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)]
+    Ty,
+    #[note(query_system::cycle_recursive_trait_alias)]
+    Trait,
+}
+
+#[derive(SessionSubdiagnostic)]
+#[note(query_system::cycle_usage)]
+pub struct CycleUsage {
+    #[primary_span]
+    pub span: Span,
+    pub usage: String,
+}
+
+#[derive(SessionDiagnostic)]
+#[diag(query_system::cycle, code = "E0391")]
+pub struct Cycle {
+    #[primary_span]
+    pub span: Span,
+    pub stack_bottom: String,
+    #[subdiagnostic]
+    pub cycle_stack: Vec<CycleStack>,
+    #[subdiagnostic]
+    pub stack_count: StackCount,
+    #[subdiagnostic]
+    pub alias: Option<Alias>,
+    #[subdiagnostic]
+    pub cycle_usage: Option<CycleUsage>,
+}
+
 #[derive(SessionDiagnostic)]
 #[diag(query_system::reentrant)]
 pub struct Reentrant;
diff --git a/compiler/rustc_query_system/src/lib.rs b/compiler/rustc_query_system/src/lib.rs
index da775893a2b..7067bc5f09c 100644
--- a/compiler/rustc_query_system/src/lib.rs
+++ b/compiler/rustc_query_system/src/lib.rs
@@ -5,7 +5,7 @@
 #![feature(min_specialization)]
 #![feature(extern_types)]
 #![allow(rustc::potential_query_instability)]
-#![deny(rustc::untranslatable_diagnostic)]
+// #![deny(rustc::untranslatable_diagnostic)]
 #![deny(rustc::diagnostic_outside_of_impl)]
 
 #[macro_use]
diff --git a/compiler/rustc_query_system/src/query/job.rs b/compiler/rustc_query_system/src/query/job.rs
index 718ec14db36..af1741bad6c 100644
--- a/compiler/rustc_query_system/src/query/job.rs
+++ b/compiler/rustc_query_system/src/query/job.rs
@@ -1,3 +1,4 @@
+use crate::error::CycleStack;
 use crate::query::plumbing::CycleError;
 use crate::query::{QueryContext, QueryStackFrame};
 
@@ -535,26 +536,42 @@ pub(crate) fn report_cycle<'a>(
 
     let span = stack[0].query.default_span(stack[1 % stack.len()].span);
 
-    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)),
-    };
+    let mut cycle_stack = Vec::new();
+
+    use crate::error::StackCount;
+    let stack_count = if stack.len() == 1 { StackCount::Single } else { StackCount::Multiple };
 
     for i in 1..stack.len() {
         let query = &stack[i].query;
         let span = query.default_span(stack[(i + 1) % stack.len()].span);
-        cycle_diag.upper_stack_info.push((span, query.description.to_owned()));
+        cycle_stack.push(CycleStack { span, desc: query.description.to_owned() });
     }
 
-    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;
+    let mut cycle_usage = None;
+    if let Some((span, query)) = usage {
+        cycle_usage = Some(crate::error::CycleUsage {
+            span: query.default_span(span),
+            usage: query.description,
+        });
     }
+    // let cycle_usage = usage.map(|(span, query)| query.default_span(span))
+
+    let alias = if stack.iter().all(|entry| entry.query.def_kind == Some(DefKind::TyAlias)) {
+        Some(crate::error::Alias::Ty)
+    } else if stack.iter().all(|entry| entry.query.def_kind == Some(DefKind::TraitAlias)) {
+        Some(crate::error::Alias::Trait)
+    } else {
+        None
+    };
+
+    let cycle_diag = crate::error::Cycle {
+        span,
+        cycle_stack,
+        stack_bottom: stack[0].query.description.to_owned(),
+        alias,
+        cycle_usage: cycle_usage,
+        stack_count,
+    };
 
     cycle_diag.into_diagnostic(&sess.parse_sess)
 }