#Wrong type in code generated by translate-c?

1 messages · Page 1 of 1 (latest)

dull breach
#

I am trying to translate the following C code to Zig using zig translate-c:

float float_test() {
  return (float)(10.0f > 1.0f);
}

This results in the following Zig code:

pub export fn float_test() f32 {
  return @as(f32, @floatFromInt(10.0 > 1.0));
}

Now since 10.0 > 1.0 produces a bool, trying to compile the generated code fails with error: expected integer type, found 'bool'. Could this possibly be a bug? Shouldn't the bool get casted into an integer before calling @floatFromInt, e. g. like this:

pub export fn float_test() f32 {
  return @as(f32, @floatFromInt(@intFromBool(10.0 > 1.0)));
}
silver crown
#

translate-c is pretty much only good for headers and declaration files. i dont know how well the support is/will be for definitions

#

you can just call that c function from zig pretty easily if you cImport the header

dull breach
#

@silver crown I actually ran into this issue while calling a C function using @cImport and just used translate-c for reproducing it. The generated Zig code is the same in both cases.

wanton lotus
#

cImport is just translate-c underneath

silver crown
hearty shuttle
#

Some headers have more than just declarations, such as static functions that need to be inlined (under the assumption that LTO is off)

dull breach
#

@silver crown just importing a header containing only the function declaration solves the issue. But putting the definition into the header produces the same error. So is this still a bug that I should create an issue for? As @hearty shuttle mentioned, headers can contain more than just declarations (e. g. also thinking of header-only libraries).

silver crown