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

Account for incorrect attempts to declare "hexadecimal float literals" in E0308 #104706

Closed
gasrios opened this issue Nov 22, 2022 · 0 comments · Fixed by #105852
Closed

Account for incorrect attempts to declare "hexadecimal float literals" in E0308 #104706

gasrios opened this issue Nov 22, 2022 · 0 comments · Fixed by #105852
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

@gasrios
Copy link

gasrios commented Nov 22, 2022

If someone tries to write a hexadecimal float literal:

fn main() {
    let _f:f32 = 0xAAf32;
}

They will get the following output:

Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:2:18
  |
2 |     let _f:f32 = 0xAAf32;
  |            ---   ^^^^^^^ expected `f32`, found integer
  |            |
  |            expected due to this

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

Executing rustc --explain E0308 will give you:

Expected type did not match the received type.

Erroneous code examples:

fn plus_one(x: i32) -> i32 {
    x + 1
}

plus_one("Not a number");
//       ^^^^^^^^^^^^^^ expected `i32`, found `&str`

if "Not a bool" {
// ^^^^^^^^^^^^ expected `bool`, found `&str`
}

let x: f32 = "Not a float";
//     ---   ^^^^^^^^^^^^^ expected `f32`, found `&str`
//     |
//     expected due to this

This error occurs when an expression was used in a place where the compiler
expected an expression of a different type. It can occur in several cases, the
most common being when calling a function and passing an argument which has a
different type than the matching type in the function declaration.

This is different from the much simpler "binary float literal is not supported" message one gets, when trying to use a literal like 0b11f32.

The problem is finding a "f32" at the end of a hexadecimal literal will be interpreted as part of the number, not a reference to float. If the user misses that, they will not get any help from either of the messages above.

Possible solution: the output of rustc --explain E0308 could include the following explanation at the end:

Also, Rust does not support hexadecimal float literals and cannot differentiate
them from hexadecimal integer literals; for example, `0xAAf32` is understood as
`700210 as i32`. Float literals should be written as `170_f32` or `0xAA as f32`.
@gasrios gasrios 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 Nov 22, 2022
@bors bors closed this as completed in 996fb66 Dec 28, 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.

1 participant