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

Detect match statement intended to be tail expression #81458

Merged
merged 1 commit into from
Feb 26, 2021

Conversation

estebank
Copy link
Contributor

CC #24157

@rust-highfive
Copy link
Collaborator

r? @petrochenkov

(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 Jan 28, 2021
@estebank
Copy link
Contributor Author

I'm not entirely sure this strikes the right balance between user friendliness and code amount :-/

@rust-log-analyzer

This comment has been minimized.

@petrochenkov
Copy link
Contributor

It would be great if someone else could review this, I'm leaving for vacation in a couple of days and won't have enough time.

@estebank
Copy link
Contributor Author

No problem, enjoy the time off!

r? @oli-obk would you have the bandwidth to check this one out? It doesn't need to be approved, just gauging interest/opinion.

Comment on lines +25 to +36
note: you might have meant to return the `match` expression
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:27:6
|
LL | fn wrong(c: &str) -> Box<dyn Foo> {
| ------------ the `match` arms can conform to this return type
LL | / match c {
LL | | "baz" => Box::new(Baz),
LL | | _ => Box::new(Bar),
LL | | };
| | -^ the `match` is a statement because of this semicolon, consider removing it
| |_____|
| this could be implicitly returned but it is a statement, not a tail expression
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the new part.

@bors

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@estebank estebank force-pushed the match-stmt-remove-semi branch 2 times, most recently from fd2a565 to 5afc09f Compare February 25, 2021 00:59
Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

r=me with the nits resolved

compiler/rustc_typeck/src/check/expectation.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/expectation.rs Outdated Show resolved Hide resolved
@estebank
Copy link
Contributor Author

@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Feb 25, 2021

📌 Commit 1d24f07 has been approved by oli-obk

@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 Feb 25, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 26, 2021
…oli-obk

Detect match statement intended to be tail expression

CC rust-lang#24157
@bors
Copy link
Contributor

bors commented Feb 26, 2021

⌛ Testing commit 1d24f07 with merge cecdb18...

@bors
Copy link
Contributor

bors commented Feb 26, 2021

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing cecdb18 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 26, 2021
@bors bors merged commit cecdb18 into rust-lang:master Feb 26, 2021
@rustbot rustbot added this to the 1.52.0 milestone Feb 26, 2021
@Mark-Simulacrum
Copy link
Member

It seems like this was a regression, which seems a little surprising, as the PR appears to try to affect only an error path - maybe storing the additional span is causing some overhead though.

@estebank @oli-obk any thoughts on whether there's some optimization that can be done here? If not the regression seems not horrible - primarily affecting stress tests - but it is rather large on those.

Comment on lines +211 to +218
Some(ret_coercion) if self.in_tail_expr => {
let ret_ty = ret_coercion.borrow().expected_ty();
let ret_ty = self.inh.infcx.shallow_resolve(ret_ty);
self.can_coerce(arm_ty, ret_ty)
&& prior_arm_ty.map_or(true, |t| self.can_coerce(t, ret_ty))
// The match arms need to unify for the case of `impl Trait`.
&& !matches!(ret_ty.kind(), ty::Opaque(..))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Doing this on every tail expression could potentially be the cause of the regression

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opened #82738 to check this.

Comment on lines +573 to +576
let expectation = match expr.kind {
hir::ExprKind::Match(..) if is_last => IsLast(stmt.span),
_ => NoExpectation,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

If the other piece of code isn't at fault, let's try reverting to the logic you had before (bubbling the information down in the function arguments

estebank added a commit to estebank/rust that referenced this pull request Mar 4, 2021
Move the check for potentially forgotten `return` in a tail expression
of arbitrary expressions into the coercion error branch to avoid
computing unncessary coercion checks on successful code.

Follow up to rust-lang#81458.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 6, 2021
… r=oli-obk

Move check only relevant in error case out of critical path

Move the check for potentially forgotten `return` in a tail expression
of arbitrary expressions into the coercion error branch to avoid
computing unncessary coercion checks on successful code.

Follow up to rust-lang#81458.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants