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

Account for ! arm in tail match expr #117526

Merged
merged 1 commit into from
Nov 28, 2023
Merged

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Nov 2, 2023

On functions with a default return type that influences the coerced type of match arms, check if the failing arm is actually of type !. If so, suggest changing the return type so the coercion against the prior arms is successful.

error[E0308]: `match` arms have incompatible types
  --> $DIR/match-tail-expr-never-type-error.rs:9:13
   |
LL |   fn bar(a: bool) {
   |                  - help: try adding a return type: `-> i32`
LL | /     match a {
LL | |         true => 1,
   | |                 - this is found to be of type `{integer}`
LL | |         false => {
LL | |             never()
   | |             ^^^^^^^
   | |             |
   | |             expected integer, found `()`
   | |             this expression is of type `!`, but it get's coerced to `()` due to its surrounding expression
LL | |         }
LL | |     }
   | |_____- `match` arms have incompatible types

Fix #24157.

@rustbot
Copy link
Collaborator

rustbot commented Nov 2, 2023

r? @b-naber

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 2, 2023
@@ -139,7 +139,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&cause,
Some(&arm.body),
arm_ty,
|err| self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm),
|err| {
if let hir::ExprKind::Block(block, _) = arm.body.kind
Copy link
Contributor

Choose a reason for hiding this comment

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

Please hide this closure body behind a function!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@bors
Copy link
Contributor

bors commented Nov 22, 2023

☔ The latest upstream changes (presumably #118143) made this pull request unmergeable. Please resolve the merge conflicts.

@b-naber
Copy link
Contributor

b-naber commented Nov 26, 2023

Sorry, I forgot that I had already looked at this before 🤦

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Nov 26, 2023

📌 Commit 1d8e053 has been approved by b-naber

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 Nov 26, 2023
@bors
Copy link
Contributor

bors commented Nov 27, 2023

⌛ Testing commit 1d8e053 with merge d2fb319...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 27, 2023
Account for `!` arm in tail `match` expr

On functions with a default return type that influences the coerced type of `match` arms, check if the failing arm is actually of type `!`. If so, suggest changing the return type so the coercion against the prior arms is successful.

```
error[E0308]: `match` arms have incompatible types
  --> $DIR/match-tail-expr-never-type-error.rs:9:13
   |
LL |   fn bar(a: bool) {
   |                  - help: try adding a return type: `-> i32`
LL | /     match a {
LL | |         true => 1,
   | |                 - this is found to be of type `{integer}`
LL | |         false => {
LL | |             never()
   | |             ^^^^^^^
   | |             |
   | |             expected integer, found `()`
   | |             this expression is of type `!`, but it get's coerced to `()` due to its surrounding expression
LL | |         }
LL | |     }
   | |_____- `match` arms have incompatible types
```

Fix rust-lang#24157.
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Nov 27, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 27, 2023
On functions with a default return type that influences the coerced type
of `match` arms, check if the failing arm is actually of type `!`. If
so, suggest changing the return type so the coercion against the prior
arms is successful.

```
error[E0308]: `match` arms have incompatible types
  --> $DIR/match-tail-expr-never-type-error.rs:9:13
   |
LL |   fn bar(a: bool) {
   |                  - help: try adding a return type: `-> i32`
LL | /     match a {
LL | |         true => 1,
   | |                 - this is found to be of type `{integer}`
LL | |         false => {
LL | |             never()
   | |             ^^^^^^^
   | |             |
   | |             expected integer, found `()`
   | |             this expression is of type `!`, but it get's coerced to `()` due to its surrounding expression
LL | |         }
LL | |     }
   | |_____- `match` arms have incompatible types
```

Fix rust-lang#24157.
@estebank
Copy link
Contributor Author

@bors r=b-naber

@bors
Copy link
Contributor

bors commented Nov 27, 2023

📌 Commit 8221f9c has been approved by b-naber

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 Nov 27, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 27, 2023
Account for `!` arm in tail `match` expr

On functions with a default return type that influences the coerced type of `match` arms, check if the failing arm is actually of type `!`. If so, suggest changing the return type so the coercion against the prior arms is successful.

```
error[E0308]: `match` arms have incompatible types
  --> $DIR/match-tail-expr-never-type-error.rs:9:13
   |
LL |   fn bar(a: bool) {
   |                  - help: try adding a return type: `-> i32`
LL | /     match a {
LL | |         true => 1,
   | |                 - this is found to be of type `{integer}`
LL | |         false => {
LL | |             never()
   | |             ^^^^^^^
   | |             |
   | |             expected integer, found `()`
   | |             this expression is of type `!`, but it get's coerced to `()` due to its surrounding expression
LL | |         }
LL | |     }
   | |_____- `match` arms have incompatible types
```

Fix rust-lang#24157.
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 27, 2023
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#111133 (Detect Python-like slicing and suggest how to fix)
 - rust-lang#114708 (Allow setting `rla` labels via `rustbot`)
 - rust-lang#117526 (Account for `!` arm in tail `match` expr)
 - rust-lang#118282 (effects: Run `enforce_context_effects` for all method calls)
 - rust-lang#118366 (Detect and reject malformed `repr(Rust)` hints)
 - rust-lang#118375 (Add -Zunpretty=stable-mir output test)

r? `@ghost`
`@rustbot` modify labels: rollup
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Nov 27, 2023
Account for `!` arm in tail `match` expr

On functions with a default return type that influences the coerced type of `match` arms, check if the failing arm is actually of type `!`. If so, suggest changing the return type so the coercion against the prior arms is successful.

```
error[E0308]: `match` arms have incompatible types
  --> $DIR/match-tail-expr-never-type-error.rs:9:13
   |
LL |   fn bar(a: bool) {
   |                  - help: try adding a return type: `-> i32`
LL | /     match a {
LL | |         true => 1,
   | |                 - this is found to be of type `{integer}`
LL | |         false => {
LL | |             never()
   | |             ^^^^^^^
   | |             |
   | |             expected integer, found `()`
   | |             this expression is of type `!`, but it get's coerced to `()` due to its surrounding expression
LL | |         }
LL | |     }
   | |_____- `match` arms have incompatible types
```

Fix rust-lang#24157.
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 27, 2023
…llaumeGomez

Rollup of 8 pull requests

Successful merges:

 - rust-lang#111133 (Detect Python-like slicing and suggest how to fix)
 - rust-lang#114708 (Allow setting `rla` labels via `rustbot`)
 - rust-lang#117526 (Account for `!` arm in tail `match` expr)
 - rust-lang#118341 (Simplify indenting in THIR printing)
 - rust-lang#118366 (Detect and reject malformed `repr(Rust)` hints)
 - rust-lang#118375 (Add -Zunpretty=stable-mir output test)
 - rust-lang#118381 (rustc_span: Use correct edit distance start length for suggestions)
 - rust-lang#118384 (Address unused tuple struct fields in rustdoc)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 28, 2023
…mpiler-errors

Rollup of 9 pull requests

Successful merges:

 - rust-lang#111133 (Detect Python-like slicing and suggest how to fix)
 - rust-lang#114708 (Allow setting `rla` labels via `rustbot`)
 - rust-lang#117526 (Account for `!` arm in tail `match` expr)
 - rust-lang#118172 (Add `pretty_terminator` to pretty stable-mir)
 - rust-lang#118202 (Added linker_arg(s) Linker trait methods for link-arg to be prefixed "-Wl," for cc-like linker args and not verbatim)
 - rust-lang#118374 (QueryContext: rename try_collect_active_jobs -> collect_active_jobs, change return type from Option<QueryMap> to QueryMap)
 - rust-lang#118381 (rustc_span: Use correct edit distance start length for suggestions)
 - rust-lang#118382 (Address unused tuple struct fields in the compiler)
 - rust-lang#118384 (Address unused tuple struct fields in rustdoc)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8ff558b into rust-lang:master Nov 28, 2023
11 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Nov 28, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 28, 2023
Rollup merge of rust-lang#117526 - estebank:issue-24157, r=b-naber

Account for `!` arm in tail `match` expr

On functions with a default return type that influences the coerced type of `match` arms, check if the failing arm is actually of type `!`. If so, suggest changing the return type so the coercion against the prior arms is successful.

```
error[E0308]: `match` arms have incompatible types
  --> $DIR/match-tail-expr-never-type-error.rs:9:13
   |
LL |   fn bar(a: bool) {
   |                  - help: try adding a return type: `-> i32`
LL | /     match a {
LL | |         true => 1,
   | |                 - this is found to be of type `{integer}`
LL | |         false => {
LL | |             never()
   | |             ^^^^^^^
   | |             |
   | |             expected integer, found `()`
   | |             this expression is of type `!`, but it get's coerced to `()` due to its surrounding expression
LL | |         }
LL | |     }
   | |_____- `match` arms have incompatible types
```

Fix rust-lang#24157.
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.

Incorrect "match arms have incompatible types" error message
6 participants