Skip to content

Mediocre error message for f32/f64 ambiguity #47759

Closed

Description

@nieksand

I have this code:

    let _ = |x: f64| x * 2.0.exp();

https://play.rust-lang.org/?gist=58e23173aec4b798d8021af0ead25bbd&version=nightly

I get this error message:

error[E0599]: no method named `exp` found for type `{float}` in the current scope

I can work around by:

    let _ = |x: f64| x * 2.0f64.exp();

The world would be a better place if:

  • Type inference could just figure out 2.0 has to be f64 given the type on x
  • Failing that, an error message indicating the problem is f32 vs f64 ambiguity rather than this mysterious {float} thing

Activity

  1. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jan 25, 2018
  2. kennytm commented on Jan 25, 2018

    @kennytm
    Member

    Note that type inference cannot figure out 2.0 is an f64 just by x * 2.0.exp() because this is perfectly valid.

    struct Foo;
    trait Wat {
        fn exp(self) -> Foo;
    }
    impl Wat for f32 {
        fn exp(self) -> Foo { Foo }
    }
    impl std::ops::Mul<Foo> for f64 {
        type Output = &'static str;
        fn mul(self, _: Foo) -> &'static str {
            "wat"
        }
    }
    
    fn main() {
        let _ = |x: f64| x * 2.0.exp();
        //let _ = |x: f64| x * 2.0f64.exp();
    }
  3. nieksand commented on Jan 25, 2018

    @nieksand
    ContributorAuthor

    Yipes. Inference is definitely out.

  4. added a commit that references this issue on Apr 10, 2020
    dff5a11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-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