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

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

Merged
merged 2 commits into from
Aug 7, 2022

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Aug 4, 2022

This code:

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 #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 #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.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 4, 2022
@rust-highfive
Copy link
Collaborator

r? @lcnr

(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 Aug 4, 2022
@compiler-errors
Copy link
Member Author

Specifically, #84128 regresses because the error that:

struct Foo<T>(T);

fn main() {
    || {
        if false {
            return Foo(0);
        }

        Foo(())
    };
}

the given code^ emits is not being emitted in in CoerceMany, but within the argument checking logic instead. That is, because of the fact we propagate the Expectation that the return type of the block should match return Foo(0);, we end up type-checking something that looks more like:

struct Foo<T>(T);

fn main() {
    || {
        if false {
            return Foo(0);
        }

        Foo::<i32>(())
    };
}

Unfortunately, by the time we get to checking the arguments of the second Foo constructor in that closure body's tail, we don't know why we require that the 0'th argument of Foo is an integer, and not ().

@lcnr
Copy link
Contributor

lcnr commented Aug 6, 2022

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 6, 2022

📌 Commit 1f463ac has been approved by lcnr

It is now in the queue for this repository.

@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 Aug 6, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 6, 2022
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#100071 (deps: dedupe `annotate-snippets` crate versions)
 - rust-lang#100127 (Remove Windows function preloading)
 - rust-lang#100130 (Avoid pointing out `return` span if it has nothing to do with type error)
 - rust-lang#100169 (Optimize `pointer::as_aligned_to`)
 - rust-lang#100175 (ascii -> ASCII in code comment)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1a96f31 into rust-lang:master Aug 7, 2022
@rustbot rustbot added this to the 1.65.0 milestone Aug 7, 2022
@compiler-errors compiler-errors deleted the erroneous-return-span branch August 11, 2023 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants