Skip to content

The type inference for T cannot be done when the matched type has the form of T::Output and the trait is user-defined but it can be done for FnTrait #115406

Open
@xmh0511

Description

@xmh0511
trait MyTrait<T> {
    type Output;
}
impl<T> MyTrait<T> for T {
    type Output = T;
}
fn show<T: MyTrait<T, Output = T> + Default>() -> T::Output {
    T::default()
}

fn main() {
    let i: i32 = show();
}

In this case, the compiler cannot infer the type for T, however, as specified in the trait bound T: MyTrait<T, Output = T>, which implies that T::Output is just T. If we change T::Output to T, then the compiler will infer T as i32.

In contrast, the compiler can infer Fn::Output, for example:

fn infer_output<F,U:Default>(_:F)->F::Output
where F:Fn()->U
{
   U::default()
}
fn function<U:Default>()->U{
    U::default()
}
fn main() {
    let i:i32 = infer_output(function);
}

In this example, the compiler can know that F::Output is U, and infer U with type i32. The compiler may enhance type inference to make it more powerful for the first example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-inferenceArea: Type inferenceT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions