|
| 1 | +//! Error Reporting for `impl` items that do not match the obligations from their `trait`. |
| 2 | +
|
| 3 | +use syntax_pos::Span; |
| 4 | +use crate::ty::Ty; |
| 5 | +use crate::infer::{ValuePairs, Subtype}; |
| 6 | +use crate::infer::error_reporting::nice_region_error::NiceRegionError; |
| 7 | +use crate::infer::lexical_region_resolve::RegionResolutionError; |
| 8 | +use crate::util::common::ErrorReported; |
| 9 | +use crate::traits::ObligationCauseCode::CompareImplMethodObligation; |
| 10 | + |
| 11 | +impl<'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, |
| 23 | + ) = error.clone() { |
| 24 | + match (&sup_origin, &sub_origin) { |
| 25 | + (&Subtype(ref sup_trace), &Subtype(ref sub_trace)) => { |
| 26 | + if let ( |
| 27 | + ValuePairs::Types(sub_expected_found), |
| 28 | + ValuePairs::Types(sup_expected_found), |
| 29 | + CompareImplMethodObligation { trait_item_def_id, .. }, |
| 30 | + ) = (&sub_trace.values, &sup_trace.values, &sub_trace.cause.code) { |
| 31 | + if sup_expected_found == sub_expected_found { |
| 32 | + self.emit_err( |
| 33 | + var_origin.span(), |
| 34 | + sub_expected_found.expected, |
| 35 | + sub_expected_found.found, |
| 36 | + self.tcx().def_span(*trait_item_def_id), |
| 37 | + ); |
| 38 | + return Some(ErrorReported); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + _ => {} |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + None |
| 47 | + } |
| 48 | + |
| 49 | + fn emit_err(&self, sp: Span, expected: Ty<'tcx>, found: Ty<'tcx>, impl_sp: Span) { |
| 50 | + let mut err = self.tcx().sess.struct_span_err( |
| 51 | + sp, |
| 52 | + "`impl` item signature doesn't match `trait` item signature", |
| 53 | + ); |
| 54 | + err.note(&format!("expected `{:?}`\n found `{:?}`", expected, found)); |
| 55 | + err.span_label(sp, &format!("found {:?}", found)); |
| 56 | + err.span_label(impl_sp, &format!("expected {:?}", expected)); |
| 57 | + err.emit(); |
| 58 | + } |
| 59 | +} |
0 commit comments