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

Open
kornelski opened this issue Jan 2, 2019 · 3 comments

Comments

@kornelski
Copy link
Contributor

commented Jan 2, 2019

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 label Jan 2, 2019
@estebank

This comment has been minimized.

Copy link
Contributor

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.

@kornelski

This comment has been minimized.

Copy link
Contributor Author

commented Sep 27, 2019

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

@estebank

This comment has been minimized.

Copy link
Contributor

commented Sep 28, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.