Skip to content

do catch should also be allowed before blocks #42528

Closed
@qnighy

Description

@qnighy

Currently do catch can't appear before blocks.

#![feature(catch_expr)]
fn main() {
    if let Ok(x) = do catch { Err("foo") as Result<i32, _> } {
        println!("x = {}", x);
    }
}
rustc 1.19.0-nightly (76242aebb 2017-06-06)
error: expected expression, found reserved keyword `do`
 --> <anon>:3:20
  |
3 |     if let Ok(x) = do catch { Err("foo") as Result<i32, _> } {
  |                    ^^

error: aborting due to previous error(s)

One has to enclose the do catch with parentheses.

#![feature(catch_expr)]
fn main() {
    if let Ok(x) = (do catch { Err("foo") as Result<i32, _> }) {
        println!("x = {}", x);
    }
}
rustc 1.19.0-nightly (76242aebb 2017-06-06)
warning: unnecessary parentheses around `if let` head expression
 --> <anon>:3:20
  |
3 |     if let Ok(x) = (do catch { Err("foo") as Result<i32, _> }) {
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_parens)] on by default

I think it is inconsistent with the behavior like this:

fn main() {
    // compiles.
    if if true { true } else { false } {
        
    }
}

The RESTRICTION_NO_STRUCT_LITERAL is mainly used to distinguish if x == (A { .. }) { .. } from if x == A { .. } and unnecessary for do catch {} where {} definitely comes here.

At least the emitted warning for (do catch { .. }) is wrong and the emitted error for do catch { .. } is unhelpful for now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an ASTC-feature-requestCategory: A feature request, i.e: not implemented / a PR.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions