Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhelpful error messages for trait with unbound lifetime #72106

Closed
willir opened this issue May 11, 2020 · 3 comments · Fixed by #67460
Closed

Unhelpful error messages for trait with unbound lifetime #72106

willir opened this issue May 11, 2020 · 3 comments · Fixed by #67460
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@willir
Copy link

willir commented May 11, 2020

I was trying to implement FromSql of rusqlite for my reference type (with lifetime). And I faced an unclear error message, later I was explained that the trait does not allow the return type to borrow from the value argument since it doesn't specify lifetime parameter explicitly.

However, I thought that it worth filing a bug report about unhelpful error message anyway.

Here is a simplified code:

use std::error::Error;
use std::fmt;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ValueRef<'a> {
    Null,
    Integer(i64),
    Real(f64),
    Text(&'a [u8]),
    Blob(&'a [u8]),
}

impl<'a> ValueRef<'a> {
    pub fn as_str(&self) -> FromSqlResult<&'a str> {
        match *self {
            ValueRef::Text(t) => {
                std::str::from_utf8(t).map_err(|_| FromSqlError::InvalidType)
            }
            _ => Err(FromSqlError::InvalidType),
        }
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub enum FromSqlError {
    InvalidType
}

impl fmt::Display for FromSqlError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "InvalidType")
    }
}

impl Error for FromSqlError {}

pub type FromSqlResult<T> = Result<T, FromSqlError>;

pub trait FromSql: Sized {
    fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self>;
}

impl FromSql for &str {
    fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str> {
        value.as_str()
    }
}

pub fn main() {
    println!("{}", "Hello World");
}

The error message I'm getting is:

error: `impl` item signature doesn't match `trait` item signature
  --> src/main.rs:45:5
   |
41 |     fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self>;
   |     ------------------------------------------------------------- expected fn(ValueRef<'_>) -> std::result::Result<&str, FromSqlError>
...
45 |     fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(ValueRef<'_>) -> std::result::Result<&str, FromSqlError>
   |
   = note: expected `fn(ValueRef<'_>) -> std::result::Result<&str, FromSqlError>`
              found `fn(ValueRef<'_>) -> std::result::Result<&str, FromSqlError>`

error: aborting due to previous error

error: could not compile `playground`.

To learn more, run the command again with --verbose.

So basically it says that I have a wrong signature, even though the expected and found signatures are the same.

I'm not sure what would be the right error message to raise here since I don't know lifetime rules quite well.

Here is, just in case, a link to playground

The rust version I'm using is: 1.44.0-nightly, but it also reproduces on 1.43.1.

@willir willir added the C-bug Category: This is a bug. label May 11, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels May 11, 2020
@lcnr
Copy link
Contributor

lcnr commented May 11, 2020

Minimized:

pub trait Foo: Sized {
    fn foo(value: &str) -> Self;
}

impl Foo for &str {
    fn foo(value: &str) -> &str {
        value
    }
}

Error message:

error: `impl` item signature doesn't match `trait` item signature
 --> src/lib.rs:6:5
  |
2 |     fn foo(value: &str) -> Self;
  |     ---------------------------- expected fn(&str) -> &str
...
6 |     fn foo(value: &str) -> &str {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&str) -> &str
  |
  = note: expected `fn(&str) -> &str`
             found `fn(&str) -> &str`

@lcnr lcnr added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels May 11, 2020
@estebank
Copy link
Contributor

Duplicate of #66406. CC #67460.

@estebank
Copy link
Contributor

estebank commented May 28, 2020

Update, not a duplicate. The output with #67460 is:

error: `impl` item signature doesn't match `trait` item signature
  --> file8.rs:45:5
   |
41 |     fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self>;
   |     ------------------------------------------------------------- expected `fn(ValueRef<'_>) -> std::result::Result<&str, FromSqlError>`
...
45 |     fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(ValueRef<'_>) -> std::result::Result<&str, FromSqlError>`
   |
   = note: expected `fn(ValueRef<'_>) -> std::result::Result<&str, _>`
              found `fn(ValueRef<'_>) -> std::result::Result<&str, _>`
   = help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
   = help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output

Notice the highlighting of the lifetimes (which is a weak hint at the problem). We need to account for Self (as opposed to type parameters).


Update: managed to fix it with a tiny change to that PR:

RalfJung added a commit to RalfJung/rust that referenced this issue May 29, 2020
Tweak impl signature mismatch errors involving `RegionKind::ReVar` lifetimes

Fix rust-lang#66406, fix rust-lang#72106.

```
error: `impl` item signature doesn't match `trait` item signature
  --> $DIR/trait-param-without-lifetime-constraint.rs:14:5
   |
LL |     fn get_relation(&self) -> To;
   |     ----------------------------- expected `fn(&Article) -> &ProofReader`
...
LL |     fn get_relation(&self) -> &ProofReader {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Article) -> &ProofReader`
   |
   = note: expected `fn(&Article) -> &ProofReader`
              found `fn(&Article) -> &ProofReader`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
  --> $DIR/trait-param-without-lifetime-constraint.rs:10:31
   |
LL |     fn get_relation(&self) -> To;
   |                               ^^ consider borrowing this type parameter in the trait
```

r? @nikomatsakis
RalfJung added a commit to RalfJung/rust that referenced this issue May 29, 2020
Tweak impl signature mismatch errors involving `RegionKind::ReVar` lifetimes

Fix rust-lang#66406, fix rust-lang#72106.

```
error: `impl` item signature doesn't match `trait` item signature
  --> $DIR/trait-param-without-lifetime-constraint.rs:14:5
   |
LL |     fn get_relation(&self) -> To;
   |     ----------------------------- expected `fn(&Article) -> &ProofReader`
...
LL |     fn get_relation(&self) -> &ProofReader {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Article) -> &ProofReader`
   |
   = note: expected `fn(&Article) -> &ProofReader`
              found `fn(&Article) -> &ProofReader`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
  --> $DIR/trait-param-without-lifetime-constraint.rs:10:31
   |
LL |     fn get_relation(&self) -> To;
   |                               ^^ consider borrowing this type parameter in the trait
```

r? @nikomatsakis
RalfJung added a commit to RalfJung/rust that referenced this issue May 29, 2020
Tweak impl signature mismatch errors involving `RegionKind::ReVar` lifetimes

Fix rust-lang#66406, fix rust-lang#72106.

```
error: `impl` item signature doesn't match `trait` item signature
  --> $DIR/trait-param-without-lifetime-constraint.rs:14:5
   |
LL |     fn get_relation(&self) -> To;
   |     ----------------------------- expected `fn(&Article) -> &ProofReader`
...
LL |     fn get_relation(&self) -> &ProofReader {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Article) -> &ProofReader`
   |
   = note: expected `fn(&Article) -> &ProofReader`
              found `fn(&Article) -> &ProofReader`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
  --> $DIR/trait-param-without-lifetime-constraint.rs:10:31
   |
LL |     fn get_relation(&self) -> To;
   |                               ^^ consider borrowing this type parameter in the trait
```

r? @nikomatsakis
RalfJung added a commit to RalfJung/rust that referenced this issue May 29, 2020
Tweak impl signature mismatch errors involving `RegionKind::ReVar` lifetimes

Fix rust-lang#66406, fix rust-lang#72106.

```
error: `impl` item signature doesn't match `trait` item signature
  --> $DIR/trait-param-without-lifetime-constraint.rs:14:5
   |
LL |     fn get_relation(&self) -> To;
   |     ----------------------------- expected `fn(&Article) -> &ProofReader`
...
LL |     fn get_relation(&self) -> &ProofReader {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Article) -> &ProofReader`
   |
   = note: expected `fn(&Article) -> &ProofReader`
              found `fn(&Article) -> &ProofReader`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
  --> $DIR/trait-param-without-lifetime-constraint.rs:10:31
   |
LL |     fn get_relation(&self) -> To;
   |                               ^^ consider borrowing this type parameter in the trait
```

r? @nikomatsakis
@bors bors closed this as completed in 8120780 May 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants