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

transpile: Make cast from bool to pointer compile through size_t #1134

Merged
merged 1 commit into from
Sep 22, 2024

Conversation

GPHemsley
Copy link
Contributor

@GPHemsley GPHemsley commented Sep 21, 2024

@kkysen kkysen changed the title transpile: Make cast from bool to pointer compile transpile: Make cast from bool to pointer compile through size_t Sep 22, 2024
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.

LGTM. Same exact thing as #1030 except it's through size_t.

One thing that we should keep in mind is that #![feature(strict_provenance)] will probably be stabilized soon (rust-lang/rust#130350), and at that point, it'd probably be better to be more specific about this.

In #1077, @jorendorff said this was the reason why this is done:

(Why, you might ask, would anyone ever do this in real code. The answer is that C doesn't have generics, so projects will have, say, a single hash table type with void * values, and store booleans in it by casting.)

This seems likely, as a provenance-preserving pointer cast through a bool does not make any sense, as false/0 is null and true/1 should almost always be an invalid address as well. Thus, we should be able to unconditionally use ptr::without_provenance for this cast once strict_provenance is stabilized.

} else if target_ty_ctype.is_pointer() && source_ty_kind.is_bool() {
val.and_then(|x| {
Ok(WithStmts::new_val(mk().cast_expr(
mk().cast_expr(x, mk().path_ty(vec!["libc", "size_t"])),
Copy link
Contributor

Choose a reason for hiding this comment

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

We really should change these to core::ffi:: instead of libc:: at some point, but it's fine here; that's what we've done so far.

@kkysen kkysen merged commit 73bdbb8 into immunant:master Sep 22, 2024
8 of 9 checks passed
@GPHemsley GPHemsley deleted the GH-1077 branch September 22, 2024 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Casting bool to pointer type results in invalid Rust
2 participants