Skip to content

Commit d83687f

Browse files
committed
Fix can_begin_expr keyword behavior
1 parent 1283c02 commit d83687f

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/libsyntax/parse/token.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ impl Lit {
8080
}
8181
}
8282

83+
fn ident_can_begin_expr(ident: ast::Ident) -> bool {
84+
let ident_token: Token = Ident(ident);
85+
86+
!ident_token.is_any_keyword() ||
87+
ident_token.is_path_segment_keyword() ||
88+
[
89+
keywords::Box.name(),
90+
keywords::Break.name(),
91+
keywords::Continue.name(),
92+
keywords::False.name(),
93+
keywords::For.name(),
94+
keywords::If.name(),
95+
keywords::Loop.name(),
96+
keywords::Match.name(),
97+
keywords::Move.name(),
98+
keywords::Return.name(),
99+
keywords::True.name(),
100+
keywords::Unsafe.name(),
101+
keywords::While.name(),
102+
].contains(&ident.name)
103+
}
104+
83105
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug)]
84106
pub enum Token {
85107
/* Expression-operator symbols. */
@@ -163,7 +185,7 @@ impl Token {
163185
pub fn can_begin_expr(&self) -> bool {
164186
match *self {
165187
OpenDelim(..) => true,
166-
Ident(..) => true,
188+
Ident(ident) => ident_can_begin_expr(ident),
167189
Literal(..) => true,
168190
Not => true,
169191
BinOp(Minus) => true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn main() {
12+
13+
return;
14+
return ();
15+
return as ();
16+
return return as ();
17+
return return return;
18+
19+
return if true {
20+
()
21+
} else {
22+
()
23+
};
24+
25+
loop {
26+
return break as ();
27+
}
28+
29+
return enum; //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `enum`
30+
}

0 commit comments

Comments
 (0)