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

Incorrect suggestion of function_item_references lint #78571

Closed
tesuji opened this issue Oct 30, 2020 · 1 comment · Fixed by #78659
Closed

Incorrect suggestion of function_item_references lint #78571

tesuji opened this issue Oct 30, 2020 · 1 comment · Fixed by #78659
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tesuji
Copy link
Contributor

tesuji commented Oct 30, 2020

The function_item_references lint suggests a wrong function item cast.

I tried this code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d46c30ab809202ea2b88891e529da4f9

fn main() {
    use std::mem::size_of;
    type Left = [u8; 16];
    println!("{:p}", &size_of::<Left>);
    // println!("{:p}", size_of as fn() -> _);
}

The compiler suggests this:

warning: taking a reference to a function item does not give a function pointer
 --> src/main.rs:4:22
  |
4 |     println!("{:p}", &size_of::<Left>);
  |                      ^^^^^^^^^^^^^^^^ help: cast `size_of` to obtain a function pointer: `size_of as fn() -> _`
  |
  = note: `#[warn(function_item_references)]` on by default

However, following the suggestion causes a compiler error:

error[E0282]: type annotations needed
 --> src/main.rs:5:22
  |
5 |     println!("{:p}", size_of as fn() -> _);
  |                      ^^^^^^^ cannot infer type for type parameter `T` declared on the function `size_of`

I would expect the compiler suggests size_of::<Left> as fn() -> _ instead.

Meta

rustc --version --verbose: (2020-10-29 6bdae9e) from playground

cc #76269

@tesuji tesuji added the C-bug Category: This is a bug. label Oct 30, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 30, 2020
@JohnTitor JohnTitor added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Oct 30, 2020
@ayrtonm
Copy link
Contributor

ayrtonm commented Nov 1, 2020

I'll fix it
@rustbot claim

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Nov 3, 2020
Corrected suggestion for generic parameters in `function_item_references` lint

This commit handles functions with generic type parameters like you pointed out as well as const generics. Also this is probably a minor thing, but the type alias you used in the example doesn't show up so the suggestion right now would be `size_of::<[u8; 16]> as fn() ->`. This is because the lint checker works with MIR instead of HIR. I don't think we can get the alias at that point, but let me know if I'm wrong and there's a way to fix this. Also I put you as the reviewer, but I'm not sure if you want to review it or if it makes more sense to ask one of the original reviewers of this lint.
closes rust-lang#78571
@bors bors closed this as completed in f347dab Nov 3, 2020
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 A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. 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