Skip to content

Rust suggests invalid syntax for returning a method call in E0282 #127109

Closed
@theemathas

Description

@theemathas

Code

struct Foo;

impl Foo {
    fn generic<T>(self) -> Option<T> {
        None
    }
}

fn bar() -> Option<i32> {
    if true {
        Foo.generic();   
    }
    
    None
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0282]: type annotations needed
  --> src/lib.rs:11:13
   |
11 |         Foo.generic();   
   |             ^^^^^^^ cannot infer type of the type parameter `T` declared on the method `generic`
   |
help: consider specifying the generic argument
   |
11 |         Foo.generic::<T>();   
   |                    +++++
help: you might have meant to return this to infer its type parameters
   |
11 |         Foo.return generic();   
   |             ++++++

For more information about this error, try `rustc --explain E0282`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

Compiling playground v0.0.1 (/playground)
error[E0282]: type annotations needed
  --> src/lib.rs:11:13
   |
11 |         Foo.generic();   
   |             ^^^^^^^ cannot infer type of the type parameter `T` declared on the method `generic`
   |
help: consider specifying the generic argument
   |
11 |         Foo.generic::<T>();   
   |                    +++++
help: you might have meant to return this to infer its type parameters
   |
11 |         return Foo.generic();   
   |         ++++++

For more information about this error, try `rustc --explain E0282`.
error: could not compile `playground` (lib) due to 1 previous error

Rationale and extra context

The suggested return is incorrect.

Discovered by shrodingerskittenn on the rust community discord.

Other cases

No response

Rust Version

Stable version: 1.79.0 on the playground

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions