avoid duplicate requirement diag args in RegionOriginNote

This commit is contained in:
Takayuki Maeda
2026-02-25 01:06:07 +09:00
parent fe7dc48fc8
commit 3f76a0db94
+21 -17
View File
@@ -477,7 +477,7 @@ pub enum RegionOriginNote<'a> {
impl Subdiagnostic for RegionOriginNote<'_> {
fn add_to_diag<G: EmissionGuarantee>(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<G: EmissionGuarantee>(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<G: EmissionGuarantee>(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<G: EmissionGuarantee>(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<G: EmissionGuarantee>(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);
}
};
}