Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 892c18b

Browse files
authoredMar 27, 2019
Rollup merge of rust-lang#59421 - estebank:tuple-index-suffix, r=petrochenkov
Reject integer suffix when tuple indexing Fix rust-lang#59418. r? @varkor
2 parents 8d5bb65 + 8d1cc72 commit 892c18b

File tree

5 files changed

+120
-74
lines changed

5 files changed

+120
-74
lines changed
 

‎src/libsyntax/parse/parser.rs

+52-50
Original file line numberDiff line numberDiff line change
@@ -1119,9 +1119,8 @@ impl<'a> Parser<'a> {
11191119
if text.is_empty() {
11201120
self.span_bug(sp, "found empty literal suffix in Some")
11211121
}
1122-
let msg = format!("{} with a suffix is invalid", kind);
1123-
self.struct_span_err(sp, &msg)
1124-
.span_label(sp, msg)
1122+
self.struct_span_err(sp, &format!("suffixes on {} are invalid", kind))
1123+
.span_label(sp, format!("invalid suffix `{}`", text))
11251124
.emit();
11261125
}
11271126
}
@@ -2150,7 +2149,7 @@ impl<'a> Parser<'a> {
21502149

21512150
if suffix_illegal {
21522151
let sp = self.span;
2153-
self.expect_no_suffix(sp, lit.literal_name(), suf)
2152+
self.expect_no_suffix(sp, &format!("a {}", lit.literal_name()), suf)
21542153
}
21552154

21562155
result.unwrap()
@@ -2481,7 +2480,8 @@ impl<'a> Parser<'a> {
24812480
}
24822481

24832482
fn parse_field_name(&mut self) -> PResult<'a, Ident> {
2484-
if let token::Literal(token::Integer(name), None) = self.token {
2483+
if let token::Literal(token::Integer(name), suffix) = self.token {
2484+
self.expect_no_suffix(self.span, "a tuple index", suffix);
24852485
self.bump();
24862486
Ok(Ident::new(name, self.prev_span))
24872487
} else {
@@ -3185,51 +3185,53 @@ impl<'a> Parser<'a> {
31853185
// expr.f
31863186
if self.eat(&token::Dot) {
31873187
match self.token {
3188-
token::Ident(..) => {
3189-
e = self.parse_dot_suffix(e, lo)?;
3190-
}
3191-
token::Literal(token::Integer(name), _) => {
3192-
let span = self.span;
3193-
self.bump();
3194-
let field = ExprKind::Field(e, Ident::new(name, span));
3195-
e = self.mk_expr(lo.to(span), field, ThinVec::new());
3196-
}
3197-
token::Literal(token::Float(n), _suf) => {
3198-
self.bump();
3199-
let fstr = n.as_str();
3200-
let mut err = self.diagnostic()
3201-
.struct_span_err(self.prev_span, &format!("unexpected token: `{}`", n));
3202-
err.span_label(self.prev_span, "unexpected token");
3203-
if fstr.chars().all(|x| "0123456789.".contains(x)) {
3204-
let float = match fstr.parse::<f64>().ok() {
3205-
Some(f) => f,
3206-
None => continue,
3207-
};
3208-
let sugg = pprust::to_string(|s| {
3209-
use crate::print::pprust::PrintState;
3210-
s.popen()?;
3211-
s.print_expr(&e)?;
3212-
s.s.word( ".")?;
3213-
s.print_usize(float.trunc() as usize)?;
3214-
s.pclose()?;
3215-
s.s.word(".")?;
3216-
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
3217-
});
3218-
err.span_suggestion(
3219-
lo.to(self.prev_span),
3220-
"try parenthesizing the first index",
3221-
sugg,
3222-
Applicability::MachineApplicable
3223-
);
3188+
token::Ident(..) => {
3189+
e = self.parse_dot_suffix(e, lo)?;
32243190
}
3225-
return Err(err);
3191+
token::Literal(token::Integer(name), suffix) => {
3192+
let span = self.span;
3193+
self.bump();
3194+
let field = ExprKind::Field(e, Ident::new(name, span));
3195+
e = self.mk_expr(lo.to(span), field, ThinVec::new());
3196+
3197+
self.expect_no_suffix(span, "a tuple index", suffix);
3198+
}
3199+
token::Literal(token::Float(n), _suf) => {
3200+
self.bump();
3201+
let fstr = n.as_str();
3202+
let mut err = self.diagnostic()
3203+
.struct_span_err(self.prev_span, &format!("unexpected token: `{}`", n));
3204+
err.span_label(self.prev_span, "unexpected token");
3205+
if fstr.chars().all(|x| "0123456789.".contains(x)) {
3206+
let float = match fstr.parse::<f64>().ok() {
3207+
Some(f) => f,
3208+
None => continue,
3209+
};
3210+
let sugg = pprust::to_string(|s| {
3211+
use crate::print::pprust::PrintState;
3212+
s.popen()?;
3213+
s.print_expr(&e)?;
3214+
s.s.word( ".")?;
3215+
s.print_usize(float.trunc() as usize)?;
3216+
s.pclose()?;
3217+
s.s.word(".")?;
3218+
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
3219+
});
3220+
err.span_suggestion(
3221+
lo.to(self.prev_span),
3222+
"try parenthesizing the first index",
3223+
sugg,
3224+
Applicability::MachineApplicable
3225+
);
3226+
}
3227+
return Err(err);
32263228

3227-
}
3228-
_ => {
3229-
// FIXME Could factor this out into non_fatal_unexpected or something.
3230-
let actual = self.this_token_to_string();
3231-
self.span_err(self.span, &format!("unexpected token: `{}`", actual));
3232-
}
3229+
}
3230+
_ => {
3231+
// FIXME Could factor this out into non_fatal_unexpected or something.
3232+
let actual = self.this_token_to_string();
3233+
self.span_err(self.span, &format!("unexpected token: `{}`", actual));
3234+
}
32333235
}
32343236
continue;
32353237
}
@@ -7827,7 +7829,7 @@ impl<'a> Parser<'a> {
78277829
match self.token {
78287830
token::Literal(token::Str_(s), suf) | token::Literal(token::StrRaw(s, _), suf) => {
78297831
let sp = self.span;
7830-
self.expect_no_suffix(sp, "ABI spec", suf);
7832+
self.expect_no_suffix(sp, "an ABI spec", suf);
78317833
self.bump();
78327834
match abi::lookup(&s.as_str()) {
78337835
Some(abi) => Ok(Some(abi)),
@@ -8648,7 +8650,7 @@ impl<'a> Parser<'a> {
86488650
match self.parse_optional_str() {
86498651
Some((s, style, suf)) => {
86508652
let sp = self.prev_span;
8651-
self.expect_no_suffix(sp, "string literal", suf);
8653+
self.expect_no_suffix(sp, "a string literal", suf);
86528654
Ok((s, style))
86538655
}
86548656
_ => {

‎src/test/ui/parser/bad-lit-suffixes.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33

44
extern
5-
"C"suffix //~ ERROR ABI spec with a suffix is invalid
5+
"C"suffix //~ ERROR suffixes on an ABI spec are invalid
66
fn foo() {}
77

88
extern
9-
"C"suffix //~ ERROR ABI spec with a suffix is invalid
9+
"C"suffix //~ ERROR suffixes on an ABI spec are invalid
1010
{}
1111

1212
fn main() {
13-
""suffix; //~ ERROR string literal with a suffix is invalid
14-
b""suffix; //~ ERROR byte string literal with a suffix is invalid
15-
r#""#suffix; //~ ERROR string literal with a suffix is invalid
16-
br#""#suffix; //~ ERROR byte string literal with a suffix is invalid
17-
'a'suffix; //~ ERROR char literal with a suffix is invalid
18-
b'a'suffix; //~ ERROR byte literal with a suffix is invalid
13+
""suffix; //~ ERROR suffixes on a string literal are invalid
14+
b""suffix; //~ ERROR suffixes on a byte string literal are invalid
15+
r#""#suffix; //~ ERROR suffixes on a string literal are invalid
16+
br#""#suffix; //~ ERROR suffixes on a byte string literal are invalid
17+
'a'suffix; //~ ERROR suffixes on a char literal are invalid
18+
b'a'suffix; //~ ERROR suffixes on a byte literal are invalid
1919

2020
1234u1024; //~ ERROR invalid width `1024` for integer literal
2121
1234i1024; //~ ERROR invalid width `1024` for integer literal

‎src/test/ui/parser/bad-lit-suffixes.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
error: ABI spec with a suffix is invalid
1+
error: suffixes on an ABI spec are invalid
22
--> $DIR/bad-lit-suffixes.rs:5:5
33
|
44
LL | "C"suffix
5-
| ^^^^^^^^^ ABI spec with a suffix is invalid
5+
| ^^^^^^^^^ invalid suffix `suffix`
66

7-
error: ABI spec with a suffix is invalid
7+
error: suffixes on an ABI spec are invalid
88
--> $DIR/bad-lit-suffixes.rs:9:5
99
|
1010
LL | "C"suffix
11-
| ^^^^^^^^^ ABI spec with a suffix is invalid
11+
| ^^^^^^^^^ invalid suffix `suffix`
1212

13-
error: string literal with a suffix is invalid
13+
error: suffixes on a string literal are invalid
1414
--> $DIR/bad-lit-suffixes.rs:13:5
1515
|
1616
LL | ""suffix;
17-
| ^^^^^^^^ string literal with a suffix is invalid
17+
| ^^^^^^^^ invalid suffix `suffix`
1818

19-
error: byte string literal with a suffix is invalid
19+
error: suffixes on a byte string literal are invalid
2020
--> $DIR/bad-lit-suffixes.rs:14:5
2121
|
2222
LL | b""suffix;
23-
| ^^^^^^^^^ byte string literal with a suffix is invalid
23+
| ^^^^^^^^^ invalid suffix `suffix`
2424

25-
error: string literal with a suffix is invalid
25+
error: suffixes on a string literal are invalid
2626
--> $DIR/bad-lit-suffixes.rs:15:5
2727
|
2828
LL | r#""#suffix;
29-
| ^^^^^^^^^^^ string literal with a suffix is invalid
29+
| ^^^^^^^^^^^ invalid suffix `suffix`
3030

31-
error: byte string literal with a suffix is invalid
31+
error: suffixes on a byte string literal are invalid
3232
--> $DIR/bad-lit-suffixes.rs:16:5
3333
|
3434
LL | br#""#suffix;
35-
| ^^^^^^^^^^^^ byte string literal with a suffix is invalid
35+
| ^^^^^^^^^^^^ invalid suffix `suffix`
3636

37-
error: char literal with a suffix is invalid
37+
error: suffixes on a char literal are invalid
3838
--> $DIR/bad-lit-suffixes.rs:17:5
3939
|
4040
LL | 'a'suffix;
41-
| ^^^^^^^^^ char literal with a suffix is invalid
41+
| ^^^^^^^^^ invalid suffix `suffix`
4242

43-
error: byte literal with a suffix is invalid
43+
error: suffixes on a byte literal are invalid
4444
--> $DIR/bad-lit-suffixes.rs:18:5
4545
|
4646
LL | b'a'suffix;
47-
| ^^^^^^^^^^ byte literal with a suffix is invalid
47+
| ^^^^^^^^^^ invalid suffix `suffix`
4848

4949
error: invalid width `1024` for integer literal
5050
--> $DIR/bad-lit-suffixes.rs:20:5

‎src/test/ui/parser/issue-59418.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct X(i32,i32,i32);
2+
3+
fn main() {
4+
let a = X(1, 2, 3);
5+
let b = a.1suffix;
6+
//~^ ERROR suffixes on a tuple index are invalid
7+
println!("{}", b);
8+
let c = (1, 2, 3);
9+
let d = c.1suffix;
10+
//~^ ERROR suffixes on a tuple index are invalid
11+
println!("{}", d);
12+
let s = X { 0suffix: 0, 1: 1, 2: 2 };
13+
//~^ ERROR suffixes on a tuple index are invalid
14+
match s {
15+
X { 0suffix: _, .. } => {}
16+
//~^ ERROR suffixes on a tuple index are invalid
17+
}
18+
}

‎src/test/ui/parser/issue-59418.stderr

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: suffixes on a tuple index are invalid
2+
--> $DIR/issue-59418.rs:5:15
3+
|
4+
LL | let b = a.1suffix;
5+
| ^^^^^^^ invalid suffix `suffix`
6+
7+
error: suffixes on a tuple index are invalid
8+
--> $DIR/issue-59418.rs:9:15
9+
|
10+
LL | let d = c.1suffix;
11+
| ^^^^^^^ invalid suffix `suffix`
12+
13+
error: suffixes on a tuple index are invalid
14+
--> $DIR/issue-59418.rs:12:17
15+
|
16+
LL | let s = X { 0suffix: 0, 1: 1, 2: 2 };
17+
| ^^^^^^^ invalid suffix `suffix`
18+
19+
error: suffixes on a tuple index are invalid
20+
--> $DIR/issue-59418.rs:15:13
21+
|
22+
LL | X { 0suffix: _, .. } => {}
23+
| ^^^^^^^ invalid suffix `suffix`
24+
25+
error: aborting due to 4 previous errors
26+

0 commit comments

Comments
 (0)
Please sign in to comment.