Skip to content

Labeled line for "here the type of var is inferred to be" can be wrong #106590

Closed

Description

Given the following code: playground

struct Wrap<T, S>(T, S);

fn main() {
    let s = Wrap(None, None);
    s.0 = Some(1);
    s.1 = Some(2);
    let z: Wrap<Option<i32>, Option<&str>> = s;
}

The current output is:

error[E0308]: mismatched types
 --> src/main.rs:7:46
  |
5 |     s.0 = Some(1);
  |     - here the type of `s` is inferred to be `Wrap<Option<{integer}>, Option<_>>`
6 |     s.1 = Some(2);
7 |     let z: Wrap<Option<i32>, Option<&str>> = s;
  |            -------------------------------   ^ expected `&str`, found integer
  |            |
  |            expected due to this
  |
  = note: expected struct `Wrap<Option<i32>, Option<&str>>`
             found struct `Wrap<Option<{integer}>, Option<{integer}>>`

Ideally the output should look like:

error[E0308]: mismatched types
 --> src/main.rs:7:46
  |
5 |     s.0 = Some(1);
6 |     s.1 = Some(2);
  |     - here the type of `s` is inferred to be `Wrap<Option<{integer}>, Option<{integer}>>`
7 |     let z: Wrap<Option<i32>, Option<&str>> = s;
  |            -------------------------------   ^ expected `&str`, found integer
  |            |
  |            expected due to this
  |
  = note: expected struct `Wrap<Option<i32>, Option<&str>>`
             found struct `Wrap<Option<{integer}>, Option<{integer}>>`

The problem here is that while s.0 = .. is the first expression to constrain Wrap<_, _>, it's not the expression which constrains Wrap<_, _> in such a way that causes the type error, so pointing to it is possibly misleading -- possibly even more confusing if the lines are further separated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions