diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index cfc697b521fc..f73409ce1be5 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -235,9 +235,6 @@ pub struct DiagInner { pub suggestions: Suggestions, pub args: DiagArgMap, - // This is used to store args and restore them after a subdiagnostic is rendered. - pub reserved_args: DiagArgMap, - /// This is not used for highlighting or rendering any error message. Rather, it can be used /// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of /// `span` if there is one. Otherwise, it is `DUMMY_SP`. @@ -268,7 +265,6 @@ pub fn new_with_messages(level: Level, messages: Vec<(DiagMessage, Style)>) -> S children: vec![], suggestions: Suggestions::Enabled(vec![]), args: Default::default(), - reserved_args: Default::default(), sort_span: DUMMY_SP, is_lint: None, long_ty_path: None, @@ -333,14 +329,6 @@ pub fn remove_arg(&mut self, name: &str) { self.args.swap_remove(name); } - pub fn store_args(&mut self) { - self.reserved_args = self.args.clone(); - } - - pub fn restore_args(&mut self) { - self.args = std::mem::take(&mut self.reserved_args); - } - pub fn emitted_at_sub_diag(&self) -> Subdiag { let track = format!("-Ztrack-diagnostics: created at {}", self.emitted_at); Subdiag { diff --git a/compiler/rustc_trait_selection/src/errors.rs b/compiler/rustc_trait_selection/src/errors.rs index 254a626ce221..e4bc7c93fa63 100644 --- a/compiler/rustc_trait_selection/src/errors.rs +++ b/compiler/rustc_trait_selection/src/errors.rs @@ -1,5 +1,6 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexSet}; use rustc_errors::codes::*; +use rustc_errors::formatting::DiagMessageAddArg; use rustc_errors::{ Applicability, Diag, DiagCtxtHandle, DiagMessage, DiagStyledString, Diagnostic, EmissionGuarantee, IntoDiagArg, Level, MultiSpan, Subdiagnostic, msg, @@ -450,28 +451,23 @@ fn add_to_diag(self, diag: &mut Diag<'_, G>) { requirement, expected_found: Some((expected, found)), } => { - // `RegionOriginNote` can appear multiple times on one diagnostic with different - // `requirement` values. Scope args per-note and eagerly translate to avoid - // cross-note arg collisions. - // See https://github.com/rust-lang/rust/issues/143872 for details. - diag.store_args(); - diag.arg("requirement", requirement); - let msg = diag.eagerly_format(msg!( + let msg = msg!( "...so that the {$requirement -> - [method_compat] method type is compatible with trait - [type_compat] associated type is compatible with trait - [const_compat] const is compatible with trait - [expr_assignable] expression is assignable - [if_else_different] `if` and `else` have incompatible types - [no_else] `if` missing an `else` returns `()` - [fn_main_correct_type] `main` function has the correct type - [fn_lang_correct_type] lang item function has the correct type - [intrinsic_correct_type] intrinsic has the correct type - [method_correct_type] method receiver has the correct type - *[other] types are compatible - }" - )); - diag.restore_args(); + [method_compat] method type is compatible with trait + [type_compat] associated type is compatible with trait + [const_compat] const is compatible with trait + [expr_assignable] expression is assignable + [if_else_different] `if` and `else` have incompatible types + [no_else] `if` missing an `else` returns `()` + [fn_main_correct_type] `main` function has the correct type + [fn_lang_correct_type] lang item function has the correct type + [intrinsic_correct_type] intrinsic has the correct type + [method_correct_type] method receiver has the correct type + *[other] types are compatible + }" + ) + .arg("requirement", requirement) + .format(); label_or_note(diag, span, msg); diag.note_expected_found("", expected, "", found); @@ -480,9 +476,7 @@ fn add_to_diag(self, diag: &mut Diag<'_, G>) { // FIXME: this really should be handled at some earlier stage. Our // handling of region checking when type errors are present is // *terrible*. - diag.store_args(); - diag.arg("requirement", requirement); - let msg = diag.eagerly_format(msg!( + let msg = msg!( "...so that {$requirement -> [method_compat] method type is compatible with trait [type_compat] associated type is compatible with trait @@ -496,8 +490,9 @@ fn add_to_diag(self, diag: &mut Diag<'_, G>) { [method_correct_type] method receiver has the correct type *[other] types are compatible }" - )); - diag.restore_args(); + ) + .arg("requirement", requirement) + .format(); label_or_note(diag, span, msg); } }; diff --git a/compiler/rustc_trait_selection/src/errors/note_and_explain.rs b/compiler/rustc_trait_selection/src/errors/note_and_explain.rs index 5121ca886c1b..fd943bff3700 100644 --- a/compiler/rustc_trait_selection/src/errors/note_and_explain.rs +++ b/compiler/rustc_trait_selection/src/errors/note_and_explain.rs @@ -1,3 +1,4 @@ +use rustc_errors::formatting::DiagMessageAddArg; use rustc_errors::{Diag, EmissionGuarantee, IntoDiagArg, Subdiagnostic, msg}; use rustc_hir::def_id::LocalDefId; use rustc_middle::bug; @@ -163,13 +164,7 @@ pub fn new<'tcx>( impl Subdiagnostic for RegionExplanation<'_> { fn add_to_diag(self, diag: &mut Diag<'_, G>) { - diag.store_args(); - diag.arg("pref_kind", self.prefix); - diag.arg("suff_kind", self.suffix); - diag.arg("desc_kind", self.desc.kind); - diag.arg("desc_arg", self.desc.arg); - - let msg = diag.eagerly_format(msg!( + let msg = msg!( "{$pref_kind -> *[should_not_happen] [{$pref_kind}] [ref_valid_for] ...the reference is valid for @@ -202,8 +197,14 @@ fn add_to_diag(self, diag: &mut Diag<'_, G>) { [continues] ... [req_by_binding] {\" \"}as required by this binding }" - )); - diag.restore_args(); + ) + .arg("pref_kind", self.prefix) + .arg("suff_kind", self.suffix) + .arg("desc_kind", self.desc.kind) + .arg("desc_arg", self.desc.arg) + .format(); + + // diag.restore_args(); if let Some(span) = self.desc.span { diag.span_note(span, msg); } else {