Skip to content

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

Closed
@Aaron1011

Description

@Aaron1011

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.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