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

Type inferencer probably could figure this case out (associated types) #23728

Open
carllerche opened this issue Mar 26, 2015 · 6 comments
Open
Labels
A-inference Area: Type inference A-typesystem Area: The type system 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. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@carllerche
Copy link
Member

trait Async {
    type Value;
    type Error;
}

enum Result<T, E> {
    Ok(T),
    Err(E),
}

impl<T, E> Result<T, E> {
    fn reduce<F, U>(self, init: U::Value, action: F) -> U::Value
            where F: Fn(U::Value, T) -> U,
                  U: Async<Error=E> {
        unimplemented!();
    }
}

impl Async for i32 {
    type Value = i32;
    type Error = ();
}

pub fn main() {
    let res: Result<&'static str, ()> = Result::Ok("hello");
    let val: i32 = 123;

    res.reduce(val, |val /* i32 */, curr| val);
    // Compiles if annotated ^^
}

cc @nikomatsakis

@eddyb
Copy link
Member

eddyb commented Mar 26, 2015

This requires two features which have not been implemented yet: #19476 and #21939 - and I'm not certain on the interaction between the two.

@steveklabnik steveklabnik added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-typesystem Area: The type system labels Mar 26, 2015
@nikomatsakis
Copy link
Contributor

Carl, I think you could workaround this limitation by introducing another type parameter:

impl<T, E> Result<T, E> {
    fn reduce<F, U, X>(self, init: X, action: F) -> U::Value
            where F: Fn(X, T) -> U,
                  U: Async<Error=E, Value=X> {
        unimplemented!();
    }
}

Indeed this works in play.

I'm actually not sure why it's not working today. I think what should be happening is that when we try to normalize $U::Value, where $U is the variable we created for U, we wind up with a type variable $1 and the side-constraint that $U: Async<Value=$1>. Clearly $1 will get unified with i32 and then $U should (through the return value) be unified with $1. This seems like enough to figure out everything that is needed. But you do get an error, so presumably something goes wrong at an earlier point in the process, I'm not quite sure what.

@eddyb
Copy link
Member

eddyb commented Mar 26, 2015

Oh, my bad, I didn't realize U::Value was being fed i32 from passing val into the call.

@steveklabnik
Copy link
Member

Triage: this still reproduces today.

@Mark-Simulacrum Mark-Simulacrum added the A-inference Area: Type inference label May 16, 2017
@steveklabnik
Copy link
Member

Triage: no change

1 similar comment
@Spoonbender
Copy link

Triage: no change

@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. labels Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference A-typesystem Area: The type system 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. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants