-
Notifications
You must be signed in to change notification settings - Fork 246
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
: emit zeroed array expressions as [0; N]
#1170
transpile
: emit zeroed array expressions as [0; N]
#1170
Conversation
Thanks Folkert, I like this idea. Your comment indicates the output Rust is likely correct – but not necessarily so. Any examples where this would fail? If so, should this be guarded by a flag like other risky/experimental features? Also, it would be great if you could add a test or two. |
a56b3ee
to
e3405fd
Compare
I believe the current change is correct, across rust versions. It just isn't as general as it could be. For a "repeat" expression
The current versions restricts the type to be numeric But e.g. a For such a struct, c2rust will actually emit a
I added something basic, what else do you want to test and where? |
@folkertdev, thanks for this improvement! It seems like a sound transformation to me.
I'm pretty sure that all C types are implicitly That said, the CI is failing: error[E0599]: no method named `is_integral_type` found for struct `c_ast::CTypeId` in the current scope
--> c2rust-transpile/src/translator/literals.rs:193:48
|
193 | } else if ids.len() == 0 && ty.is_integral_type() || ty.is_floating_type() {
| ^^^^^^^^^^^^^^^^ method not found in `c_ast::CTypeId`
|
::: c2rust-transpile/src/c_ast/mod.rs:15:1
|
15 | pub struct CTypeId(pub u64);
| ------------------ method `is_integral_type` not found for this struct
error[E0599]: no method named `is_floating_type` found for struct `c_ast::CTypeId` in the current scope
--> c2rust-transpile/src/translator/literals.rs:193:73
|
193 | } else if ids.len() == 0 && ty.is_integral_type() || ty.is_floating_type() {
| ^^^^^^^^^^^^^^^^ method not found in `c_ast::CTypeId`
|
::: c2rust-transpile/src/c_ast/mod.rs:15:1
|
15 | pub struct CTypeId(pub u64);
| ------------------ method `is_floating_type` not found for this struct Did you see this? |
e3405fd
to
e86322b
Compare
C data types should be c2rust/c2rust-transpile/src/translator/mod.rs Lines 4570 to 4579 in 5830b4f
I think that would not come up here because we're working with a CI should work now |
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.
Oh, that's a good find. I didn't think about VLAs and unsized types.
I think that would not come up here because we're working with a
CTypeKind::ConstantArray
, but I'm not 100% sure.
I would think so, too. If the C compiles, an unsized type shouldn't be allowed in an array, and therefore the array's element type should be Copy
.
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.
LGTM now, but could someone else take a quite look to make sure there's not a case I'm overlooking where this might not work? @fw-immunant maybe?
[0; N]
[0; N]
[0; N]
transpile
: emit zeroed array expressions as [0; N]
I think this is fine, but I would want to test it against a case like |
an array like `int x[16] = {}` was emitted as `[0, 0, 0, 0, ...]`, but is not emitted as `[0; 16]`.
e86322b
to
9e5a1c0
Compare
I added that test case |
an input like
was emitted as
but now emits
The current version only supports floating and integral types: in more recent rust versions it would be easy to make this work for e.g. structs: just use a
const { ... }
block. But c2rust targets older versions, and there (without introducing extra constants) the expression's type must be copy, and I could not find a convenient way to tell whether aCTypeId
will derive/implementCopy
.this came up when working with the output of
creduce
, which loves zeroing arrays.