Skip to content

try_block cannot be used everywhere an expression can #56828

Closed
@rodrigorc

Description

@rodrigorc

The following code fails to compile (rustc --edition=2018):

#![feature(try_blocks)]
fn main() {
    match try { 0 } {
        None => (),
        Some(_) => ()
    };
}

The compiler error is:

error: expected expression, found reserved keyword `try`
 --> test.rs:3:11
  |
3 |     match try { 0 } {
  |     ----- ^^^ expected expression
  |     |
  |     while parsing this match expression

Adding parentheses or braces, it will compile fine:

    match ( try { 0 } ) { ... } //warning: unnecessary parentheses around `match` head expression
    match { try { 0 } } { ... }

The same issue happens with other constructs such as:

    if let Some(_) = try { 0 } { }
    for _ in try { 0 } { } //should fail because of "cannot infer type", not "expected expression"

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.F-try_blocks`#![feature(try_blocks)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions