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

Error message for type mismatch between impl Traits needs better explanation #57266

Closed
kornelski opened this issue Jan 2, 2019 · 3 comments · Fixed by #66463
Closed

Error message for type mismatch between impl Traits needs better explanation #57266

kornelski opened this issue Jan 2, 2019 · 3 comments · Fixed by #66463
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kornelski
Copy link
Contributor

The message "expected type impl Trait (opaque type), found type impl Trait (opaque type)" sounds like a paradox: the compiler has found exactly what it expected, and yet it doesn't match.

The explanatory text of E0308 is the same as for all other banal type errors, so it's not addressing this problem specifically.

From the questions I'm seeing in the user forum I have an impression that users think impl Trait is like dyn Trait and try to use it as an abstraction for mixing different types.

fn f1() -> impl Copy {
    "1"
}
fn f2() -> impl Copy {
    2
}

fn main() {
    let mut x = f1();
    x = f2();
}

10 | x = f2();
| ^^^^ expected opaque type, found a different opaque type
|
= note: expected type impl std::marker::Copy (opaque type)
found type impl std::marker::Copy (opaque type)


Suggestions:

  • Make it a separate error, so that it'll get its own rustc --explain document.

  • If possible, instead of "(opaque type)" print more internal type information, e.g. like for closures:

    = note: expected type `impl [std::marker::Copy@src/main.rs:2:10]`
            found type `impl [std::marker::Copy@src/main.rs:5:10]`
    
    = note: each `impl Trait`, even for the same trait, is a different type.
    = help: consider using `Box<dyn Trait>` or separate generic parameters.
    
@varkor varkor added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 2, 2019
@estebank
Copy link
Contributor

estebank commented Sep 26, 2019

Current output:

error[E0308]: mismatched types
  --> src/main.rs:10:9
   |
10 |     x = f2();
   |         ^^^^ expected opaque type, found a different opaque type
   |
   = note: expected type `impl std::marker::Copy` (opaque type at <src/main.rs:1:12>)
              found type `impl std::marker::Copy` (opaque type at <src/main.rs:4:12>)
   = note: distinct uses of `impl Trait` result in different opaque types

The suggestion to use Box<dyn Trait> could be interesting as text, but giving a structured suggestion would be quite difficult.

@estebank estebank added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 26, 2019
@kornelski
Copy link
Contributor Author

Addition of the line where the type comes from is a significant improvement. Shall I close the issue?

@estebank
Copy link
Contributor

Let's keep it open focused on getting some span labels pointing at those places and nothing else.

Centril added a commit to Centril/rust that referenced this issue Jan 10, 2020
…e-types, r=Centril

Point at opaque and closure type definitions in type errors

Fixes rust-lang#57266, fixes rust-lang#67117.
@bors bors closed this as completed in aabb037 Jan 10, 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 A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-type-system Area: Type system 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.

3 participants