Skip to content

Commit ea4e8b0

Browse files
committed
Temporarily prefix catch block with do keyword
1 parent d95c543 commit ea4e8b0

9 files changed

+11
-78
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,6 @@ impl<'a> Parser<'a> {
602602
}
603603
}
604604

605-
pub fn error_if_typename_is_catch(&mut self, ident: ast::Ident) {
606-
if ident.name == keywords::Catch.name() {
607-
self.span_err(self.span, "cannot use `catch` as the name of a type");
608-
}
609-
}
610-
611605
/// Check if the next token is `tok`, and return `true` if so.
612606
///
613607
/// This method will automatically add `tok` to `expected_tokens` if `tok` is not
@@ -2280,6 +2274,7 @@ impl<'a> Parser<'a> {
22802274
attrs);
22812275
}
22822276
if self.is_catch_expr() {
2277+
assert!(self.eat_keyword(keywords::Do));
22832278
assert!(self.eat_keyword(keywords::Catch));
22842279
let lo = self.prev_span.lo;
22852280
return self.parse_catch_expr(lo, attrs);
@@ -3103,7 +3098,7 @@ impl<'a> Parser<'a> {
31033098
Ok(self.mk_expr(span_lo, hi, ExprKind::Loop(body, opt_ident), attrs))
31043099
}
31053100

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

37233718
fn is_catch_expr(&mut self) -> bool {
3724-
self.token.is_keyword(keywords::Catch) &&
3725-
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) &&
3719+
self.token.is_keyword(keywords::Do) &&
3720+
self.look_ahead(1, |t| t.is_keyword(keywords::Catch)) &&
3721+
self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace)) &&
37263722

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

49094904
let mut generics = self.parse_generics()?;
49104905

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

49604954
let mut generics = self.parse_generics()?;
49614955

@@ -5473,7 +5467,6 @@ impl<'a> Parser<'a> {
54735467
let struct_def;
54745468
let mut disr_expr = None;
54755469
let ident = self.parse_ident()?;
5476-
self.error_if_typename_is_catch(ident);
54775470
if self.check(&token::OpenDelim(token::Brace)) {
54785471
// Parse a struct variant.
54795472
all_nullary = false;
@@ -5515,7 +5508,6 @@ impl<'a> Parser<'a> {
55155508
/// Parse an "enum" declaration
55165509
fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
55175510
let id = self.parse_ident()?;
5518-
self.error_if_typename_is_catch(id);
55195511
let mut generics = self.parse_generics()?;
55205512
generics.where_clause = self.parse_where_clause()?;
55215513
self.expect(&token::OpenDelim(token::Brace))?;

src/libsyntax/parse/token.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ fn ident_can_begin_expr(ident: ast::Ident) -> bool {
8686
!ident_token.is_any_keyword() ||
8787
ident_token.is_path_segment_keyword() ||
8888
[
89+
keywords::Do.name(),
8990
keywords::Box.name(),
9091
keywords::Break.name(),
9192
keywords::Continue.name(),

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,7 @@ impl<'a> State<'a> {
22802280
word(&mut self.s, "?")?
22812281
}
22822282
ast::ExprKind::Catch(ref blk) => {
2283-
self.head("catch")?;
2283+
self.head("do catch")?;
22842284
space(&mut self.s)?;
22852285
self.print_block_with_attrs(&blk, attrs)?
22862286
}

src/test/compile-fail/catch-empty-struct-name.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/test/compile-fail/catch-enum-variant.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/test/compile-fail/catch-struct-name.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/test/compile-fail/catch-tuple-struct-name.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/test/compile-fail/feature-gate-catch_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
pub fn main() {
12-
let catch_result = catch { //~ ERROR `catch` expression is experimental
12+
let catch_result = do catch { //~ ERROR `catch` expression is experimental
1313
let x = 5;
1414
x
1515
};

src/test/run-pass/catch-expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
#![feature(catch_expr)]
1212

13+
struct catch {}
14+
1315
pub fn main() {
14-
let catch_result = catch {
16+
let catch_result = do catch {
1517
let x = 5;
1618
x
1719
};

0 commit comments

Comments
 (0)