-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: f32 and f64 representation during lowering #12395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
crates/syntax/src/ast/token_ext.rs
Outdated
|
||
#[derive(Default, Debug, Clone, Eq, PartialEq)] | ||
pub struct FloatTypeWrapper { | ||
//We convert float values into bits and that's how we don't need to deal with f32 and f64. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: missing space after //
and I'd probably go for a tuple struct (struct FloatEq(u64);
), but it doesn't matter too much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using tuple struct seemed good, fixed the nit too!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also sorry for bringing it up here, tho I am not able to reach Jake ( @~jhgg ) for further review on this PR:
#12347
I try to press the re-request review button, but it doesn't do anything :(
Can you have a look at that PR too? 😅
crates/syntax/src/ast/token_ext.rs
Outdated
pub fn float_value(&self) -> Option<FloatTypeWrapper> { | ||
let (_, text, _) = self.split_into_parts(); | ||
let float_value = text.parse::<f64>().ok()?; | ||
Some(FloatTypeWrapper::new(float_value)) | ||
} | ||
} | ||
|
||
// We convert float values into bits and that's how we don't need to deal with f32 and f64. | ||
// For PartialEq, bits comparison should work, as ordering is not important | ||
// https://github.com/rust-lang/rust-analyzer/issues/12380#issuecomment-1137284360 | ||
#[derive(Default, Debug, Clone, Eq, PartialEq)] | ||
pub struct FloatTypeWrapper(u64); | ||
|
||
impl FloatTypeWrapper { | ||
fn new(value: f64) -> Self { | ||
Self(value.to_bits()) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for FloatTypeWrapper { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", f64::from_bits(self.0)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't go into syntax
, let's put this where it is used by hir-def
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shifted it to same file where Literal::Float
is defined
crates/syntax/src/ast/token_ext.rs
Outdated
pub fn value(&self) -> Option<FloatTypeWrapper> { | ||
let float_value = self.text().parse::<f64>().ok()?; | ||
Some(FloatTypeWrapper::new(float_value)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly this should just return the f64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made changes for the same
crates/syntax/src/ast/token_ext.rs
Outdated
@@ -331,6 +355,11 @@ impl ast::FloatNumber { | |||
} | |||
Some(&text[suffix_start..]) | |||
} | |||
|
|||
pub fn value(&self) -> Option<FloatTypeWrapper> { | |||
let float_value = self.text().parse::<f64>().ok()?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will fail when we have things like 1.0f32
, we should probably have a split_into_parts
akin to ast::IntNumber (minus the prefix part).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's right, implemented the same too!
expect![[r#" | ||
*FOO* | ||
|
||
```rust |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be good to move these tests to hir_ty::consteval::tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So just have one float literal test here and move other cases to consteval?
Edit: Actually should they go in consteval
this was a general problem regarding handling of floats, and they are being used in other places too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So just have one float literal test here and move other cases to consteval?
Yes, that sounds good to me.
Actually should they go in consteval this was a general problem regarding handling of floats
I think it only affects consteval
, and current tests just test this, but if there are other problems fixed/affected by this, then adding test for those in addition to consteval
tests is good.
So, while re-reviewing the PR and understanding more from the review of @HKalbasi , I realized this does not fix But for cases like rust-analyzer/crates/hir-ty/src/consteval.rs Lines 222 to 226 in 732eb9a
Though it does fix exactly what the title mentions, so to close the issue #12380 completely I would have to add support for float in I was wondering should I do it in the same PR or separate one? We can merge the current one for basic support and as a partial fix to #12380 , then the remaning fix can be made in a separate PR |
I think it is fine to not support float operations for now, since hover on |
Then ig it makes sense to leave the current tests where they are :) |
Thanks! |
📌 Commit 1f4870f has been approved by |
☀️ Test successful - checks-actions |
should fix #12380