Skip to content

Inference failure with int inference variable #26841

@eefriedman

Description

@eefriedman
fn f<T>() -> T { panic!(); }
fn main() {
    let mut a = f();
    let b : u8 = a+a;
    a = 1;
}
<anon>:4:18: 4:21 error: type mismatch resolving `<i32 as core::ops::Add>::Output == u8`:
 expected i32,
    found u8 [E0271]
<anon>:4     let b : u8 = a+a;
                          ^~~

Given that the type of b is u8, the type of a is a builtin integer type, and the result of a+a is assigned to b, the type of a must be u8. Currently, this rule only applies "forwards": it only works if a is known to be a builtin integer type when the addition is being checked. However, type inference could be extended to apply it backwards as well.

I'm not sure if this is relevant in practical usage of Rust.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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