Skip to content

Commit

Permalink
Suggest try if someone uses do catch
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed Aug 19, 2018
1 parent 91967a8 commit 817efc2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -2386,6 +2386,11 @@ impl<'a> Parser<'a> {
BlockCheckMode::Unsafe(ast::UserProvided),
attrs);
}
if self.is_do_catch_block() {
let mut db = self.fatal("found removed `do catch` syntax");
db.help("Following RFC #2388, the new non-placeholder syntax is `try`");
return Err(db);
}
if self.is_try_block() {
let lo = self.span;
assert!(self.eat_keyword(keywords::Try));
Expand Down Expand Up @@ -4406,6 +4411,13 @@ impl<'a> Parser<'a> {
)
}

fn is_do_catch_block(&mut self) -> bool {
self.token.is_keyword(keywords::Do) &&
self.look_ahead(1, |t| t.is_keyword(keywords::Catch)) &&
self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace)) &&
!self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
}

fn is_try_block(&mut self) -> bool {
self.token.is_keyword(keywords::Try) &&
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) &&
Expand Down
17 changes: 17 additions & 0 deletions src/test/parse-fail/do-catch-suggests-try.rs
@@ -0,0 +1,17 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -Z parse-only

fn main() {
let _: Option<()> = do catch {};
//~^ ERROR found removed `do catch` syntax
//~^^ HELP Following RFC #2388, the new non-placeholder syntax is `try`
}

0 comments on commit 817efc2

Please sign in to comment.