]> git.proxmox.com Git - rustc.git/blame - src/librustc_infer/infer/error_reporting/nice_region_error/trait_impl_difference.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_infer / infer / error_reporting / nice_region_error / trait_impl_difference.rs
CommitLineData
e74abb32
XL
1//! Error Reporting for `impl` items that do not match the obligations from their `trait`.
2
e74abb32
XL
3use crate::infer::error_reporting::nice_region_error::NiceRegionError;
4use crate::infer::lexical_region_resolve::RegionResolutionError;
dfeec247 5use crate::infer::{Subtype, ValuePairs};
e74abb32 6use crate::traits::ObligationCauseCode::CompareImplMethodObligation;
ba9703b0
XL
7use rustc_errors::ErrorReported;
8use rustc_middle::ty::Ty;
dfeec247 9use rustc_span::Span;
e74abb32
XL
10
11impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
12 /// Print the error message for lifetime errors when the `impl` doesn't conform to the `trait`.
13 pub(super) fn try_report_impl_not_conforming_to_trait(&self) -> Option<ErrorReported> {
14 if let Some(ref error) = self.error {
15 debug!("try_report_impl_not_conforming_to_trait {:?}", error);
16 if let RegionResolutionError::SubSupConflict(
17 _,
18 var_origin,
19 sub_origin,
20 _sub,
21 sup_origin,
22 _sup,
dfeec247
XL
23 ) = error.clone()
24 {
ba9703b0
XL
25 if let (&Subtype(ref sup_trace), &Subtype(ref sub_trace)) =
26 (&sup_origin, &sub_origin)
27 {
28 if let (
29 ValuePairs::Types(sub_expected_found),
30 ValuePairs::Types(sup_expected_found),
31 CompareImplMethodObligation { trait_item_def_id, .. },
32 ) = (&sub_trace.values, &sup_trace.values, &sub_trace.cause.code)
33 {
34 if sup_expected_found == sub_expected_found {
35 self.emit_err(
36 var_origin.span(),
37 sub_expected_found.expected,
38 sub_expected_found.found,
39 self.tcx().def_span(*trait_item_def_id),
40 );
41 return Some(ErrorReported);
e74abb32
XL
42 }
43 }
e74abb32
XL
44 }
45 }
46 }
47 None
48 }
49
50 fn emit_err(&self, sp: Span, expected: Ty<'tcx>, found: Ty<'tcx>, impl_sp: Span) {
dfeec247
XL
51 let mut err = self
52 .tcx()
53 .sess
54 .struct_span_err(sp, "`impl` item signature doesn't match `trait` item signature");
e74abb32
XL
55 err.note(&format!("expected `{:?}`\n found `{:?}`", expected, found));
56 err.span_label(sp, &format!("found {:?}", found));
57 err.span_label(impl_sp, &format!("expected {:?}", expected));
58 err.emit();
59 }
60}