fix: f32 and f64 representation during lowering by feniljain · Pull Request #123...
source link: https://github.com/rust-lang/rust-analyzer/pull/12395
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
fix: f32 and f64 representation during lowering #12395
Conversation
Contributor
feniljain commented 7 days ago
should fix #12380
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. |
Nit: missing space after //
and I'd probably go for a tuple struct (struct FloatEq(u64);
), but it doesn't matter too much.
Contributor
Author
feniljain 7 days ago
Using tuple struct seemed good, fixed the nit too!
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)) |
||
} |
This shouldn't go into syntax
, let's put this where it is used by hir-def
Contributor
Author
feniljain 7 days ago
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)) |
||
} |
Similarly this should just return the f64
Contributor
Author
feniljain 7 days ago
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()?; |
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).
Contributor
Author
feniljain 7 days ago
Yeah, that's right, implemented the same too!
expect![[r#" |
||
*FOO* |
||
```rust |
Contributor
HKalbasi 7 days ago
It might be good to move these tests to hir_ty::consteval::tests
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
Contributor
HKalbasi 7 days ago
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 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 |
Contributor
HKalbasi commented 6 days ago
I think it is fine to not support float operations for now, since hover on |
Contributor
Author
feniljain commented 6 days ago
Then ig it makes sense to leave the current tests where they are :) |
Member
Veykril commented 6 days ago
Thanks! |
Collaborator
bors commented 6 days ago
Commit 1f4870f has been approved by |
Collaborator
bors commented 6 days ago
Collaborator
bors commented 6 days ago
Test successful - checks-actions |
Member
lnicola commented 3 days ago
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
No one assigned
None yet
No milestone
Successfully merging this pull request may close these issues.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK