mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Auto merge of #13161 - ChayimFriedman2:pi-is-zero, r=jonas-schievink
fix: Lower float literals with underscores Fixes #13155 (the problem was the `PI` is defined with `_f64` suffix). `PI` is still truncated, though, because `f64` cannot represent the value with full precision.
This commit is contained in:
commit
f23114c854
1 changed files with 19 additions and 2 deletions
|
@ -322,7 +322,7 @@ impl ast::IntNumber {
|
||||||
|
|
||||||
pub fn float_value(&self) -> Option<f64> {
|
pub fn float_value(&self) -> Option<f64> {
|
||||||
let (_, text, _) = self.split_into_parts();
|
let (_, text, _) = self.split_into_parts();
|
||||||
text.parse::<f64>().ok()
|
text.replace('_', "").parse::<f64>().ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,7 +361,7 @@ impl ast::FloatNumber {
|
||||||
|
|
||||||
pub fn value(&self) -> Option<f64> {
|
pub fn value(&self) -> Option<f64> {
|
||||||
let (text, _) = self.split_into_parts();
|
let (text, _) = self.split_into_parts();
|
||||||
text.parse::<f64>().ok()
|
text.replace('_', "").parse::<f64>().ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -397,6 +397,15 @@ mod tests {
|
||||||
assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
|
assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_float_value(lit: &str, expected: impl Into<Option<f64>> + Copy) {
|
||||||
|
assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.value(), expected.into());
|
||||||
|
assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.float_value(), expected.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_int_value(lit: &str, expected: impl Into<Option<u128>>) {
|
||||||
|
assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.value(), expected.into());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_float_number_suffix() {
|
fn test_float_number_suffix() {
|
||||||
check_float_suffix("123.0", None);
|
check_float_suffix("123.0", None);
|
||||||
|
@ -437,6 +446,14 @@ mod tests {
|
||||||
check_string_value(r"\nfoobar", "\nfoobar");
|
check_string_value(r"\nfoobar", "\nfoobar");
|
||||||
check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");
|
check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_value_underscores() {
|
||||||
|
check_float_value("3.141592653589793_f64", 3.141592653589793_f64);
|
||||||
|
check_float_value("1__0.__0__f32", 10.0);
|
||||||
|
check_int_value("0b__1_0_", 2);
|
||||||
|
check_int_value("1_1_1_1_1_1", 111111);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::Char {
|
impl ast::Char {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue