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

Add catch {} to AST #39921

Merged
merged 3 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Temporarily prefix catch block with do keyword
  • Loading branch information
cramertj committed Mar 12, 2017
commit ea4e8b0a81171d4d6c28a61fa0d1c4d74837bb65
18 changes: 5 additions & 13 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,6 @@ impl<'a> Parser<'a> {
}
}

pub fn error_if_typename_is_catch(&mut self, ident: ast::Ident) {
if ident.name == keywords::Catch.name() {
self.span_err(self.span, "cannot use `catch` as the name of a type");
}
}

/// Check if the next token is `tok`, and return `true` if so.
///
/// This method will automatically add `tok` to `expected_tokens` if `tok` is not
Expand Down Expand Up @@ -2280,6 +2274,7 @@ impl<'a> Parser<'a> {
attrs);
}
if self.is_catch_expr() {
assert!(self.eat_keyword(keywords::Do));
assert!(self.eat_keyword(keywords::Catch));
let lo = self.prev_span.lo;
return self.parse_catch_expr(lo, attrs);
Expand Down Expand Up @@ -3103,7 +3098,7 @@ impl<'a> Parser<'a> {
Ok(self.mk_expr(span_lo, hi, ExprKind::Loop(body, opt_ident), attrs))
}

/// Parse a `catch {...}` expression (`catch` token already eaten)
/// Parse a `do catch {...}` expression (`do catch` token already eaten)
pub fn parse_catch_expr(&mut self, span_lo: BytePos, mut attrs: ThinVec<Attribute>)
-> PResult<'a, P<Expr>>
{
Expand Down Expand Up @@ -3721,8 +3716,9 @@ impl<'a> Parser<'a> {
}

fn is_catch_expr(&mut self) -> bool {
self.token.is_keyword(keywords::Catch) &&
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) &&
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)) &&

// prevent `while catch {} {}`, `if catch {} {} else {}`, etc.
!self.restrictions.contains(Restrictions::RESTRICTION_NO_STRUCT_LITERAL)
Expand Down Expand Up @@ -4904,7 +4900,6 @@ impl<'a> Parser<'a> {
/// Parse struct Foo { ... }
fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
let class_name = self.parse_ident()?;
self.error_if_typename_is_catch(class_name);

let mut generics = self.parse_generics()?;

Expand Down Expand Up @@ -4955,7 +4950,6 @@ impl<'a> Parser<'a> {
/// Parse union Foo { ... }
fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
let class_name = self.parse_ident()?;
self.error_if_typename_is_catch(class_name);

let mut generics = self.parse_generics()?;

Expand Down Expand Up @@ -5473,7 +5467,6 @@ impl<'a> Parser<'a> {
let struct_def;
let mut disr_expr = None;
let ident = self.parse_ident()?;
self.error_if_typename_is_catch(ident);
if self.check(&token::OpenDelim(token::Brace)) {
// Parse a struct variant.
all_nullary = false;
Expand Down Expand Up @@ -5515,7 +5508,6 @@ impl<'a> Parser<'a> {
/// Parse an "enum" declaration
fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
let id = self.parse_ident()?;
self.error_if_typename_is_catch(id);
let mut generics = self.parse_generics()?;
generics.where_clause = self.parse_where_clause()?;
self.expect(&token::OpenDelim(token::Brace))?;
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn ident_can_begin_expr(ident: ast::Ident) -> bool {
!ident_token.is_any_keyword() ||
ident_token.is_path_segment_keyword() ||
[
keywords::Do.name(),
keywords::Box.name(),
keywords::Break.name(),
keywords::Continue.name(),
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,7 @@ impl<'a> State<'a> {
word(&mut self.s, "?")?
}
ast::ExprKind::Catch(ref blk) => {
self.head("catch")?;
self.head("do catch")?;
space(&mut self.s)?;
self.print_block_with_attrs(&blk, attrs)?
}
Expand Down
15 changes: 0 additions & 15 deletions src/test/compile-fail/catch-empty-struct-name.rs

This file was deleted.

17 changes: 0 additions & 17 deletions src/test/compile-fail/catch-enum-variant.rs

This file was deleted.

15 changes: 0 additions & 15 deletions src/test/compile-fail/catch-struct-name.rs

This file was deleted.

15 changes: 0 additions & 15 deletions src/test/compile-fail/catch-tuple-struct-name.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/compile-fail/feature-gate-catch_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

pub fn main() {
let catch_result = catch { //~ ERROR `catch` expression is experimental
let catch_result = do catch { //~ ERROR `catch` expression is experimental
let x = 5;
x
};
Expand Down
4 changes: 3 additions & 1 deletion src/test/run-pass/catch-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

#![feature(catch_expr)]

struct catch {}

pub fn main() {
let catch_result = catch {
let catch_result = do catch {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seeing as how there is special code around restrictions, shouldn't we have a test like while do catch { ... } and show that it results in an error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can add one.

let x = 5;
x
};
Expand Down