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

fix incomplete diagnostic notes when closure returns conflicting for genric type #84244

Merged
merged 1 commit into from
Apr 17, 2021

Conversation

ABouttefeux
Copy link
Contributor

fixes #84128
Correctly report the span on for conflicting return type in closures

@rust-highfive
Copy link
Collaborator

r? @estebank

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 16, 2021
@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Apr 16, 2021

📌 Commit af90cac has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 16, 2021
LL | Foo(())
| ^^ expected integer, found `()`
|
note: return type inferred to be `{integer}` here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really the 'return type' that's being inferred here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that, thanks. I will fix it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it either be "return type inferred to be Foo<{integer}> here" with span of Foo(0) indicated; or else "{integer} inferred here" (with the span of 0 indicated)? (My personal view, FWIW, is that the former is a tad more informative but either seems fine).

As it is, it's a bit confusing: essentially "{integer} inferred here" but Foo(0) indicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some issue to solve this I am not sure how do do it. I found that the suggestions is emitted from
compiler\rustc_typeck\src\check\fn_ctxt\checks.rs: check_argument_types
compiler\rustc_typeck\src\check\demand.rs: demand_coerce
compiler\rustc_typeck\src\check\demand.rs: demand_coerce_diag
compiler\rustc_typeck\src\check\demand.rs: emit_coerce_suggestions
is there a way to get the return type from the FnCtxt ?

jyn514 pushed a commit to jyn514/rust that referenced this pull request Apr 16, 2021
…-suggest, r=estebank

fix incomplete diagnostic notes when closure returns conflicting for genric type

fixes rust-lang#84128
Correctly report the span on for conflicting return type in closures
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Apr 17, 2021
…-suggest, r=estebank

fix incomplete diagnostic notes when closure returns conflicting for genric type

fixes rust-lang#84128
Correctly report the span on for conflicting return type in closures
@bors
Copy link
Contributor

bors commented Apr 17, 2021

⌛ Testing commit af90cac with merge 080d302...

@bors
Copy link
Contributor

bors commented Apr 17, 2021

☀️ Test successful - checks-actions
Approved by: estebank
Pushing 080d302 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 17, 2021
@bors bors merged commit 080d302 into rust-lang:master Apr 17, 2021
@rustbot rustbot added this to the 1.53.0 milestone Apr 17, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 6, 2022
…pan, r=lcnr

Avoid pointing out `return` span if it has nothing to do with type error

This code:

```rust
fn f(_: String) {}

fn main() {
    let x = || {
        if true {
            return ();
        }
        f("");
    };
}
```

Emits this:
```
   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:8:11
  |
8 |         f("");
  |           ^^- help: try using a conversion method: `.to_string()`
  |           |
  |           expected struct `String`, found `&str`
  |
note: return type inferred to be `String` here
 --> src/main.rs:6:20
  |
6 |             return ();
  |                    ^^
```

Specifically, that note has nothing to do with the type error in question. This is because the change implemented in rust-lang#84244 tries to point out the `return` span on _any_ type coercion error within a closure that happens after a `return` statement, regardless of if the error has anything to do with it.

This is really easy to trigger -- just needs a closure (or an `async`) and an early return (or any other form, e.g. `?` operator suffices) -- and super distracting in production codebases. I'm letting rust-lang#84128 regress because that issue is much harder to fix correctly, and I can re-open that issue after this lands.

As a drive-by, I added a `resolve_vars_if_possible` to the coercion error logic, which leads to some error improvements. Unrelated to the issue above, though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incomplete diagnostic notes when closure returns conflicting instantiations of generic type
7 participants