Skip to content

Commit b0f880d

Browse files
committed
in which leading zeroes on tuple-struct accesses are abjured
Resolves #47073.
1 parent 54d7285 commit b0f880d

3 files changed

+48
-3
lines changed

src/libsyntax/parse/parser.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,7 @@ impl<'a> Parser<'a> {
25922592
token::Ident(..) => {
25932593
e = self.parse_dot_suffix(e, lo)?;
25942594
}
2595-
token::Literal(token::Integer(n), suf) => {
2595+
token::Literal(token::Integer(index_ident), suf) => {
25962596
let sp = self.span;
25972597

25982598
// A tuple index may not have a suffix
@@ -2602,16 +2602,25 @@ impl<'a> Parser<'a> {
26022602
hi = self.span;
26032603
self.bump();
26042604

2605-
let index = n.as_str().parse::<usize>().ok();
2605+
let invalid_msg = "invalid tuple or struct index";
2606+
2607+
let index = index_ident.as_str().parse::<usize>().ok();
26062608
match index {
26072609
Some(n) => {
2610+
if n.to_string() != index_ident.as_str() {
2611+
let mut err = self.struct_span_err(self.prev_span, invalid_msg);
2612+
err.span_suggestion(self.prev_span,
2613+
"try simplifying the index",
2614+
n.to_string());
2615+
err.emit();
2616+
}
26082617
let id = respan(dot_span.to(hi), n);
26092618
let field = self.mk_tup_field(e, id);
26102619
e = self.mk_expr(lo.to(hi), field, ThinVec::new());
26112620
}
26122621
None => {
26132622
let prev_span = self.prev_span;
2614-
self.span_err(prev_span, "invalid tuple or tuple struct index");
2623+
self.span_err(prev_span, invalid_msg);
26152624
}
26162625
}
26172626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 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+
type Guilty = bool;
12+
type FineDollars = u32;
13+
14+
struct Verdict(Guilty, Option<FineDollars>);
15+
16+
fn main() {
17+
let justice = Verdict(true, Some(2718));
18+
let _condemned = justice.00;
19+
//~^ ERROR invalid tuple or struct index
20+
let _punishment = justice.001;
21+
//~^ ERROR invalid tuple or struct index
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: invalid tuple or struct index
2+
--> $DIR/issue-47073-zero-padded-tuple-struct-indices.rs:18:30
3+
|
4+
18 | let _condemned = justice.00;
5+
| ^^ help: try simplifying the index: `0`
6+
7+
error: invalid tuple or struct index
8+
--> $DIR/issue-47073-zero-padded-tuple-struct-indices.rs:20:31
9+
|
10+
20 | let _punishment = justice.001;
11+
| ^^^ help: try simplifying the index: `1`
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)