From dca52ac835f8c87dfc3167606d1146ce6b9bba11 Mon Sep 17 00:00:00 2001 From: Tshepang Mbambo Date: Fri, 24 Feb 2023 23:37:10 +0200 Subject: [PATCH] make "proc macro panicked" translatable --- compiler/rustc_expand/locales/en-US.ftl | 4 ++++ compiler/rustc_expand/src/errors.rs | 15 +++++++++++++++ compiler/rustc_expand/src/proc_macro.rs | 12 +++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_expand/locales/en-US.ftl b/compiler/rustc_expand/locales/en-US.ftl index dbd80954382db..b475d285f6b7e 100644 --- a/compiler/rustc_expand/locales/en-US.ftl +++ b/compiler/rustc_expand/locales/en-US.ftl @@ -129,3 +129,7 @@ expand_module_multiple_candidates = .help = delete or rename one of them to remove the ambiguity expand_trace_macro = trace_macro + +expand_proc_macro_panicked = + proc macro panicked + .help = message: {$message} diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index d9b2b5f4802c8..70ab222b48470 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -375,3 +375,18 @@ pub struct TraceMacro { #[primary_span] pub span: Span, } + +#[derive(Diagnostic)] +#[diag(expand_proc_macro_panicked)] +pub(crate) struct ProcMacroPanicked { + #[primary_span] + pub span: Span, + #[subdiagnostic] + pub message: Option, +} + +#[derive(Subdiagnostic)] +#[help(expand_help)] +pub(crate) struct ProcMacroPanickedHelp { + pub message: String, +} diff --git a/compiler/rustc_expand/src/proc_macro.rs b/compiler/rustc_expand/src/proc_macro.rs index e9a6919206894..cef64a1047902 100644 --- a/compiler/rustc_expand/src/proc_macro.rs +++ b/compiler/rustc_expand/src/proc_macro.rs @@ -1,4 +1,5 @@ use crate::base::{self, *}; +use crate::errors; use crate::proc_macro_server; use rustc_ast as ast; @@ -60,11 +61,12 @@ impl base::BangProcMacro for BangProcMacro { let strategy = exec_strategy(ecx); let server = proc_macro_server::Rustc::new(ecx); self.client.run(&strategy, server, input, proc_macro_backtrace).map_err(|e| { - let mut err = ecx.struct_span_err(span, "proc macro panicked"); - if let Some(s) = e.as_str() { - err.help(&format!("message: {}", s)); - } - err.emit() + ecx.sess.emit_err(errors::ProcMacroPanicked { + span, + message: e + .as_str() + .map(|message| errors::ProcMacroPanickedHelp { message: message.into() }), + }) }) } }