rust-lang / rust-lang/rust

pow inference is not great

Open
#137,644 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference C-discussion T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

fn do_pow(n: i32) -> f64 {
    0.95.powi(n)
}
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
   --> mail/mail-common/src/mailbox/attachments.rs:335:18
    |
335 |             0.95.powi(n)
    |                  ^^^^
    |
help: you must specify a concrete type for this numeric value, like `f32`
    |
335 |             0.95_f32.powi(n)
    |             ~~~~~~~~

Why can't the compiler infer that if it's being expected to produce a f64 the type should be an f64?
I can't think of any other cases outside of pow where the inference fails.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal do_pow reproducer and the example at mail/mail-common/src/mailbox/attachments.rs:335. Read the compiler's numeric type inference and method-resolution behavior around powi, then determine whether the expected f64 result can constrain the receiver; done means a documented resolution or an accepted compiler change with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.