Skip to content

Compiler suggests giving types to typed variables #72130

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

Closed
ISibboI opened this issue May 12, 2020 · 2 comments
Closed

Compiler suggests giving types to typed variables #72130

ISibboI opened this issue May 12, 2020 · 2 comments

Comments

@ISibboI
Copy link

ISibboI commented May 12, 2020

fn main() {
    let steps = 18;
    
    /*
        Uncomment none of the following lines: Compiler wants angle to have a type
        Uncomment any of the following lines: Compiler wants the first uncommented line to have a type
    */
    
    //let radius: f64 = 60.0;
    //let xoff: f64 = 5.0;
    //let yoff: f64 = 4.0;

    for i in 0..steps {
        let angle: f64 = 3.14f64 / steps.into() * i.into();
        println!("{}", angle);
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0284]: type annotations needed for `f64`
  --> src/main.rs:14:34
   |
9  |     let radius: f64 = 60.0;
   |         ------ consider giving `radius` a type
...
14 |         let angle: f64 = 3.14f64 / steps.into() * i.into();
   |                                  ^ cannot infer type for type `f64`
   |
   = note: cannot resolve `<f64 as std::ops::Div<_>>::Output == _`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0284`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

The compiler seems to fail to infer that I want to divide f64 by f64 here. I am no master of the type system so I just gotta accept that. The error message is confusing though, as it points me to give variables a type that are already typed. Rust version is 1.43.1 stable.

Also try uncommenting the commented lines, it will make the error message point to them, even though they are completely unrelated.

@estebank
Copy link
Contributor

The output in nightly is

error[E0284]: type annotations needed: cannot satisfy `<f64 as std::ops::Div<_>>::Output == _`
  --> src/main.rs:14:34
   |
14 |         let angle: f64 = 3.14f64 / steps.into() * i.into();
   |                                  ^ cannot satisfy `<f64 as std::ops::Div<_>>::Output == _`

Fixed by #71960.

@ISibboI ISibboI changed the title Compiler suggests giving types to types variables Compiler suggests giving types to typed variables May 13, 2020
@ISibboI
Copy link
Author

ISibboI commented May 13, 2020

Thank you, then this seems to be resolved.

@ISibboI ISibboI closed this as completed May 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants