Skip to content

Commit 1e95b5c

Browse files
committed
Point at the trait item and tweak wording
1 parent 3e2bc19 commit 1e95b5c

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

src/librustc/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ use crate::infer::{ValuePairs, Subtype};
66
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
77
use crate::infer::lexical_region_resolve::RegionResolutionError;
88
use crate::util::common::ErrorReported;
9+
use crate::traits::ObligationCauseCode::CompareImplMethodObligation;
910

1011
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
1112
/// Print the error message for lifetime errors when the `impl` doesn't conform to the `trait`.
1213
pub(super) fn try_report_impl_not_conforming_to_trait(&self) -> Option<ErrorReported> {
1314
if let Some(ref error) = self.error {
15+
debug!("try_report_impl_not_conforming_to_trait {:?}", error);
1416
if let RegionResolutionError::SubSupConflict(
1517
_,
1618
var_origin,
@@ -27,10 +29,18 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2729
) = (&sub_trace.values, &sup_trace.values) {
2830
if sup_expected_found == sub_expected_found {
2931
let sp = var_origin.span();
32+
let impl_sp = if let CompareImplMethodObligation {
33+
trait_item_def_id, ..
34+
} = &sub_trace.cause.code {
35+
Some(self.tcx().def_span(*trait_item_def_id))
36+
} else {
37+
None
38+
};
3039
self.emit_err(
3140
sp,
3241
sub_expected_found.expected,
3342
sub_expected_found.found,
43+
impl_sp,
3444
);
3545
return Some(ErrorReported);
3646
}
@@ -43,14 +53,16 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
4353
None
4454
}
4555

46-
fn emit_err(&self, sp: Span, expected: Ty<'tcx>, found: Ty<'tcx>) {
56+
fn emit_err(&self, sp: Span, expected: Ty<'tcx>, found: Ty<'tcx>, impl_sp: Option<Span>) {
4757
let mut err = self.tcx().sess.struct_span_err(
4858
sp,
4959
"`impl` item signature doesn't match `trait` item signature",
5060
);
51-
err.note(&format!("expected: {:?}\n found: {:?}", expected, found));
61+
err.note(&format!("expected `{:?}`\n found `{:?}`", expected, found));
5262
err.span_label(sp, &format!("found {:?}", found));
53-
// FIXME: recover the `FnPtr`'s `HirId`/`Node` to point to it.
63+
if let Some(span) = impl_sp {
64+
err.span_label(span, &format!("expected {:?}", expected));
65+
}
5466
err.emit();
5567
}
5668
}

src/test/ui/in-band-lifetimes/mismatched_trait_impl-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ struct Struct;
66
impl Deref for Struct {
77
type Target = dyn Trait;
88
fn deref(&self) -> &dyn Trait {
9+
//~^ ERROR `impl` item signature doesn't match `trait` item signature
910
unimplemented!();
1011
}
1112
}
12-
//~^^^^ ERROR `impl` item signature doesn't match `trait` item signature
1313

1414
fn main() {}

src/test/ui/in-band-lifetimes/mismatched_trait_impl-2.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ error: `impl` item signature doesn't match `trait` item signature
33
|
44
LL | fn deref(&self) -> &dyn Trait {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&Struct) -> &dyn Trait
6+
|
7+
::: $SRC_DIR/libcore/ops/deref.rs:LL:COL
68
|
7-
= note: expected: fn(&Struct) -> &(dyn Trait + 'static)
8-
found: fn(&Struct) -> &dyn Trait
9+
LL | fn deref(&self) -> &Self::Target;
10+
| --------------------------------- expected fn(&Struct) -> &(dyn Trait + 'static)
11+
|
12+
= note: expected `fn(&Struct) -> &(dyn Trait + 'static)`
13+
found `fn(&Struct) -> &dyn Trait`
914

1015
error: aborting due to previous error
1116

12-
For more information about this error, try `rustc --explain E0495`.

src/test/ui/in-band-lifetimes/mismatched_trait_impl.stderr

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
error: `impl` item signature doesn't match `trait` item signature
22
--> $DIR/mismatched_trait_impl.rs:9:5
33
|
4+
LL | fn foo(&self, x: &'a u32, y: &u32) -> &'a u32;
5+
| ---------------------------------------------- expected fn(&i32, &'a u32, &u32) -> &'a u32
6+
...
47
LL | fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 {
58
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&i32, &u32, &u32) -> &u32
69
|
7-
= note: expected: fn(&i32, &'a u32, &u32) -> &'a u32
8-
found: fn(&i32, &u32, &u32) -> &u32
10+
= note: expected `fn(&i32, &'a u32, &u32) -> &'a u32`
11+
found `fn(&i32, &u32, &u32) -> &u32`
912

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

2023
error: aborting due to 2 previous errors
2124

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

1013
error: aborting due to previous error
1114

src/test/ui/reject-specialized-drops-8142.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ error: `impl` item signature doesn't match `trait` item signature
9595
LL | impl<'lw> Drop for W<'lw,'lw> { fn drop(&mut self) { } } // REJECT
9696
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found W<'_, '_>
9797
|
98-
= note: expected: W<'l1, 'l2>
99-
found: W<'_, '_>
98+
= note: expected `W<'l1, 'l2>`
99+
found `W<'_, '_>`
100100

101101
error: aborting due to 8 previous errors
102102

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

0 commit comments

Comments
 (0)