Skip to content

Commit f662478

Browse files
committed
Parse numeric fields in struct expressions and patterns
1 parent 59be332 commit f662478

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/libsyntax/parse/parser.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -2009,10 +2009,19 @@ impl<'a> Parser<'a> {
20092009
}
20102010
}
20112011

2012+
pub fn parse_field_name(&mut self) -> PResult<'a, Ident> {
2013+
if let token::Literal(token::Integer(name), None) = self.token {
2014+
self.bump();
2015+
Ok(Ident::with_empty_ctxt(name))
2016+
} else {
2017+
self.parse_ident()
2018+
}
2019+
}
2020+
20122021
/// Parse ident COLON expr
20132022
pub fn parse_field(&mut self) -> PResult<'a, Field> {
20142023
let lo = self.span.lo;
2015-
let i = self.parse_ident()?;
2024+
let i = self.parse_field_name()?;
20162025
let hi = self.last_span.hi;
20172026
self.expect(&token::Colon)?;
20182027
let e = self.parse_expr()?;
@@ -3508,7 +3517,7 @@ impl<'a> Parser<'a> {
35083517
// Check if a colon exists one ahead. This means we're parsing a fieldname.
35093518
let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
35103519
// Parsing a pattern of the form "fieldname: pat"
3511-
let fieldname = self.parse_ident()?;
3520+
let fieldname = self.parse_field_name()?;
35123521
self.bump();
35133522
let pat = self.parse_pat()?;
35143523
hi = pat.span.hi;
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 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+
#![feature(relaxed_adts)]
12+
13+
struct S(u8, u16);
14+
15+
fn main() {
16+
let s = S{0b1: 10, 0: 11}; //~ ERROR structure `S` has no field named `0b1`
17+
match s {
18+
S{0: a, 0x1: b, ..} => {} //~ ERROR does not have a field named `0x1`
19+
}
20+
}

src/test/run-pass/numeric-fields.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 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+
#![feature(relaxed_adts)]
12+
13+
struct S(u8, u16);
14+
15+
fn main() {
16+
let s = S{1: 10, 0: 11};
17+
match s {
18+
S{0: a, 1: b, ..} => {
19+
assert_eq!(a, 11);
20+
assert_eq!(b, 10);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)