Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3935a81

Browse files
committed
Migrate trait_impl_difference.rs
1 parent 2118ff4 commit 3935a81

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

compiler/rustc_error_messages/locales/en-US/infer.ftl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,13 @@ infer_trait_placeholder_mismatch = implementation of `{$trait_def_id}` is not ge
211211
.label_satisfy = doesn't satisfy where-clause
212212
.label_where = due to a where-clause on `{$def_id}`...
213213
.label_dup = implementation of `{$trait_def_id}` is not general enough
214+
215+
infer_trait_impl_diff = `impl` item signature doesn't match `trait` item signature
216+
.found = found `{$found}`
217+
.expected = expected `{$expected}`
218+
.expected_found = expected `{$expected}`
219+
{" "}found `{$found}`
220+
221+
infer_tid_rel_help = verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output
222+
infer_tid_consider_borriwing = consider borrowing this type parameter in the trait
223+
infer_tid_param_help = the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,41 @@ pub struct TraitPlaceholderMismatch {
581581
#[subdiagnostic]
582582
pub actual_impl_expl_notes: Vec<ActualImplExplNotes>,
583583
}
584+
585+
pub struct ConsiderBorrowingParamHelp {
586+
pub spans: Vec<Span>,
587+
}
588+
589+
impl AddSubdiagnostic for ConsiderBorrowingParamHelp {
590+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
591+
let mut type_param_span: MultiSpan = self.spans.clone().into();
592+
for &span in &self.spans {
593+
type_param_span.push_span_label(span, fluent::infer::tid_consider_borriwing);
594+
}
595+
diag.span_help(type_param_span, fluent::infer::tid_param_help);
596+
}
597+
}
598+
599+
#[derive(SessionSubdiagnostic)]
600+
#[help(infer::tid_rel_help)]
601+
pub struct RelationshipHelp;
602+
603+
#[derive(SessionDiagnostic)]
604+
#[diag(infer::trait_impl_diff)]
605+
pub struct TraitImplDiff {
606+
#[primary_span]
607+
#[label(infer::found)]
608+
pub sp: Span,
609+
#[label(infer::expected)]
610+
pub trait_sp: Span,
611+
#[note(infer::expected_found)]
612+
pub note: (),
613+
#[subdiagnostic]
614+
pub param_help: ConsiderBorrowingParamHelp,
615+
#[subdiagnostic]
616+
// Seems like subdiagnostics are always pushed to the end, so this one
617+
// also has to be a subdiagnostic to maintain order.
618+
pub rel_help: Option<RelationshipHelp>,
619+
pub expected: String,
620+
pub found: String,
621+
}

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Error Reporting for `impl` items that do not match the obligations from their `trait`.
22
3+
use crate::errors::{ConsiderBorrowingParamHelp, RelationshipHelp, TraitImplDiff};
34
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
45
use crate::infer::lexical_region_resolve::RegionResolutionError;
56
use crate::infer::Subtype;
67
use crate::traits::ObligationCauseCode::CompareImplItemObligation;
7-
use rustc_errors::{ErrorGuaranteed, MultiSpan};
8+
use rustc_errors::ErrorGuaranteed;
89
use rustc_hir as hir;
910
use rustc_hir::def::Res;
1011
use rustc_hir::def_id::DefId;
@@ -51,10 +52,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5152
trait_def_id: DefId,
5253
) -> ErrorGuaranteed {
5354
let trait_sp = self.tcx().def_span(trait_def_id);
54-
let mut err = self
55-
.tcx()
56-
.sess
57-
.struct_span_err(sp, "`impl` item signature doesn't match `trait` item signature");
5855

5956
// Mark all unnamed regions in the type with a number.
6057
// This diagnostic is called in response to lifetime errors, so be informative.
@@ -91,9 +88,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
9188
let found =
9289
self.cx.extract_inference_diagnostics_data(found.into(), Some(found_highlight)).name;
9390

94-
err.span_label(sp, &format!("found `{}`", found));
95-
err.span_label(trait_sp, &format!("expected `{}`", expected));
96-
9791
// Get the span of all the used type parameters in the method.
9892
let assoc_item = self.tcx().associated_item(trait_def_id);
9993
let mut visitor = TypeParamSpanVisitor { tcx: self.tcx(), types: vec![] };
@@ -110,26 +104,18 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
110104
}
111105
_ => {}
112106
}
113-
let mut type_param_span: MultiSpan = visitor.types.to_vec().into();
114-
for &span in &visitor.types {
115-
type_param_span
116-
.push_span_label(span, "consider borrowing this type parameter in the trait");
117-
}
118107

119-
err.note(&format!("expected `{}`\n found `{}`", expected, found));
120-
121-
err.span_help(
122-
type_param_span,
123-
"the lifetime requirements from the `impl` do not correspond to the requirements in \
124-
the `trait`",
125-
);
126-
if visitor.types.is_empty() {
127-
err.help(
128-
"verify the lifetime relationships in the `trait` and `impl` between the `self` \
129-
argument, the other inputs and its output",
130-
);
131-
}
132-
err.emit()
108+
let diag = TraitImplDiff {
109+
sp,
110+
trait_sp,
111+
note: (),
112+
param_help: ConsiderBorrowingParamHelp { spans: visitor.types.to_vec() },
113+
rel_help: visitor.types.is_empty().then_some(RelationshipHelp),
114+
expected,
115+
found,
116+
};
117+
118+
self.tcx().sess.emit_err(diag)
133119
}
134120
}
135121

0 commit comments

Comments
 (0)