Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/ast/token_ext.rs')
| -rw-r--r-- | crates/syntax/src/ast/token_ext.rs | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 1ce548f8fc..df017ddde6 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -1,9 +1,6 @@ //! There are many AstNodes, but only a few tokens, so we hand-write them here. -use std::{ - borrow::Cow, - num::{ParseFloatError, ParseIntError}, -}; +use std::{borrow::Cow, num::ParseIntError}; use rustc_lexer::unescape::{ unescape_byte, unescape_char, unescape_mixed, unescape_unicode, EscapeError, MixedUnit, Mode, @@ -393,9 +390,9 @@ impl ast::IntNumber { } } - pub fn float_value(&self) -> Option<f64> { + pub fn value_string(&self) -> String { let (_, text, _) = self.split_into_parts(); - text.replace('_', "").parse::<f64>().ok() + text.replace('_', "") } } @@ -432,14 +429,9 @@ impl ast::FloatNumber { } } - pub fn value(&self) -> Result<f64, ParseFloatError> { - let (text, _) = self.split_into_parts(); - text.replace('_', "").parse::<f64>() - } - - pub fn value_f32(&self) -> Result<f32, ParseFloatError> { + pub fn value_string(&self) -> String { let (text, _) = self.split_into_parts(); - text.replace('_', "").parse::<f32>() + text.replace('_', "") } } @@ -497,6 +489,8 @@ impl ast::Byte { #[cfg(test)] mod tests { + use rustc_apfloat::ieee::Quad as f128; + use crate::ast::{self, make, FloatNumber, IntNumber}; fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) { @@ -507,12 +501,17 @@ mod tests { assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into()); } - fn check_float_value(lit: &str, expected: impl Into<Option<f64>> + Copy) { + // FIXME(#17451) Use `expected: f128` once `f128` is stabilised. + fn check_float_value(lit: &str, expected: &str) { + let expected = Some(expected.parse::<f128>().unwrap()); assert_eq!( - FloatNumber { syntax: make::tokens::literal(lit) }.value().ok(), - expected.into() + FloatNumber { syntax: make::tokens::literal(lit) }.value_string().parse::<f128>().ok(), + expected + ); + assert_eq!( + IntNumber { syntax: make::tokens::literal(lit) }.value_string().parse::<f128>().ok(), + expected ); - assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.float_value(), expected.into()); } fn check_int_value(lit: &str, expected: impl Into<Option<u128>>) { @@ -525,9 +524,9 @@ mod tests { check_float_suffix("123f32", "f32"); check_float_suffix("123.0e", None); check_float_suffix("123.0e4", None); - check_float_suffix("123.0ef32", "f32"); + check_float_suffix("123.0ef16", "f16"); check_float_suffix("123.0E4f32", "f32"); - check_float_suffix("1_2_3.0_f32", "f32"); + check_float_suffix("1_2_3.0_f128", "f128"); } #[test] @@ -594,8 +593,10 @@ bcde", b"abcde", #[test] fn test_value_underscores() { - check_float_value("1.234567891011121_f64", 1.234567891011121_f64); - check_float_value("1__0.__0__f32", 10.0); + check_float_value("1.3_4665449586950493453___6_f128", "1.346654495869504934536"); + check_float_value("1.234567891011121_f64", "1.234567891011121"); + check_float_value("1__0.__0__f32", "10.0"); + check_float_value("3._0_f16", "3.0"); check_int_value("0b__1_0_", 2); check_int_value("1_1_1_1_1_1", 111111); } |