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

Incorrect bracket is pointed to for return type in closure #50085

Closed
varkor opened this issue Apr 19, 2018 · 6 comments
Closed

Incorrect bracket is pointed to for return type in closure #50085

varkor opened this issue Apr 19, 2018 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@varkor
Copy link
Member

varkor commented Apr 19, 2018

fn main() {
    let _ = || {
        if true {
            true
        } else {
            false
        } // Whoops, forgot a semicolon!
        false
    };
}

Gives the error:

error[E0308]: mismatched types
 --> src/main.rs:4:13
  |
1 | fn main() {
  |           - expected `()` because of default return type
...
4 |             true
  |             ^^^^ expected (), found bool
  |
  = note: expected type `()`
             found type `bool`

The return type of main is referenced, which is wrong — but really the problem is that there are two consecutive expressions that are not separated by a semicolon. It would be good to have an improvement in either respect, though.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 19, 2018
@ashtneoi
Copy link
Contributor

I did some poking around and it looks like it doesn't specifically involve closures. From what I saw, there are three possibly-distinct cases that trigger it, all with slightly different error messages:

1

fn main() {
    let a = { { true } true };
}
error[E0308]: mismatched types
 --> a.rs:2:17
  |
1 | fn main() {
  |           - expected `()` because of default return type
2 |     let a = { { true } true };
  |                 ^^^^ expected (), found bool
  |
  = note: expected type `()`
             found type `bool`

2

fn foo() {
    let a = { { true } true };
}
error[E0308]: mismatched types
 --> b.rs:2:17
  |
1 | fn foo() {
  |          - help: try adding a return type: `-> bool`
2 |     let a = { { true } true };
  |                 ^^^^ expected (), found bool
  |
  = note: expected type `()`
             found type `bool`

3

fn foo() -> () {
    let a = { { true } true };
}
error[E0308]: mismatched types
 --> c.rs:2:17
  |
1 | fn foo() -> () {
  |             -- expected `()` because of return type
2 |     let a = { { true } true };
  |                 ^^^^ expected (), found bool
  |
  = note: expected type `()`
             found type `bool`

It doesn't seem to happen with any other return type.

@estebank
Copy link
Contributor

CC #46720

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 25, 2018
@estebank
Copy link
Contributor

Current output:

error[E0308]: mismatched types
 --> file5.rs:4:13
  |
4 |             true
  |             ^^^^ expected (), found bool
  |
  = note: expected type `()`
             found type `bool`

error[E0308]: mismatched types
 --> file5.rs:6:13
  |
6 |             false
  |             ^^^^^ expected (), found bool
  |
  = note: expected type `()`
             found type `bool`

@varkor
Copy link
Member Author

varkor commented Sep 26, 2019

@estebank: do we have test cases for this sort of problem? It might be worth adding the test in the issue.

@estebank
Copy link
Contributor

@varkor we do, can try and find them for you, but I'm 99% sure we already cover these cases.

@varkor
Copy link
Member Author

varkor commented Sep 26, 2019

I thought it was likely we did. They should be sufficient.

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants