diff --git a/compiler/rustc_trait_selection/src/errors.rs b/compiler/rustc_trait_selection/src/errors.rs index 8f353cae0beb..7a1485908818 100644 --- a/compiler/rustc_trait_selection/src/errors.rs +++ b/compiler/rustc_trait_selection/src/errors.rs @@ -477,7 +477,7 @@ pub enum RegionOriginNote<'a> { impl Subdiagnostic for RegionOriginNote<'_> { fn add_to_diag(self, diag: &mut Diag<'_, G>) { - let mut label_or_note = |span, msg: DiagMessage| { + let label_or_note = |diag: &mut Diag<'_, G>, span, msg: DiagMessage| { let sub_count = diag.children.iter().filter(|d| d.span.is_dummy()).count(); let expanded_sub_count = diag.children.iter().filter(|d| !d.span.is_dummy()).count(); let span_is_primary = diag.span.primary_spans().iter().all(|&sp| sp == span); @@ -491,22 +491,26 @@ fn add_to_diag(self, diag: &mut Diag<'_, G>) { }; match self { RegionOriginNote::Plain { span, msg } => { - label_or_note(span, msg); + label_or_note(diag, span, msg); } RegionOriginNote::WithName { span, msg, name, continues } => { - label_or_note(span, msg); diag.arg("name", name); diag.arg("continues", continues); + label_or_note(diag, span, msg); } RegionOriginNote::WithRequirement { span, requirement, expected_found: Some((expected, found)), } => { - label_or_note( - span, - msg!( - "...so that the {$requirement -> + // `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_translate(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 @@ -519,9 +523,9 @@ fn add_to_diag(self, diag: &mut Diag<'_, G>) { [method_correct_type] method receiver has the correct type *[other] types are compatible }" - ), - ); - diag.arg("requirement", requirement); + )); + diag.restore_args(); + label_or_note(diag, span, msg); diag.note_expected_found("", expected, "", found); } @@ -529,10 +533,10 @@ 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*. - label_or_note( - span, - msg!( - "...so that {$requirement -> + diag.store_args(); + diag.arg("requirement", requirement); + let msg = diag.eagerly_translate(msg!( + "...so that {$requirement -> [method_compat] method type is compatible with trait [type_compat] associated type is compatible with trait [const_compat] const is compatible with trait @@ -545,9 +549,9 @@ fn add_to_diag(self, diag: &mut Diag<'_, G>) { [method_correct_type] method receiver has the correct type *[other] types are compatible }" - ), - ); - diag.arg("requirement", requirement); + )); + diag.restore_args(); + label_or_note(diag, span, msg); } }; }