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

Parse expression after else as a condition if followed by { #97298

Merged
merged 1 commit into from
May 24, 2022

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented May 22, 2022

Fixes #49361.

Two things:

  1. This wording needs help. I can never find a natural/intuitive phrasing when I write diagnostics 😅
  2. Do we even want to show the "wrap in braces" case? I would assume most of the time the "add an if" case is the right one.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 22, 2022
@rust-highfive
Copy link
Collaborator

r? @davidtwco

(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 May 22, 2022
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

One suggestion, otherwise looks good to me.

compiler/rustc_parse/src/parser/expr.rs Outdated Show resolved Hide resolved
@davidtwco davidtwco added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 23, 2022
src/test/ui/parser/else-no-if.stderr Outdated Show resolved Hide resolved
src/test/ui/parser/else-no-if.stderr Outdated Show resolved Hide resolved
@compiler-errors
Copy link
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 24, 2022
@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented May 24, 2022

📌 Commit 9be37b2 has been approved by davidtwco

@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 May 24, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request May 24, 2022
…, r=davidtwco

Parse expression after `else` as a condition if followed by `{`

Fixes rust-lang#49361.

Two things:
1. This wording needs help. I can never find a natural/intuitive phrasing when I write diagnostics 😅
2. Do we even want to show the "wrap in braces" case? I would assume most of the time the "add an `if`" case is the right one.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request May 24, 2022
…, r=davidtwco

Parse expression after `else` as a condition if followed by `{`

Fixes rust-lang#49361.

Two things:
1. This wording needs help. I can never find a natural/intuitive phrasing when I write diagnostics 😅
2. Do we even want to show the "wrap in braces" case? I would assume most of the time the "add an `if`" case is the right one.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 24, 2022
Rollup of 4 pull requests

Successful merges:

 - rust-lang#97288 (Lifetime variance fixes for rustdoc)
 - rust-lang#97298 (Parse expression after `else` as a condition if followed by `{`)
 - rust-lang#97308 (Stabilize `cell_filter_map`)
 - rust-lang#97321 (explain how to turn integers into fn ptrs)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 0531521 into rust-lang:master May 24, 2022
@rustbot rustbot added this to the 1.63.0 milestone May 24, 2022
Comment on lines +2284 to +2289
Ok(cond)
// If it's not a free-standing expression, and is followed by a block,
// then it's very likely the condition to an `else if`.
if self.check(&TokenKind::OpenDelim(Delimiter::Brace))
&& classify::expr_requires_semi_to_be_stmt(&cond) =>
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Given all the checks you have here...

Comment on lines +2297 to +2304
).multipart_suggestion(
"... otherwise, place this expression inside of a block if it is not an `if` condition",
vec![
(cond.span.shrink_to_lo(), "{ ".to_string()),
(cond.span.shrink_to_hi(), " }".to_string()),
],
Applicability::MaybeIncorrect,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

...I would have likely skipped this suggestion :)

.span_label(else_span, "expected an `if` or a block after this `else`")
.span_suggestion(
cond.span.shrink_to_lo(),
"add an `if` if this is the condition to an chained `if` statement after the `else`",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"add an `if` if this is the condition to an chained `if` statement after the `else`",
"add an `if` if this is the condition to a chained `if` statement after the `else`",

Copy link
Member Author

Choose a reason for hiding this comment

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

#97370 addresses this suggestion and @estebank's above.

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request May 25, 2022
…an-DPC

Minor improvement on else-no-if diagnostic

Don't suggest wrapping in block since it's highly likely to be a missing `if` after `else`. Also rework message a bit (open to further suggestions).

cc: rust-lang#97298 (comment)

r? `@estebank`
@compiler-errors compiler-errors deleted the if-else-stmt-braces branch August 11, 2023 20:02
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.

Confusing suggestion when if is missing in else if
8 participants