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 calling size_of when it is missing parens in a comparison #101054

Closed
fitzgen opened this issue Aug 26, 2022 · 1 comment · Fixed by #101100
Closed

Suggest calling size_of when it is missing parens in a comparison #101054

fitzgen opened this issue Aug 26, 2022 · 1 comment · Fixed by #101100
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

@fitzgen
Copy link
Member

fitzgen commented Aug 26, 2022

Given the following code:

fn is_one_byte<T>() -> bool {
    if std::mem::size_of::<T> == 1 {
        true
    } else {
        false
    }
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0369]](https://doc.rust-lang.org/stable/error-index.html#E0369): binary operation `==` cannot be applied to type `fn() -> usize {std::mem::size_of::<T>}`
 --> src/lib.rs:2:31
  |
2 |     if std::mem::size_of::<T> == 1 {
  |        ---------------------- ^^ - {integer}
  |        |
  |        fn() -> usize {std::mem::size_of::<T>}

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/lib.rs:2:34
  |
2 |     if std::mem::size_of::<T> == 1 {
  |                                  ^ expected fn item, found integer
  |
  = note: expected fn item `fn() -> usize {std::mem::size_of::<T>}`
                found type `{integer}`

Some errors have detailed explanations: E0308, E0369.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `playground` due to 2 previous errors

Ideally the output should look like:

Try calling the `size_of` function:

    std::mem::size_of::<T>()
                          ^^ try calling the `size_of` function        

Probably worth not making this diagnostic suggestion specific to size_of but to any type error where there is a comparison of a function and the return type of that function.

@fitzgen fitzgen 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 Aug 26, 2022
@compiler-errors
Copy link
Member

We have similar diagnostics to suggest using parentheses other places, probably just need to generalize them to work for operator dispatch as well. I'll fix this this weekend.

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 31, 2022
…ggestions, r=petrochenkov

Make call suggestions more general and more accurate

Cleans up some suggestions that have to do with adding `()` to make typeck happy.

1. Drive-by rename of `expr_t` to `base_ty` since it's the type of the `base_expr`
1. Autoderef until we get to a callable type in `suggest_fn_call`.
1. Don't erroneously suggest calling constructor when a method/field does not exist on it.
1. Suggest calling a method receiver if its function output has a method (e.g. `fn.method()` => `fn().method()`)
1. Extend call suggestions to type parameters, fn pointers, trait objects where possible
1. Suggest calling in operators too (fixes rust-lang#101054)
1. Use `/* {ty} */` as argument placeholder instead of just `_`, which is confusing and makes suggestions look less like `if let` syntax.
@bors bors closed this as completed in b8b2f88 Aug 31, 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.

2 participants