Skip to content

Commit 91b7423

Browse files
committed
Reject integer suffix when tuple indexing
1 parent 4c27fb1 commit 91b7423

File tree

3 files changed

+69
-43
lines changed

3 files changed

+69
-43
lines changed

src/libsyntax/parse/parser.rs

+52-43
Original file line numberDiff line numberDiff line change
@@ -3196,51 +3196,60 @@ impl<'a> Parser<'a> {
31963196
// expr.f
31973197
if self.eat(&token::Dot) {
31983198
match self.token {
3199-
token::Ident(..) => {
3200-
e = self.parse_dot_suffix(e, lo)?;
3201-
}
3202-
token::Literal(token::Integer(name), _) => {
3203-
let span = self.span;
3204-
self.bump();
3205-
let field = ExprKind::Field(e, Ident::new(name, span));
3206-
e = self.mk_expr(lo.to(span), field, ThinVec::new());
3207-
}
3208-
token::Literal(token::Float(n), _suf) => {
3209-
self.bump();
3210-
let fstr = n.as_str();
3211-
let mut err = self.diagnostic()
3212-
.struct_span_err(self.prev_span, &format!("unexpected token: `{}`", n));
3213-
err.span_label(self.prev_span, "unexpected token");
3214-
if fstr.chars().all(|x| "0123456789.".contains(x)) {
3215-
let float = match fstr.parse::<f64>().ok() {
3216-
Some(f) => f,
3217-
None => continue,
3218-
};
3219-
let sugg = pprust::to_string(|s| {
3220-
use crate::print::pprust::PrintState;
3221-
s.popen()?;
3222-
s.print_expr(&e)?;
3223-
s.s.word( ".")?;
3224-
s.print_usize(float.trunc() as usize)?;
3225-
s.pclose()?;
3226-
s.s.word(".")?;
3227-
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
3228-
});
3229-
err.span_suggestion(
3230-
lo.to(self.prev_span),
3231-
"try parenthesizing the first index",
3232-
sugg,
3233-
Applicability::MachineApplicable
3234-
);
3199+
token::Ident(..) => {
3200+
e = self.parse_dot_suffix(e, lo)?;
32353201
}
3236-
return Err(err);
3202+
token::Literal(token::Integer(name), suffix) => {
3203+
let span = self.span;
3204+
self.bump();
3205+
let field = ExprKind::Field(e, Ident::new(name, span));
3206+
e = self.mk_expr(lo.to(span), field, ThinVec::new());
32373207

3238-
}
3239-
_ => {
3240-
// FIXME Could factor this out into non_fatal_unexpected or something.
3241-
let actual = self.this_token_to_string();
3242-
self.span_err(self.span, &format!("unexpected token: `{}`", actual));
3243-
}
3208+
if let Some(suffix) = suffix {
3209+
let mut err = self.diagnostic().struct_span_err(
3210+
span,
3211+
"tuple index with a suffix is invalid",
3212+
);
3213+
err.span_label(span, format!("invalid suffix `{}`", suffix));
3214+
err.emit();
3215+
}
3216+
}
3217+
token::Literal(token::Float(n), _suf) => {
3218+
self.bump();
3219+
let fstr = n.as_str();
3220+
let mut err = self.diagnostic()
3221+
.struct_span_err(self.prev_span, &format!("unexpected token: `{}`", n));
3222+
err.span_label(self.prev_span, "unexpected token");
3223+
if fstr.chars().all(|x| "0123456789.".contains(x)) {
3224+
let float = match fstr.parse::<f64>().ok() {
3225+
Some(f) => f,
3226+
None => continue,
3227+
};
3228+
let sugg = pprust::to_string(|s| {
3229+
use crate::print::pprust::PrintState;
3230+
s.popen()?;
3231+
s.print_expr(&e)?;
3232+
s.s.word( ".")?;
3233+
s.print_usize(float.trunc() as usize)?;
3234+
s.pclose()?;
3235+
s.s.word(".")?;
3236+
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
3237+
});
3238+
err.span_suggestion(
3239+
lo.to(self.prev_span),
3240+
"try parenthesizing the first index",
3241+
sugg,
3242+
Applicability::MachineApplicable
3243+
);
3244+
}
3245+
return Err(err);
3246+
3247+
}
3248+
_ => {
3249+
// FIXME Could factor this out into non_fatal_unexpected or something.
3250+
let actual = self.this_token_to_string();
3251+
self.span_err(self.span, &format!("unexpected token: `{}`", actual));
3252+
}
32443253
}
32453254
continue;
32463255
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct X(i32,i32,i32);
2+
3+
fn main() {
4+
let a = X(1, 2, 3);
5+
let b = a.1suffix;
6+
//~^ ERROR tuple index with a suffix is invalid
7+
println!("{}", b);
8+
}
9+

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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: tuple index with a suffix is invalid
2+
--> $DIR/issue-59418.rs:5:15
3+
|
4+
LL | let b = a.1suffix;
5+
| ^^^^^^^ invalid suffix `suffix`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)