Reject integer suffix when tuple indexing
This commit is contained in:
parent
4c27fb19ba
commit
91b7423760
3 changed files with 69 additions and 43 deletions
|
@ -3196,51 +3196,60 @@ impl<'a> Parser<'a> {
|
||||||
// expr.f
|
// expr.f
|
||||||
if self.eat(&token::Dot) {
|
if self.eat(&token::Dot) {
|
||||||
match self.token {
|
match self.token {
|
||||||
token::Ident(..) => {
|
token::Ident(..) => {
|
||||||
e = self.parse_dot_suffix(e, lo)?;
|
e = self.parse_dot_suffix(e, lo)?;
|
||||||
}
|
|
||||||
token::Literal(token::Integer(name), _) => {
|
|
||||||
let span = self.span;
|
|
||||||
self.bump();
|
|
||||||
let field = ExprKind::Field(e, Ident::new(name, span));
|
|
||||||
e = self.mk_expr(lo.to(span), field, ThinVec::new());
|
|
||||||
}
|
|
||||||
token::Literal(token::Float(n), _suf) => {
|
|
||||||
self.bump();
|
|
||||||
let fstr = n.as_str();
|
|
||||||
let mut err = self.diagnostic()
|
|
||||||
.struct_span_err(self.prev_span, &format!("unexpected token: `{}`", n));
|
|
||||||
err.span_label(self.prev_span, "unexpected token");
|
|
||||||
if fstr.chars().all(|x| "0123456789.".contains(x)) {
|
|
||||||
let float = match fstr.parse::<f64>().ok() {
|
|
||||||
Some(f) => f,
|
|
||||||
None => continue,
|
|
||||||
};
|
|
||||||
let sugg = pprust::to_string(|s| {
|
|
||||||
use crate::print::pprust::PrintState;
|
|
||||||
s.popen()?;
|
|
||||||
s.print_expr(&e)?;
|
|
||||||
s.s.word( ".")?;
|
|
||||||
s.print_usize(float.trunc() as usize)?;
|
|
||||||
s.pclose()?;
|
|
||||||
s.s.word(".")?;
|
|
||||||
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
|
|
||||||
});
|
|
||||||
err.span_suggestion(
|
|
||||||
lo.to(self.prev_span),
|
|
||||||
"try parenthesizing the first index",
|
|
||||||
sugg,
|
|
||||||
Applicability::MachineApplicable
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return Err(err);
|
token::Literal(token::Integer(name), suffix) => {
|
||||||
|
let span = self.span;
|
||||||
|
self.bump();
|
||||||
|
let field = ExprKind::Field(e, Ident::new(name, span));
|
||||||
|
e = self.mk_expr(lo.to(span), field, ThinVec::new());
|
||||||
|
|
||||||
}
|
if let Some(suffix) = suffix {
|
||||||
_ => {
|
let mut err = self.diagnostic().struct_span_err(
|
||||||
// FIXME Could factor this out into non_fatal_unexpected or something.
|
span,
|
||||||
let actual = self.this_token_to_string();
|
"tuple index with a suffix is invalid",
|
||||||
self.span_err(self.span, &format!("unexpected token: `{}`", actual));
|
);
|
||||||
}
|
err.span_label(span, format!("invalid suffix `{}`", suffix));
|
||||||
|
err.emit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token::Literal(token::Float(n), _suf) => {
|
||||||
|
self.bump();
|
||||||
|
let fstr = n.as_str();
|
||||||
|
let mut err = self.diagnostic()
|
||||||
|
.struct_span_err(self.prev_span, &format!("unexpected token: `{}`", n));
|
||||||
|
err.span_label(self.prev_span, "unexpected token");
|
||||||
|
if fstr.chars().all(|x| "0123456789.".contains(x)) {
|
||||||
|
let float = match fstr.parse::<f64>().ok() {
|
||||||
|
Some(f) => f,
|
||||||
|
None => continue,
|
||||||
|
};
|
||||||
|
let sugg = pprust::to_string(|s| {
|
||||||
|
use crate::print::pprust::PrintState;
|
||||||
|
s.popen()?;
|
||||||
|
s.print_expr(&e)?;
|
||||||
|
s.s.word( ".")?;
|
||||||
|
s.print_usize(float.trunc() as usize)?;
|
||||||
|
s.pclose()?;
|
||||||
|
s.s.word(".")?;
|
||||||
|
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
|
||||||
|
});
|
||||||
|
err.span_suggestion(
|
||||||
|
lo.to(self.prev_span),
|
||||||
|
"try parenthesizing the first index",
|
||||||
|
sugg,
|
||||||
|
Applicability::MachineApplicable
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return Err(err);
|
||||||
|
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// FIXME Could factor this out into non_fatal_unexpected or something.
|
||||||
|
let actual = self.this_token_to_string();
|
||||||
|
self.span_err(self.span, &format!("unexpected token: `{}`", actual));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
9
src/test/ui/parser/issue-59418.rs
Normal file
9
src/test/ui/parser/issue-59418.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
struct X(i32,i32,i32);
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = X(1, 2, 3);
|
||||||
|
let b = a.1suffix;
|
||||||
|
//~^ ERROR tuple index with a suffix is invalid
|
||||||
|
println!("{}", b);
|
||||||
|
}
|
||||||
|
|
8
src/test/ui/parser/issue-59418.stderr
Normal file
8
src/test/ui/parser/issue-59418.stderr
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
error: tuple index with a suffix is invalid
|
||||||
|
--> $DIR/issue-59418.rs:5:15
|
||||||
|
|
|
||||||
|
LL | let b = a.1suffix;
|
||||||
|
| ^^^^^^^ invalid suffix `suffix`
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue