Skip to content

Type inference does not propagate back to {float} associated functions #102401

Open
@jruderman

Description

@jruderman

I tried this code (playground):

fn main() {
    let r = 1.0;
    let _ = r.sqrt();
    let _ = r == 0_f32;
}

I expected rustc to infer the type of r as f32 based on the last line.

Instead, rustc failed to compile my program:

error[E0689]: can't call method `sqrt` on ambiguous numeric type `{float}`
 --> src/main.rs:3:15
  |
3 |     let _ = r.sqrt();
  |               ^^^^
  |
help: you must specify a type for this binding, like `f32`
  |
2 |     let r: f32 = 1.0;
  |          +++++

In most cases, type inference works both forwards and backwards, so why didn't it work here?

This is similar to #24124, but with an even stronger reason to feel the code should work. #99405 may also be related.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-typesRelevant to the types 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