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

Translate-C works with some pointer arithmetic syntax, but not semantically equivalent one #22925

Open
drfuchs opened this issue Feb 17, 2025 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)

Comments

@drfuchs
Copy link

drfuchs commented Feb 17, 2025

Zig Version

0.14.0-dev.2851+b074fb7dd

Steps to Reproduce and Observed Behavior

These two printf lines are semantically equivalent in C, but Translate-C produces different zig code for them, the second of which doesn't compile.

#include <stdio.h>

int main(void)
{
  static const int primes[] = {2, 3, 5, 7, 11};
  printf("%d\n", (primes+3)[-1]);
  printf("%d\n", (&primes[3])[-1]);
  return 0;
}

results in zig errors from the second printf:

arybug.zig:336:188: error: incompatible types: '*const c_int' and 'usize'
        if (tmp >= 0) break :blk (&primes.static[@as(c_uint, @intCast(@as(c_int, 3)))]) + @as(usize, @intCast(tmp)) else break :blk (&primes.static[@as(c_uint, @intCast(@as(c_int, 3)))]) - ~@as(usize, @bitCast(@as(isize, @intCast(tmp)) +% -1));
                                                                                                                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arybug.zig:336:133: note: type '*const c_int' here
        if (tmp >= 0) break :blk (&primes.static[@as(c_uint, @intCast(@as(c_int, 3)))]) + @as(usize, @intCast(tmp)) else break :blk (&primes.static[@as(c_uint, @intCast(@as(c_int, 3)))]) - ~@as(usize, @bitCast(@as(isize, @intCast(tmp)) +% -1));
                                                                                                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arybug.zig:336:190: note: type 'usize' here
        if (tmp >= 0) break :blk (&primes.static[@as(c_uint, @intCast(@as(c_int, 3)))]) + @as(usize, @intCast(tmp)) else break :blk (&primes.static[@as(c_uint, @intCast(@as(c_int, 3)))]) - ~@as(usize, @bitCast(@as(isize, @intCast(tmp)) +% -1));
                                                                                                                                                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

Should result in zig code that compiles and runs and produces the same two-duplicate-lines as the C code does:

5
5
@drfuchs drfuchs added the bug Observed behavior contradicts documented or intended behavior label Feb 17, 2025
@alexrp alexrp added the translate-c C to Zig source translation feature (@cImport) label Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

No branches or pull requests

2 participants