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

Reordering type parameters leads to wrong inference #1569

Open
Schaeff opened this issue Jul 13, 2024 · 1 comment
Open

Reordering type parameters leads to wrong inference #1569

Schaeff opened this issue Jul 13, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Schaeff
Copy link
Collaborator

Schaeff commented Jul 13, 2024

This code compiles:

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

let<T, E, U> map: Result<T, E>, (T -> U) -> Result<U, E> = |result, f| match result {
    Result::Ok(v) => Result::Ok(f(v)),
    Result::Err(e) => Result::Err(e)
};

But if we change this

-let<T, E, U>
+let<T, U, E>

We get this error:

error: Inferred type scheme for symbol std::result::map does not match the declared type.
Inferred: let<T1, T2, T3> std::result::map: std::result::Result<T1, T2>, (T1 -> T3) -> std::result::Result<T3, T2>
Declared: let<T, U, E> std::result::map: std::result::Result<T, E>, (T -> U) -> std::result::Result<U, E>

It seems like type parameters must be ordered in the same order they appear in the type for inference to work.

@Schaeff Schaeff added the bug Something isn't working label Jul 13, 2024
@chriseth
Copy link
Member

It's a duplicate of #1305 (but let me close that one).

In general, I'm also considering not making it an error if the declared type is not the most generic possible, just check that it's compatible with the code...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants