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

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

Closed
theemathas opened this issue Jun 29, 2024 · 0 comments · Fixed by #127129
Closed

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

theemathas opened this issue Jun 29, 2024 · 0 comments · Fixed by #127129
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

@theemathas
Copy link
Contributor

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

@theemathas theemathas 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 Jun 29, 2024
@compiler-errors compiler-errors self-assigned this Jun 29, 2024
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jul 1, 2024
…jieyouxu

Use full expr span for return suggestion on type error/ambiguity

We sometimes use parts of an expression rather than the whole thing for an obligation span. For example, a method obligation will just point to the path segment corresponding to the `method` in `rcvr.method(args)`.

So let's not use that assuming it'll point to the *whole* expression span, which we can access from the expr hir id we store in `ObligationCauseCode::WhereClauseInExpr`.

Fixes rust-lang#127109
@bors bors closed this as completed in 61fe6b6 Jul 1, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 1, 2024
Rollup merge of rust-lang#127129 - compiler-errors:full-expr-span, r=jieyouxu

Use full expr span for return suggestion on type error/ambiguity

We sometimes use parts of an expression rather than the whole thing for an obligation span. For example, a method obligation will just point to the path segment corresponding to the `method` in `rcvr.method(args)`.

So let's not use that assuming it'll point to the *whole* expression span, which we can access from the expr hir id we store in `ObligationCauseCode::WhereClauseInExpr`.

Fixes rust-lang#127109
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