diff options
| author | bors <bors@rust-lang.org> | 2023-01-31 00:03:08 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-01-31 00:03:08 +0000 |
| commit | 487e83b711b96d84ce2edfc4cb4ba792c10b589e (patch) | |
| tree | 6e79e3ff2f39930c60e7b1758d9ed8b4f3d6864c /compiler/rustc_parse/src | |
| parent | 001a77fac33f6560ff361ff38f661ff5f1c6bf85 (diff) | |
| parent | 80fcd7c40ec46613b130b5d732162ecbfda6c509 (diff) | |
| download | rust-487e83b711b96d84ce2edfc4cb4ba792c10b589e.tar.gz rust-487e83b711b96d84ce2edfc4cb4ba792c10b589e.zip | |
Auto merge of #105650 - cassaundra:float-literal-suggestion, r=pnkfelix
Fix invalid float literal suggestions when recovering an integer Only suggest adding a zero to integers with a preceding dot when the change will result in a valid floating point literal. For example, `.0x0` should not be turned into `0.0x0`. r? nnethercote
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 17d1e200b41..dcc3059a7f4 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1882,7 +1882,16 @@ impl<'a> Parser<'a> { if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = next_token.kind { - if self.token.span.hi() == next_token.span.lo() { + // If this integer looks like a float, then recover as such. + // + // We will never encounter the exponent part of a floating + // point literal here, since there's no use of the exponent + // syntax that also constitutes a valid integer, so we need + // not check for that. + if suffix.map_or(true, |s| s == sym::f32 || s == sym::f64) + && symbol.as_str().chars().all(|c| c.is_numeric() || c == '_') + && self.token.span.hi() == next_token.span.lo() + { let s = String::from("0.") + symbol.as_str(); let kind = TokenKind::lit(token::Float, Symbol::intern(&s), suffix); return Some(Token::new(kind, self.token.span.to(next_token.span))); |
