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 diagnostic: 'expected X<'_>, found X<'_>' #81446

Closed
cole-miller opened this issue Jan 27, 2021 · 1 comment
Closed

Unhelpful diagnostic: 'expected X<'_>, found X<'_>' #81446

cole-miller opened this issue Jan 27, 2021 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@cole-miller
Copy link

Compiling this code on stable/beta/nightly (playground):

struct X<'x> {
    x: &'x str,
}

trait B {
    fn new(x: X) -> Self;
}

struct S<'x> {
    x: X<'x>,
}

impl<'x> B for S<'x> {
    fn new(x: X) -> S<'x> {
        S { x }
    }
}

correctly results in an error, but part of the diagnostic is less than helpful:

   = note: expected `X<'_>`
              found `X<'_>`
click for full error text
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'x` due to conflicting requirements
  --> src/lib.rs:15:9
   |
15 |         S { x }
   |         ^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 14:5...
  --> src/lib.rs:14:5
   |
14 |     fn new(x: X) -> S<'x> {
   |     ^^^^^^^^^^^^^^^^^^^^^
note: ...so that the expression is assignable
  --> src/lib.rs:15:13
   |
15 |         S { x }
   |             ^
   = note: expected `X<'_>`
              found `X<'_>`
note: but, the lifetime must be valid for the lifetime `'x` as defined on the impl at 13:6...
  --> src/lib.rs:13:6
   |
13 | impl<'x> B for S<'x> {
   |      ^^
note: ...so that the expression is assignable
  --> src/lib.rs:15:9
   |
15 |         S { x }
   |         ^^^^^^^
   = note: expected `S<'x>`
              found `S<'_>`

It seems like either this note should have some detail indicating what the difference between the expected and found conditions is, or it should be left out entirely, because currently it's not providing any information at all.

Reported on URLO here.

(Sorry if this is a duplicate, I don't know how to search effectively for previous reports of the same thing.)

@cole-miller cole-miller added the C-bug Category: This is a bug. label Jan 27, 2021
@jyn514
Copy link
Member

jyn514 commented Jan 29, 2021

Duplicate of #75791

@jyn514 jyn514 marked this as a duplicate of #75791 Jan 29, 2021
@jyn514 jyn514 closed this as completed Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants