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

Struct literals are forbidden in MatchExpressions (feature(if_let_guard)) #93817

Closed
pushkine opened this issue Feb 9, 2022 · 3 comments · Fixed by #115371
Closed

Struct literals are forbidden in MatchExpressions (feature(if_let_guard)) #93817

pushkine opened this issue Feb 9, 2022 · 3 comments · Fixed by #115371
Assignees
Labels
C-bug Category: This is a bug. F-if_let_guard `#![feature(if_let_guard)]` T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@pushkine
Copy link

pushkine commented Feb 9, 2022

Bug Report

🕗 Version & Regression Information

nightly

🧪 Features

  • if_let_guard

💻 Code

Playground Link

#![feature(if_let_guard)]

struct Bar {}
struct Foo { a: u32 }

fn main() {
    match 0 {
        _ if let _ = Bar {} => {}
        _ if let _ = Foo { a: 0 } => {}
        _ => {}
    }
}

🙁 Actual behavior

  _ if let _ = Bar {} => {}
//                 ^ error: expected one of `!`, `.`, `::`, `=>`, `?`, or an operator
  _ if let _ = Foo { a: 0 } => {}
//             ^^^^^^^^^^^^ error: struct literals are not allowed here
//                           help: surround the struct literal with parentheses

🙂 Expected behavior

No error

@pushkine pushkine added the C-bug Category: This is a bug. label Feb 9, 2022
@scottmcm scottmcm added F-if_let_guard `#![feature(if_let_guard)]` T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Feb 10, 2022
@scottmcm
Copy link
Member

scottmcm commented Feb 10, 2022

This is probably using the same parsing rules as normal if let expressions, where this behaviour is essential for avoiding ambiguity. I don't know how to weigh the tradeoffs between "it's the same rules" and "we can be more flexible here".

(Nice to see the help for it, at least!)

@joshtriplett
Copy link
Member

I'm also not sure we should be more flexible here, given the potential difficulty of reading it in more complex cases.

@pushkine
Copy link
Author

pushkine commented Feb 11, 2022

given the potential difficulty of reading it in more complex cases

How so? _ if Bar {} => {} is already valid, the function reading the let expr is just hardcoded to not parse a struct expr

@matthewjasper matthewjasper self-assigned this Aug 24, 2023
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Sep 5, 2023
…, r=cjgillot

Make if let guard parsing consistent with normal guards

- Add tests that struct expressions are not allowed in `if let` and `while let` (no change, consistent with `if` and `while`)
- Allow struct expressions in `if let` guards (consistent with `if` guards).

r? `@cjgillot`

Closes rust-lang#93817
cc rust-lang#51114
@bors bors closed this as completed in 25283f4 Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-if_let_guard `#![feature(if_let_guard)]` T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants