Skip to content

do catch should also be allowed before blocks #42528

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

Closed
qnighy opened this issue Jun 8, 2017 · 1 comment
Closed

do catch should also be allowed before blocks #42528

qnighy opened this issue Jun 8, 2017 · 1 comment
Labels
A-parser Area: The lexing & parsing of Rust source code to an AST C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@qnighy
Copy link
Contributor

qnighy commented Jun 8, 2017

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.

@Mark-Simulacrum Mark-Simulacrum added the A-parser Area: The lexing & parsing of Rust source code to an AST label Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 27, 2017
@comex
Copy link
Contributor

comex commented Apr 15, 2020

I guess this is the same as #56828, which was just closed, so this should be closed as well.

@bjorn3 bjorn3 closed this as completed May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The lexing & parsing of Rust source code to an AST C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

4 participants