Skip to content

Rustc Confuses Associated Types on Type Inference #82513

@apopiak

Description

@apopiak

The following code does not compile (playground link):

pub trait Trait {
	type Value;
	type AccountId: Default;
}

pub enum A<T: Trait> {
	_C(
		(T::AccountId, T::AccountId),
	),
}

impl<T: Trait> A<T>
where
	(T::Value, T::Value): Default,
{
	fn foo() -> Self {
		A::_C(Default::default())
	}
}

I expected to see this happen: Successful compilation

Instead, this happened: I get E0271

error[E0271]: type mismatch resolving `<T as Trait>::AccountId == <T as Trait>::Value`
  --> src/lib.rs:17:3
   |
17 |         A::_C(Default::default())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^ expected Trait::AccountId, found Trait::Value
   |
   = note: expected associated type `<T as Trait>::AccountId`
              found associated type `<T as Trait>::Value`

Ways to get this to compile (that shouldn't matter/don't change the semantics):

  • remove the where bound
  • use explicit call to default <(T::AccountId, T::AccountId)>::default()

Meta

rustc --version --verbose:

rustc 1.50.0 (cb75ad5db 2021-02-10)
binary: rustc
commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
commit-date: 2021-02-10
host: x86_64-unknown-linux-gnu
release: 1.50.0

(also happens on 1.52-nightly)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions