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

Suggest adding a => after match guard #94932

Conversation

TaKO8Ki
Copy link
Member

@TaKO8Ki TaKO8Ki commented Mar 14, 2022

closes #78585

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

r? @estebank

(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 Mar 14, 2022
@TaKO8Ki TaKO8Ki marked this pull request as draft March 14, 2022 14:30
@rust-log-analyzer

This comment has been minimized.

@fmease
Copy link
Member

fmease commented Mar 14, 2022

Does the following still parse after your change?

fn main() {
    let x = Some(It { a: 1 });
    match x {
        Some(x) if x == It { a: 0 } => {}
        _ => {}
    }
}
#[derive(PartialEq, Eq)]
struct It { a: i32 }

@TaKO8Ki TaKO8Ki changed the title Suggest adding a => after match guard [WIP] Suggest adding a => after match guard Mar 14, 2022
@TaKO8Ki TaKO8Ki changed the title [WIP] Suggest adding a => after match guard Suggest adding a => after match guard Mar 14, 2022
@@ -2617,6 +2626,26 @@ impl<'a> Parser<'a> {
}
}

fn parse_match_guard_cond(&mut self) -> PResult<'a, P<Expr>> {
let snapshot = self.clone();
Copy link
Contributor

Choose a reason for hiding this comment

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

The only issue I'm seeing here is that this pessimises the parsing of every match arm. In this case I think it is reasonable to instead catch the problem in line 2499 and only in the error arm try to conditionally parse the match arm body. Probably also do that for the substitution cases handled right below.

Comment on lines +2632 to +2637
let (is_lhs_struct, is_rhs_struct) = match cond.kind {
ExprKind::Binary(_, ref lhs, ref rhs) => {
(matches!(lhs.kind, ExprKind::Struct(..)), matches!(rhs.kind, ExprKind::Struct(..)))
}
_ => (false, false),
};
Copy link
Contributor

Choose a reason for hiding this comment

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

As you recognize here, we might want to also detect when a struct literal was parsed that instead was supposed to be an ident and the match arm body, but that is not as critical, I think.

@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 Apr 3, 2022
@bors
Copy link
Contributor

bors commented Jun 26, 2022

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

@Dylan-DPC
Copy link
Member

@TaKO8Ki any updates on this?

@Dylan-DPC
Copy link
Member

Closing this as inactive

@Dylan-DPC Dylan-DPC closed this Nov 12, 2022
@Dylan-DPC Dylan-DPC added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. 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 parse error for missing => after match guard
9 participants