Skip to content

Fix type checking of return expressions outside of function bodies - #86206

Merged
bors merged 1 commit into
rust-lang:masterfrom
FabianWolff:issue-86188
Jun 28, 2021
Merged

Fix type checking of return expressions outside of function bodies#86206
bors merged 1 commit into
rust-lang:masterfrom
FabianWolff:issue-86188

Conversation

@FabianWolff

Copy link
Copy Markdown
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):

fnmain(){[();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
Copy Markdown
Contributor

r? @varkor

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

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 10, 2021
@crlf0710crlf0710 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
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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

bors commented Jun 28, 2021

Copy link
Copy Markdown
Collaborator

📌 Commit bdddaeb has been approved by Mark-Simulacrum

@borsbors 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
@JohnTitorJohnTitor mentioned this pull request 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:masterJun 28, 2021
@rustbotrustbot added this to the 1.55.0 milestone Jun 28, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.

Projects

None yet

9 participants

@FabianWolff@rust-highfive@Dylan-DPC-zz@Mark-Simulacrum@bors@tlyu@crlf0710@varkor@rustbot