Skip to content
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

c2rust transpile: When casting bools to floats, go through the integral type u8 #1030

Merged
merged 2 commits into from
Nov 16, 2023

Conversation

dgherzka
Copy link
Contributor

@dgherzka dgherzka commented Sep 29, 2023

Previously, C code that cast bools to floating types, like this

#include <stdbool.h>

void cast_stuff(void) {
    bool b = true;
    float x15 = b;
}

would try to do so directly in Rust, like this

#[no_mangle]
pub unsafe extern "C" fn cast_stuff() {
    let mut b: bool = 1 as libc::c_int != 0;
    let mut x15: libc::c_float = b as libc::c_float;
}

which isn't allowed, resulting in errors like this

error[E0606]: casting `bool` as `f32` is invalid
  --> src/casts.rs:31:34
   |
31 |     let mut x15: libc::c_float = b as libc::c_float;
   |                                  ^^^^^^^^^^^^^^^^^^
   |
   = help: cast through an integer first

This fixes things by emitting this Rust instead by casting through the integral type u8:

#[no_mangle]
pub unsafe extern "C" fn cast_stuff() {
    let mut b: bool = 1 as libc::c_int != 0;
    let mut x15: libc::c_float = b as u8 as libc::c_float;
}

@dgherzka
Copy link
Contributor Author

It wasn't clear to me whether this should be implemented as a new CastKind (BooleanToFloating) or treated as a special case of IntegralToFloating.

@dgherzka
Copy link
Contributor Author

Hi @kkysen, pinging you because I see that you are a major contributor to this project. I apologize if you aren't the right person to talk to. What's the process for getting this reviewed? Thanks in advance!

@dgherzka
Copy link
Contributor Author

dgherzka commented Nov 3, 2023

Sorry for continuing to tag people, I just want to try one more time. @spernsteiner are you the right person to talk to? Please let me know if I should just abandon this.

@kkysen
Copy link
Contributor

kkysen commented Nov 3, 2023

Hi @dgherzka! Sorry for taking a while to review this; I saw your ping but forgot to take a look. I'll review it now, and your other PRs and issues.

@kkysen kkysen added the bug Something isn't working label Nov 3, 2023
@kkysen kkysen self-assigned this Nov 3, 2023
@kkysen kkysen self-requested a review November 3, 2023 20:47
Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! Sorry we took so long to review it, but we really appreciate it; thanks for fixing this bug!

It wasn't clear to me whether this should be implemented as a new CastKind (BooleanToFloating) or treated as a special case of IntegralToFloating.

bool technically is an integral type, so I think a case of IntegralToFloating should be fine, and the new code handling this case fits in nicely.

And the simple test case looks good and is enough to catch this bug, so everything LGTM. Thanks!

@kkysen kkysen changed the title When casting bool to float, go through integral type c2rust transpile: When casting bool to float, go through integral type Nov 4, 2023
@kkysen kkysen changed the title c2rust transpile: When casting bool to float, go through integral type c2rust transpile: When casting bool to float, go through integral type Nov 4, 2023
Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for casting through u8 to go bool => u8 => floating type, I'm not sure if casting through c_int would be more correct in terms of what C is supposed to do, but casting through u8 should always be correct, and it seems more idiomatic for Rust, so I think that's fine. @thedataking, do you think that's fine? Are there any subtleties in choosing the intermediary integral type?

@kkysen kkysen changed the title c2rust transpile: When casting bool to float, go through integral type c2rust transpile: When casting bools to floats, go through the integral type u8 Nov 4, 2023
@kkysen kkysen merged commit 3be0dfa into immunant:master Nov 16, 2023
8 checks passed
GPHemsley added a commit to GPHemsley/c2rust that referenced this pull request Sep 21, 2024
kkysen added a commit that referenced this pull request Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants