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

Diagnostic should explain that 'impl trait' captures generic parameters #68020

Closed
Aaron1011 opened this issue Jan 8, 2020 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. 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-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

The following code: playground:

trait MyTrait {}
impl MyTrait for bool {}

fn make_it<T>() -> impl MyTrait {
    true
}

fn main() {
    let mut a = make_it::<String>();
    a = make_it::<u8>();
}

produces the following error:

error[E0308]: mismatched types
  --> src/main.rs:10:9
   |
10 |     a = make_it::<u8>();
   |         ^^^^^^^^^^^^^^^ expected struct `std::string::String`, found u8
   |
   = note: expected type `impl MyTrait` (struct `std::string::String`)
              found type `impl MyTrait` (u8)

However, it does not explain where u8 and String come from.

When emitting a 'mistmatched type' error for two 'impl trait' types with the same DefId, we should explain that each set of generic parameters creates a unique opaque type, which is not equivalent to other opaque types generated by the same function.

See #66463 (comment) for the original discussion

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. 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. labels Jan 8, 2020
@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jan 8, 2020
@estebank
Copy link
Contributor

Current output:

error[[E0308]](https://doc.rust-lang.org/stable/error_codes/E0308.html): mismatched types
  --> src/main.rs:10:9
   |
4  | fn make_it<T>() -> impl MyTrait {
   |                    ------------
   |                    |
   |                    the expected opaque type
   |                    the found opaque type
...
9  |     let mut a = make_it::<String>();
   |                 ------------------- expected due to this value
10 |     a = make_it::<u8>();
   |         ^^^^^^^^^^^^^^^ expected `String`, found `u8`
   |
   = note: expected opaque type `impl MyTrait` (`String`)
              found opaque type `impl MyTrait` (`u8`)
   = note: distinct uses of `impl Trait` result in different opaque types

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. 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-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants