Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
893ab67
Custom lifetime error for `impl` item doesn't conform to `trait`
estebank Oct 3, 2019
3e2bc19
review comments
estebank Oct 4, 2019
1e95b5c
Point at the trait item and tweak wording
estebank Oct 15, 2019
e383b7c
Make error apply only to impl/trait mismatch
estebank Oct 15, 2019
3aba2d0
Fix NLL test
estebank Oct 15, 2019
76b8fd8
Add long error explanation for E0587
GuillaumeGomez Oct 18, 2019
42a805e
Update ui tests
GuillaumeGomez Oct 18, 2019
6661db0
Correct handling of type flags with `ConstValue::Placeholder`
varkor Oct 20, 2019
2af50a9
Improve the "try using a variant of the expected type" hint.
Patryk27 Oct 18, 2019
97b10b7
Fix a previously forgotten pretty-printing test after a change to the…
Patryk27 Oct 26, 2019
1c5d0be
Improve pretty-printing for compound qualified paths.
Patryk27 Oct 27, 2019
e2c450b
doc: mention `get(_mut)` in Vec
tesuji Oct 28, 2019
cc575a6
rustc: use IndexVec<DefIndex, T> instead of Vec<T>.
eddyb Oct 25, 2019
46a39a2
self-profiling: Record something more useful for crate metadata gener…
michaelwoerister Oct 28, 2019
05ff8db
Rollup merge of #65068 - estebank:trait-impl-lt-mismatch, r=nikomatsakis
JohnTitor Oct 28, 2019
7ba2aa8
Rollup merge of #65562 - Patryk27:master, r=estebank
JohnTitor Oct 28, 2019
84cb9af
Rollup merge of #65563 - GuillaumeGomez:long-err-explanation-E0587, r…
JohnTitor Oct 28, 2019
4867ce4
Rollup merge of #65643 - varkor:remove-free-regions-from-const-placeh…
JohnTitor Oct 28, 2019
e1c36fd
Rollup merge of #65825 - eddyb:def-index-vec, r=varkor
JohnTitor Oct 28, 2019
ea3548b
Rollup merge of #65887 - lzutao:doc-vec-get, r=rkruppe
JohnTitor Oct 28, 2019
6d092ac
Rollup merge of #65891 - michaelwoerister:sp-crate-metadata, r=wesley…
JohnTitor Oct 28, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Custom lifetime error for impl item doesn't conform to trait
  • Loading branch information
estebank committed Oct 15, 2019
commit 893ab67f288f00d67d04329d93e3f0653c5e8a16
2 changes: 2 additions & 0 deletions src/librustc/infer/error_reporting/nice_region_error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod named_anon_conflict;
mod placeholder_error;
mod outlives_closure;
mod static_impl_trait;
mod trait_impl_difference;
mod util;

impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
Expand Down Expand Up @@ -73,6 +74,7 @@ impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
.or_else(|| self.try_report_anon_anon_conflict())
.or_else(|| self.try_report_outlives_closure())
.or_else(|| self.try_report_static_impl_trait())
.or_else(|| self.try_report_impl_not_conforming_to_trait())
}

pub fn get_regions(&self) -> (Span, ty::Region<'tcx>, ty::Region<'tcx>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Error Reporting for `impl` items that do not match the obligations from their `trait`.

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;

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 {
if let RegionResolutionError::SubSupConflict(
_,
var_origin,
sub_origin,
_sub,
sup_origin,
_sup,
) = error.clone() {
match (&sup_origin, &sub_origin) {
(&Subtype(ref sup_trace), &Subtype(ref sub_trace)) => {
if let (
ValuePairs::Types(sub_expected_found),
ValuePairs::Types(sup_expected_found),
) = (&sub_trace.values, &sup_trace.values) {
if sup_expected_found == sub_expected_found {
let sp = var_origin.span();
let mut err = self.tcx().sess.struct_span_err(
sp,
"`impl` item doesn't match `trait` item"
);
err.note(&format!(
"expected: {:?}\n found: {:?}",
sub_expected_found.expected,
sub_expected_found.found,
));
err.span_label(sp, &format!(
"found {:?}",
sub_expected_found.found,
));
// FIXME: recover the `FnPtr`'s `HirId`/`Node` to point to it.
err.emit();
return Some(ErrorReported);
}
}
}
_ => {}
}
}
}
None
}
}
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 @@ -9,6 +9,6 @@ impl Deref for Struct {
unimplemented!();
}
}
//~^^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter
//~^^^^ ERROR `impl` item doesn't match `trait` item

fn main() {}
17 changes: 4 additions & 13 deletions src/test/ui/in-band-lifetimes/mismatched_trait_impl-2.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
error: `impl` item doesn't match `trait` item
--> $DIR/mismatched_trait_impl-2.rs:8:5
|
LL | fn deref(&self) -> &dyn Trait {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&Struct) -> &dyn Trait
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 8:5...
--> $DIR/mismatched_trait_impl-2.rs:8:5
|
LL | / fn deref(&self) -> &dyn Trait {
LL | | unimplemented!();
LL | | }
| |_____^
= note: ...but the lifetime must also be valid for the static lifetime...
= note: ...so that the method type is compatible with trait:
expected fn(&Struct) -> &(dyn Trait + 'static)
found fn(&Struct) -> &dyn Trait
= note: expected: fn(&Struct) -> &(dyn Trait + 'static)
found: fn(&Struct) -> &dyn Trait

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/in-band-lifetimes/mismatched_trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait Get {
}

impl Get for i32 {
fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 { //~ ERROR cannot infer
fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 { //~ ERROR `impl` item doesn't match `trait`
x //~ ERROR lifetime mismatch
}
}
Expand Down
21 changes: 4 additions & 17 deletions src/test/ui/in-band-lifetimes/mismatched_trait_impl.stderr
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'a in generic type due to conflicting requirements
error: `impl` item doesn't match `trait` item
--> $DIR/mismatched_trait_impl.rs:9:5
|
LL | fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&i32, &u32, &u32) -> &u32
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the method body at 9:5...
--> $DIR/mismatched_trait_impl.rs:9:5
|
LL | / fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 {
LL | | x
LL | | }
| |_____^
note: ...but the lifetime must also be valid for the lifetime `'a` as defined on the method body at 9:32...
--> $DIR/mismatched_trait_impl.rs:9:32
|
LL | fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 {
| ^^
= note: ...so that the method type is compatible with trait:
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 Down
12 changes: 12 additions & 0 deletions src/test/ui/lifetimes/lifetime-mismatch-between-trait-and-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait Foo {
fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32;
}

impl Foo for () {
fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 {
//~^ ERROR `impl` item doesn't match `trait` item
if x > y { x } else { y }
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: `impl` item doesn't match `trait` item
--> $DIR/lifetime-mismatch-between-trait-and-impl.rs:6:5
|
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

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/reject-specialized-drops-8142.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ impl<One> Drop for V<One,One> { fn drop(&mut self) { } } // REJECT
//~^ ERROR Implementations of Drop cannot be specialized

impl<'lw> Drop for W<'lw,'lw> { fn drop(&mut self) { } } // REJECT
//~^ ERROR cannot infer an appropriate lifetime
//~^ ERROR `impl` item doesn't match `trait` item

pub fn main() { }
19 changes: 4 additions & 15 deletions src/test/ui/reject-specialized-drops-8142.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,14 @@ note: Use same sequence of generic type and region parameters that is on the str
LL | struct V<Tva, Tvb> { x: *const Tva, y: *const Tvb }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'lw` due to conflicting requirements
error: `impl` item doesn't match `trait` item
--> $DIR/reject-specialized-drops-8142.rs:54:1
|
LL | impl<'lw> Drop for W<'lw,'lw> { fn drop(&mut self) { } } // REJECT
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'l1` as defined on the struct at 17:10...
--> $DIR/reject-specialized-drops-8142.rs:17:10
|
LL | struct W<'l1, 'l2> { x: &'l1 i8, y: &'l2 u8 }
| ^^^
note: ...but the lifetime must also be valid for the lifetime `'l2` as defined on the struct at 17:15...
--> $DIR/reject-specialized-drops-8142.rs:17:15
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found W<'_, '_>
|
LL | struct W<'l1, 'l2> { x: &'l1 i8, y: &'l2 u8 }
| ^^^
= note: ...so that the types are compatible:
expected W<'l1, 'l2>
found W<'_, '_>
= note: expected: W<'l1, 'l2>
found: W<'_, '_>

error: aborting due to 8 previous errors

Expand Down