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 type checking of return expressions outside of function bodies #86206

Merged
merged 1 commit into from
Jun 28, 2021

Conversation

FabianWolff
Copy link
Contributor

This pull request fixes #86188. The problem is that the current code for type-checking return expressions stops if the return occurs outside of a function body, while the correct behavior is to continue type-checking the return value expression (otherwise an ICE happens later on because variables declared in the return value expression don't have a type).

Also, I have noticed that it is sometimes not obvious why a return is outside of a function body; for instance, in the example from #86188 (which currently causes an ICE):

fn main() {
    [(); return || {
        let tx;
    }]
}

I have changed the error message to also explain why the return is considered outside of the function body:

error[E0572]: return statement outside of function body
 --> ice0.rs:2:10
  |
1 |  / fn main() {
2 |  |     [(); return || {
  |  |__________^
3 | ||         let tx;
4 | ||     }]
  | ||_____^ the return is part of this body...
5 |  | }
  |  |_- ...not the enclosing function body

@rust-highfive
Copy link
Collaborator

r? @varkor

(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 Jun 10, 2021
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 26, 2021
@Dylan-DPC-zz
Copy link

r? @Mark-Simulacrum

// type-checked, leading to an ICE. This test checks that the ICE no
// longer happens, and that an appropriate error message is issued that
// also explains why the return is considered "outside of a function body"
// if it seems to be inside one, as in the main function below.
Copy link
Contributor

Choose a reason for hiding this comment

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

I found the new error messages puzzling until I saw this comment. Maybe there is some way to make the new error messages be more specific, e.g., "this expression is an array repeat/length count, which is compile-time evaluated in a const context outside the function body"?

@Mark-Simulacrum
Copy link
Member

@bors r+ rollup

I agree that improving the error message to more clearly describe the situation, but this seems like an improvement in the meantime.

@bors
Copy link
Contributor

bors commented Jun 28, 2021

📌 Commit bdddaeb has been approved by Mark-Simulacrum

@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 Jun 28, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 28, 2021
Rollup of 6 pull requests

Successful merges:

 - rust-lang#86206 (Fix type checking of return expressions outside of function bodies)
 - rust-lang#86358 (fix pretty print for `loop`)
 - rust-lang#86568 (Don't dist miri or rust-analyzer on stable or beta.)
 - rust-lang#86683 (:arrow_up: rust-analyzer)
 - rust-lang#86687 (Allow anyone to set `perf-regression` label)
 - rust-lang#86688 (Add a regression test for issue-65384)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 4afdef0 into rust-lang:master Jun 28, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jun 28, 2021
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.
Projects
None yet
9 participants