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

Let-else not checking for curly brace of inline const before else #118859

Closed
dtolnay opened this issue Dec 12, 2023 · 1 comment · Fixed by #118880
Closed

Let-else not checking for curly brace of inline const before else #118859

dtolnay opened this issue Dec 12, 2023 · 1 comment · Fixed by #118880
Labels
A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) F-let_else Issues related to let-else statements (RFC 3137) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Dec 12, 2023

#![feature(inline_const)]

fn main() {
    let false = const {
        true
    } else {
        return;
    };
}

I believe the intention in let-else is that this code should be considered syntactically invalid, but it compiles successfully today.

If you replace const with unsafe, it correctly fails to parse.

error: right curly brace `}` before `else` in a `let...else` statement not allowed
 --> src/main.rs:6:5
  |
6 |     } else {
  |     ^
  |
help: wrap the expression in parentheses
  |
4 ~     let false = (unsafe {
5 |         true
6 ~     }) else {
  |
@dtolnay dtolnay added the C-bug Category: This is a bug. label Dec 12, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 12, 2023
@dtolnay dtolnay added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) F-let_else Issues related to let-else statements (RFC 3137) E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-parser Area: The parsing of Rust source code to an AST labels Dec 12, 2023
@dtolnay
Copy link
Member Author

dtolnay commented Dec 12, 2023

I believe here is the missing case for ExprKind::ConstBlock:

Gen(..) | Block(..) | ForLoop(..) | If(..) | Loop(..) | Match(..) | Struct(..)
| TryBlock(..) | While(..) => break Some(expr),

This should be changed to an exhaustive match so that future new ExprKind variants are forced to touch this code.

Some other unhandled cases that can end in }:

  • ExprKind::Become
  • ExprKind::FormatArgs
  • ExprKind::IncludedBytes
  • ExprKind::InlineAsm
  • ExprKind::MacCall
  • ExprKind::OffsetOf
  • ExprKind::Yeet

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 12, 2023
@bors bors closed this as completed in 6269bf1 Dec 17, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 17, 2023
Rollup merge of rust-lang#118880 - GearsDatapacks:issue-118859-fix, r=compiler-errors

More expressions correctly are marked to end with curly braces

Fixes rust-lang#118859, and replaces the mentioned match statement with an exhaustive list, so that this code doesn't get overlooked in the future
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) F-let_else Issues related to let-else statements (RFC 3137) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants