Skip to content

unused type params on fn lead to difficult to deciper errors at call site #28153

Closed
@pnkfelix

Description

@pnkfelix

Consider code like this (playpen):

fn add3<T>(x: i32) -> i32 { x + 3 }

fn main() {
    println!("add3(4): {}", add3(4));
}

The Rust compiler currently rejects the above, because it cannot infer what type to assign to the type parameter T in the above invocation add3(4).

The problematic thing is the message:

<anon>:4:29: 4:33 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]
<anon>:4     println!("add3(4): {}", add3(4));
                                     ^~~~
  1. This message complains about _, rather than referencing the parameter T of fn add3.
  2. It might be nice if, when we encounter this error, if we also did an additional check for the function itself and see if the type parameter in question is unused in the signature of fn add (since in such a situation, there is no way that the callsite will be able to supply the information via type annotation -- the only option is the generic parameter binding form like add3::<(f64,i8)>(4), and it is likely (though not assured) that the actual bug is in the definition of fn add3 itself, rather than at the call site.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions