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

pow inference is not great #137644

Open
oriongonza opened this issue Feb 25, 2025 · 3 comments
Open

pow inference is not great #137644

oriongonza opened this issue Feb 25, 2025 · 3 comments
Labels
A-inference Area: Type inference C-discussion Category: Discussion or questions that doesn't represent real issues. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@oriongonza
Copy link
Contributor

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.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 25, 2025
@jieyouxu jieyouxu added A-inference Area: Type inference C-discussion Category: Discussion or questions that doesn't represent real issues. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 26, 2025
@meithecatte
Copy link
Contributor

All functions being called on numeric types work like this.

This doesn't happen for operators like + because that goes through the trait resolver, I think.

@compiler-errors
Copy link
Member

Why can't the compiler infer that if it's being expected to produce a f64 the type should be an f64?

Because all numeric types define separate pow methods. Method selection cannot know that all pow functions have any underlying pattern to their method signatures, because they're not unified by an underlying trait that gives us a way to reason about these pow methods.

All functions being called on numeric types work like this.

Correct.

This doesn't happen for operators like + because that goes through the trait resolver, I think.

It's not because it goes through the trait solver, but because we special-case operators and inference in typeck.

@oriongonza
Copy link
Contributor Author

I guess that for me this only comes up when calling pow 😄

Is this a duplicate issue? If so I can close, if not, I can make a more general issue and close this too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-discussion Category: Discussion or questions that doesn't represent real issues. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants