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

"ambiguous associated type" when accessing concrete type (but not on generic type) #104119

Open
RalfJung opened this issue Nov 7, 2022 · 3 comments
Labels
A-associated-items Area: Associated items such as associated types and consts. A-inference Area: Type inference T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented Nov 7, 2022

Consider the following code:

trait Memory {
    type Provenance;
}

struct BasicMemory;

impl Memory for BasicMemory {
    type Provenance = ();
}

fn some_fn() {
    let x: BasicMemory::Provenance; // this fails
}

fn some_generic_fn<T: Memory>() {
    let x: T::Provenance; // this works
}

Using Type::Provenance works when Type is a generic parameter that implements the appropriate trait, but strangely fails when Type is a concrete type that implements the appropriate trait. That seems like a bug? Or at least it is an odd limitation that it'd be nice to lift. :)

Thanks to @memoryleak47 for pointing this out!

@JoelCourtney
Copy link

You need to use <BasicMemory as Memory>::Provenance. The reason why is because BasicMemory can implement multiple traits that happen to have the Provenance associated type, so it could be ambiguous.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 7, 2022

It could, but it doesn't. This compiles:

trait Memory {
    type Provenance;
    
    fn foo();
}

struct BasicMemory;

impl Memory for BasicMemory {
    type Provenance = ();
    fn foo() {}
}

fn some_fn() {
    let y = BasicMemory::foo();
}

fn some_generic_fn<T: Memory>() {
    let y = T::foo();
}

This is the exact same situation but with a function foo rather than an associated type.

@Jules-Bertholet
Copy link
Contributor

I know pretty much nothing about trait system internals, so can't say with any certainty, but I suspect this might be blocked on lazy normalization, like inherent associated types?

@jyn514 jyn514 added A-associated-items Area: Associated items such as associated types and consts. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference labels Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-inference Area: Type inference 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

4 participants