Skip to content

Incorrect "expected associated type, found tuple" error in trait implementation #86883

Open
@AzureMarker

Description

@AzureMarker

When generically implementing a trait with an associated type (along with some other specific circumstances), there is an error which essentially says that type A doesn't equal type A.

Minimized:

trait Protocol {
    type Address;
    fn connect(source_address: Self::Address)
    where
        Self: SupportedConfiguration;
}

impl<P> Protocol for Udp<P> {
    type Address = (usize, usize);

    fn connect((_source_address, _source_port): Self::Address)
    where
        Self: SupportedConfiguration,
    {
    }
}

trait SupportedConfiguration: Protocol {}
impl<P> SupportedConfiguration for Udp<P> {}

struct Udp<P> {
    inner_protocol: P,
}

Error message:

error[E0308]: mismatched types
  --> src/lib.rs:11:16
   |
11 |     fn connect((_source_address, _source_port): Self::Address)
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  ------------- expected due to this
   |                |
   |                expected associated type, found tuple
   |
   = note: expected associated type `<Udp<P> as Protocol>::Address`
                        found tuple `(_, _)`
   = help: consider constraining the associated type `<Udp<P> as Protocol>::Address` to `(_, _)` or calling a method that returns `<Udp<P> as Protocol>::Address`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

Changing line 11 by expanding the associated type like so produces a new error:

    fn connect((_source_address, _source_port): (usize, usize))
error[E0053]: method `connect` has an incompatible type for trait
  --> src/lib.rs:11:49
   |
3  |     fn connect(source_address: Self::Address)
   |                                ------------- type in trait
...
9  |     type Address = (usize, usize);
   |     ------------------------------ expected this associated type
10 | 
11 |     fn connect((_source_address, _source_port): (usize, usize))
   |                                                 ^^^^^^^^^^^^^^
   |                                                 |
   |                                                 expected associated type, found tuple
   |                                                 help: change the parameter type to match the trait: `<Udp<P> as Protocol>::Address`
   |
   = note: expected fn pointer `fn(<Udp<P> as Protocol>::Address)`
              found fn pointer `fn((usize, usize))`

Meta

This error appears in stable and nightly.

rustc --version --verbose:

rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-unknown-linux-gnu
release: 1.53.0
LLVM version: 12.0.1

rustc +nightly --version --verbose:

rustc 1.55.0-nightly (798baebde 2021-07-02)
binary: rustc
commit-hash: 798baebde1fe77e5a660490ec64e727a5d79970d
commit-date: 2021-07-02
host: x86_64-unknown-linux-gnu
release: 1.55.0-nightly
LLVM version: 12.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-lazy-normalizationArea: Lazy normalization (tracking issue: #60471)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions