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

Suggest adding zero to errornous floating point number with dot and exponent but no post-dot digit #98836

Closed
Havvy opened this issue Jul 3, 2022 · 2 comments · Fixed by #98972
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Havvy
Copy link
Contributor

Havvy commented Jul 3, 2022

When writing floating point numbers, some people try to include the dot and and exponent notation. Rust sees this as a field access on an integral value. It should suggest adding a 0 after the dot.

Given the following code: Playpen

fn main() {
    let n = 2.e1;
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0610]](https://doc.rust-lang.org/stable/error-index.html#E0610): `{integer}` is a primitive type and therefore doesn't have fields
 --> src/main.rs:2:15
  |
2 |     let n = 2.e1;
  |               ^^

For more information about this error, try `rustc --explain E0610`.
error: could not compile `playground` due to previous error

Ideally the output should suggest adding a zero, like as follows:

Compiling playground v0.0.1 (/playground)
error[[E0610]](https://doc.rust-lang.org/stable/error-index.html#E0610): `{integer}` is a primitive type and therefore doesn't have fields
 --> src/main.rs:2:15
  |
2 |     let n = 2.e1;
  |               ^^

For more information about this error, try `rustc --explain E0610`.
error: could not compile `playground` due to previous error

help: If the number is meant to be a floating point number, add a `0` after the period.
 --> src/main.rs:2:15
  |
2 |     let n = 2.0e1;
  |                 ^

It might also be good to squelch the E0610 message altogether too.

@Havvy Havvy added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 3, 2022
@leonardo-m
Copy link

I think not requiring a zero (or other digit) before and after the dot for floating point numbers was a small Rust design mistake, that causes troubles later.

@mcobzarenco
Copy link

I think not requiring a zero (or other digit) before and after the dot for floating point numbers was a small Rust design mistake, that causes troubles later.

Even so, the problem would remain, no? Rust interprets 2 as an integer and . as field acces. Requiring a 0 after the dot for floats would not change how this gets parsed.

Rereading, do you mean that if a digit after the dot were required, then people would be used to always include it and not to run into this? Agreed

@TaKO8Ki TaKO8Ki self-assigned this Jul 6, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 12, 2022
…-to-floating-point-number, r=compiler-errors

Suggest adding a missing zero to a floating point number

fixes rust-lang#98836
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 12, 2022
…-to-floating-point-number, r=compiler-errors

Suggest adding a missing zero to a floating point number

fixes rust-lang#98836
@bors bors closed this as completed in 99fc65b Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants