]> git.proxmox.com Git - rustc.git/blame - src/librustc/infer/error_reporting/nice_region_error/trait_impl_difference.rs
New upstream version 1.42.0+dfsg1
[rustc.git] / src / librustc / 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;
dfeec247
XL
7use crate::ty::Ty;
8use crate::util::common::ErrorReported;
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 {
e74abb32
XL
25 match (&sup_origin, &sub_origin) {
26 (&Subtype(ref sup_trace), &Subtype(ref sub_trace)) => {
27 if let (
28 ValuePairs::Types(sub_expected_found),
29 ValuePairs::Types(sup_expected_found),
30 CompareImplMethodObligation { trait_item_def_id, .. },
dfeec247
XL
31 ) = (&sub_trace.values, &sup_trace.values, &sub_trace.cause.code)
32 {
e74abb32
XL
33 if sup_expected_found == sub_expected_found {
34 self.emit_err(
35 var_origin.span(),
36 sub_expected_found.expected,
37 sub_expected_found.found,
38 self.tcx().def_span(*trait_item_def_id),
39 );
40 return Some(ErrorReported);
41 }
42 }
43 }
44 _ => {}
45 }
46 }
47 }
48 None
49 }
50
51 fn emit_err(&self, sp: Span, expected: Ty<'tcx>, found: Ty<'tcx>, impl_sp: Span) {
dfeec247
XL
52 let mut err = self
53 .tcx()
54 .sess
55 .struct_span_err(sp, "`impl` item signature doesn't match `trait` item signature");
e74abb32
XL
56 err.note(&format!("expected `{:?}`\n found `{:?}`", expected, found));
57 err.span_label(sp, &format!("found {:?}", found));
58 err.span_label(impl_sp, &format!("expected {:?}", expected));
59 err.emit();
60 }
61}