Skip to content

Commit 5eca3c2

Browse files
committed
parser: More refactoring of restricted value name checking
1 parent 21dc416 commit 5eca3c2

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
lines changed

src/librustsyntax/parse/common.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,18 @@ fn check_restricted_keywords(p: parser) {
9595
alt p.token {
9696
token::IDENT(_, false) {
9797
let w = token_to_str(p.reader, p.token);
98-
if is_restricted_keyword(p, w) {
99-
p.fatal("found `" + w + "` in expression position");
100-
}
98+
check_restricted_keywords_(p, w);
10199
}
102100
_ { }
103101
}
104102
}
105103

104+
fn check_restricted_keywords_(p: parser, w: ast::ident) {
105+
if is_restricted_keyword(p, w) {
106+
p.fatal("found `" + w + "` in restricted position");
107+
}
108+
}
109+
106110
fn expect_gt(p: parser) {
107111
if p.token == token::GT {
108112
p.bump();

src/librustsyntax/parse/parser.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,11 @@ fn parse_pat(p: parser) -> @ast::pat {
13841384
}
13851385

13861386
let lo1 = p.last_span.lo;
1387-
let fieldname = parse_ident(p);
1387+
let fieldname = if p.look_ahead(1u) == token::COLON {
1388+
parse_ident(p)
1389+
} else {
1390+
parse_value_ident(p)
1391+
};
13881392
let hi1 = p.last_span.lo;
13891393
let fieldpath = ast_util::ident_to_path(mk_sp(lo1, hi1),
13901394
fieldname);
@@ -1393,9 +1397,6 @@ fn parse_pat(p: parser) -> @ast::pat {
13931397
p.bump();
13941398
subpat = parse_pat(p);
13951399
} else {
1396-
if is_restricted_keyword(p, fieldname) {
1397-
p.fatal("found `" + fieldname + "` in binding position");
1398-
}
13991400
subpat = @{id: p.get_id(),
14001401
node: ast::pat_ident(fieldpath, none),
14011402
span: mk_sp(lo, hi)};
@@ -2147,9 +2148,7 @@ fn parse_item_enum(p: parser, attrs: [ast::attribute]) -> @ast::item {
21472148
let mut variants: [ast::variant] = [];
21482149
// Newtype syntax
21492150
if p.token == token::EQ {
2150-
if is_restricted_keyword(p, id) {
2151-
p.fatal("found `" + id + "` in enum constructor position");
2152-
}
2151+
check_restricted_keywords_(p, id);
21532152
p.bump();
21542153
let ty = parse_ty(p, false);
21552154
expect(p, token::SEMI);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
fn false() { } //! ERROR found `false` in expression position
1+
fn false() { } //! ERROR found `false` in restricted position
22
fn main() { }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
fn true() { } //! ERROR found `true` in expression position
1+
fn true() { } //! ERROR found `true` in restricted position
22
fn main() { }

src/test/compile-fail/restricted-keyword1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// error-pattern:found `let` in binding position
1+
// error-pattern:found `let` in restricted position
22

33
fn main() {
44
alt true {

src/test/compile-fail/restricted-keyword2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// error-pattern:found `let` in enum constructor position
1+
// error-pattern:found `let` in restricted position
22

33
fn main() {
44
enum let = int;

0 commit comments

Comments
 (0)