Skip to content

Commit

Permalink
Point at the trait item and tweak wording
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 15, 2019
1 parent 3e2bc19 commit 1e95b5c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use crate::infer::{ValuePairs, Subtype};
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use crate::infer::lexical_region_resolve::RegionResolutionError;
use crate::util::common::ErrorReported;
use crate::traits::ObligationCauseCode::CompareImplMethodObligation;

impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// Print the error message for lifetime errors when the `impl` doesn't conform to the `trait`.
pub(super) fn try_report_impl_not_conforming_to_trait(&self) -> Option<ErrorReported> {
if let Some(ref error) = self.error {
debug!("try_report_impl_not_conforming_to_trait {:?}", error);
if let RegionResolutionError::SubSupConflict(
_,
var_origin,
Expand All @@ -27,10 +29,18 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
) = (&sub_trace.values, &sup_trace.values) {
if sup_expected_found == sub_expected_found {
let sp = var_origin.span();
let impl_sp = if let CompareImplMethodObligation {
trait_item_def_id, ..
} = &sub_trace.cause.code {
Some(self.tcx().def_span(*trait_item_def_id))
} else {
None
};
self.emit_err(
sp,
sub_expected_found.expected,
sub_expected_found.found,
impl_sp,
);
return Some(ErrorReported);
}
Expand All @@ -43,14 +53,16 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
None
}

fn emit_err(&self, sp: Span, expected: Ty<'tcx>, found: Ty<'tcx>) {
fn emit_err(&self, sp: Span, expected: Ty<'tcx>, found: Ty<'tcx>, impl_sp: Option<Span>) {
let mut err = self.tcx().sess.struct_span_err(
sp,
"`impl` item signature doesn't match `trait` item signature",
);
err.note(&format!("expected: {:?}\n found: {:?}", expected, found));
err.note(&format!("expected `{:?}`\n found `{:?}`", expected, found));
err.span_label(sp, &format!("found {:?}", found));
// FIXME: recover the `FnPtr`'s `HirId`/`Node` to point to it.
if let Some(span) = impl_sp {
err.span_label(span, &format!("expected {:?}", expected));
}
err.emit();
}
}
2 changes: 1 addition & 1 deletion src/test/ui/in-band-lifetimes/mismatched_trait_impl-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ struct Struct;
impl Deref for Struct {
type Target = dyn Trait;
fn deref(&self) -> &dyn Trait {
//~^ ERROR `impl` item signature doesn't match `trait` item signature
unimplemented!();
}
}
//~^^^^ ERROR `impl` item signature doesn't match `trait` item signature

fn main() {}
10 changes: 7 additions & 3 deletions src/test/ui/in-band-lifetimes/mismatched_trait_impl-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ error: `impl` item signature doesn't match `trait` item signature
|
LL | fn deref(&self) -> &dyn Trait {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&Struct) -> &dyn Trait
|
::: $SRC_DIR/libcore/ops/deref.rs:LL:COL
|
= note: expected: fn(&Struct) -> &(dyn Trait + 'static)
found: fn(&Struct) -> &dyn Trait
LL | fn deref(&self) -> &Self::Target;
| --------------------------------- expected fn(&Struct) -> &(dyn Trait + 'static)
|
= note: expected `fn(&Struct) -> &(dyn Trait + 'static)`
found `fn(&Struct) -> &dyn Trait`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
8 changes: 5 additions & 3 deletions src/test/ui/in-band-lifetimes/mismatched_trait_impl.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
error: `impl` item signature doesn't match `trait` item signature
--> $DIR/mismatched_trait_impl.rs:9:5
|
LL | fn foo(&self, x: &'a u32, y: &u32) -> &'a u32;
| ---------------------------------------------- expected fn(&i32, &'a u32, &u32) -> &'a u32
...
LL | fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&i32, &u32, &u32) -> &u32
|
= note: expected: fn(&i32, &'a u32, &u32) -> &'a u32
found: fn(&i32, &u32, &u32) -> &u32
= note: expected `fn(&i32, &'a u32, &u32) -> &'a u32`
found `fn(&i32, &u32, &u32) -> &u32`

error[E0623]: lifetime mismatch
--> $DIR/mismatched_trait_impl.rs:10:9
Expand All @@ -19,4 +22,3 @@ LL | x

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
error: `impl` item signature doesn't match `trait` item signature
--> $DIR/lifetime-mismatch-between-trait-and-impl.rs:6:5
|
LL | fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32;
| ------------------------------------------- expected fn(&i32, &'a i32) -> &'a i32
...
LL | fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&i32, &i32) -> &i32
|
= note: expected: fn(&i32, &'a i32) -> &'a i32
found: fn(&i32, &i32) -> &i32
= note: expected `fn(&i32, &'a i32) -> &'a i32`
found `fn(&i32, &i32) -> &i32`

error: aborting due to previous error

6 changes: 3 additions & 3 deletions src/test/ui/reject-specialized-drops-8142.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ error: `impl` item signature doesn't match `trait` item signature
LL | impl<'lw> Drop for W<'lw,'lw> { fn drop(&mut self) { } } // REJECT
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found W<'_, '_>
|
= note: expected: W<'l1, 'l2>
found: W<'_, '_>
= note: expected `W<'l1, 'l2>`
found `W<'_, '_>`

error: aborting due to 8 previous errors

Some errors have detailed explanations: E0308, E0366, E0367, E0495.
Some errors have detailed explanations: E0308, E0366, E0367.
For more information about an error, try `rustc --explain E0308`.

0 comments on commit 1e95b5c

Please sign in to comment.