Migrate trait_impl_difference.rs

This commit is contained in:
Nikita Tomashevich 2022-09-13 20:11:42 +03:00
parent 2118ff401f
commit 3935a81d47
No known key found for this signature in database
GPG Key ID: B29791D4D878E345
3 changed files with 60 additions and 26 deletions

View File

@ -211,3 +211,13 @@ infer_trait_placeholder_mismatch = implementation of `{$trait_def_id}` is not ge
.label_satisfy = doesn't satisfy where-clause
.label_where = due to a where-clause on `{$def_id}`...
.label_dup = implementation of `{$trait_def_id}` is not general enough
infer_trait_impl_diff = `impl` item signature doesn't match `trait` item signature
.found = found `{$found}`
.expected = expected `{$expected}`
.expected_found = expected `{$expected}`
{" "}found `{$found}`
infer_tid_rel_help = verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output
infer_tid_consider_borriwing = consider borrowing this type parameter in the trait
infer_tid_param_help = the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`

View File

@ -581,3 +581,41 @@ pub struct TraitPlaceholderMismatch {
#[subdiagnostic]
pub actual_impl_expl_notes: Vec<ActualImplExplNotes>,
}
pub struct ConsiderBorrowingParamHelp {
pub spans: Vec<Span>,
}
impl AddSubdiagnostic for ConsiderBorrowingParamHelp {
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
let mut type_param_span: MultiSpan = self.spans.clone().into();
for &span in &self.spans {
type_param_span.push_span_label(span, fluent::infer::tid_consider_borriwing);
}
diag.span_help(type_param_span, fluent::infer::tid_param_help);
}
}
#[derive(SessionSubdiagnostic)]
#[help(infer::tid_rel_help)]
pub struct RelationshipHelp;
#[derive(SessionDiagnostic)]
#[diag(infer::trait_impl_diff)]
pub struct TraitImplDiff {
#[primary_span]
#[label(infer::found)]
pub sp: Span,
#[label(infer::expected)]
pub trait_sp: Span,
#[note(infer::expected_found)]
pub note: (),
#[subdiagnostic]
pub param_help: ConsiderBorrowingParamHelp,
#[subdiagnostic]
// Seems like subdiagnostics are always pushed to the end, so this one
// also has to be a subdiagnostic to maintain order.
pub rel_help: Option<RelationshipHelp>,
pub expected: String,
pub found: String,
}

View File

@ -1,10 +1,11 @@
//! Error Reporting for `impl` items that do not match the obligations from their `trait`.
use crate::errors::{ConsiderBorrowingParamHelp, RelationshipHelp, TraitImplDiff};
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use crate::infer::lexical_region_resolve::RegionResolutionError;
use crate::infer::Subtype;
use crate::traits::ObligationCauseCode::CompareImplItemObligation;
use rustc_errors::{ErrorGuaranteed, MultiSpan};
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::def_id::DefId;
@ -51,10 +52,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
trait_def_id: DefId,
) -> ErrorGuaranteed {
let trait_sp = self.tcx().def_span(trait_def_id);
let mut err = self
.tcx()
.sess
.struct_span_err(sp, "`impl` item signature doesn't match `trait` item signature");
// Mark all unnamed regions in the type with a number.
// This diagnostic is called in response to lifetime errors, so be informative.
@ -91,9 +88,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let found =
self.cx.extract_inference_diagnostics_data(found.into(), Some(found_highlight)).name;
err.span_label(sp, &format!("found `{}`", found));
err.span_label(trait_sp, &format!("expected `{}`", expected));
// Get the span of all the used type parameters in the method.
let assoc_item = self.tcx().associated_item(trait_def_id);
let mut visitor = TypeParamSpanVisitor { tcx: self.tcx(), types: vec![] };
@ -110,26 +104,18 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
}
_ => {}
}
let mut type_param_span: MultiSpan = visitor.types.to_vec().into();
for &span in &visitor.types {
type_param_span
.push_span_label(span, "consider borrowing this type parameter in the trait");
}
err.note(&format!("expected `{}`\n found `{}`", expected, found));
let diag = TraitImplDiff {
sp,
trait_sp,
note: (),
param_help: ConsiderBorrowingParamHelp { spans: visitor.types.to_vec() },
rel_help: visitor.types.is_empty().then_some(RelationshipHelp),
expected,
found,
};
err.span_help(
type_param_span,
"the lifetime requirements from the `impl` do not correspond to the requirements in \
the `trait`",
);
if visitor.types.is_empty() {
err.help(
"verify the lifetime relationships in the `trait` and `impl` between the `self` \
argument, the other inputs and its output",
);
}
err.emit()
self.tcx().sess.emit_err(diag)
}
}