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: error: expected pointer, found '?[*c]c_int' #9917

Open
sigod opened this issue Oct 7, 2021 · 3 comments
Open

translate-c: error: expected pointer, found '?[*c]c_int' #9917

sigod opened this issue Oct 7, 2021 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@sigod
Copy link

sigod commented Oct 7, 2021

Zig Version

0.9.0-dev.1324+598db831f

Steps to Reproduce

I'm trying to save a PNG file with stb_image_write library:

const c = @cImport({
    @cDefine("STB_IMAGE_WRITE_IMPLEMENTATION", {});
    @cInclude("stb_image_write.h");
});

pub fn main() anyerror!void {
    const pixels: []u8 = &.{};
    if (c.stbi_write_png("out.png", 1920, 1024, 4, pixels.ptr, 0) == 0) {
        return error.StbiError;
    }
}

Expected Behavior

Expected it to successfully compile. As it does with stbi_write_jpg, for example.

Actual Behavior

It fails with:

.\zig-cache\o\b776ce5469cb6475122be6741d09298f\cimport.zig:2209:50: error: expected pointer, found '?[*c]c_int'
    var p: ?*c_void = realloc(@ptrCast(?*c_void, if (arr.* != null) @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), arr.*)) - @bitCast(usize, @intCast(isize, @as(c_int, 2))) else null), @bitCast(c_ulonglong, @as(c_longlong, itemsize * m)) +% (@sizeOf(c_int) *% @bitCast(c_ulonglong, @as(c_longlong, @as(c_int, 2)))));
                                                 ^
.\zig-cache\o\b776ce5469cb6475122be6741d09298f\cimport.zig:2209:31: note: referenced here
    var p: ?*c_void = realloc(@ptrCast(?*c_void, if (arr.* != null) @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), arr.*)) - @bitCast(usize, @intCast(isize, @as(c_int, 2))) else null), @bitCast(c_ulonglong, @as(c_longlong, itemsize * m)) +% (@sizeOf(c_int) *% @bitCast(c_ulonglong, @as(c_longlong, @as(c_int, 2)))));
                              ^

Looks like the issue is with line 829:

void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2);
@sigod sigod added the bug Observed behavior contradicts documented or intended behavior label Oct 7, 2021
@sigod
Copy link
Author

sigod commented Oct 8, 2021

I should mention, it also prints this error:

...\zig\lib\std\meta.zig:122:37: error: no align available for type 'c_void'
        .Optional => |info| switch (@typeInfo(info.child)) {
                                    ^
.\zig-cache\o\b776ce5469cb6475122be6741d09298f\cimport.zig:2444:496: note: called from here
        _ = if ((out == null) or (((@ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), @ptrCast(?*c_void, out))) - @bitCast(usize, @intCast(isize, @as(c_int, 2))))[@intCast(c_uint, @as(c_int, 1))] + @as(c_int, 1)) >= (@ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), @ptrCast(?*c_void, out))) - @bitCast(usize, @intCast(isize, @as(c_int, 2))))[@intCast(c_uint, @as(c_int, 0))])) stbiw__sbgrowf(@ptrCast([*c]?*c_void, @alignCast(@import("std").meta.alignment(?*c_void), &out)), @as(c_int, 1), @bitCast(c_int, @truncate(c_uint, @sizeOf(u8)))) else null;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ^
.\zig-cache\o\b776ce5469cb6475122be6741d09298f\cimport.zig:2292:130: note: called from here
pub fn stbi_zlib_compress(arg_data: [*c]u8, arg_data_len: c_int, arg_out_len: [*c]c_int, arg_quality: c_int) callconv(.C) [*c]u8 {
                                                                                                                                 ^
...\zig\lib\std\meta.zig:120:49: note: referenced here
pub fn alignment(comptime T: type) comptime_int {
                                                ^
...\zig\lib\std\meta.zig:123:39: note: referenced here
            .Pointer, .Fn => alignment(info.child),
                                      ^
.\zig-cache\o\b776ce5469cb6475122be6741d09298f\cimport.zig:2444:496: note: referenced here
        _ = if ((out == null) or (((@ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), @ptrCast(?*c_void, out))) - @bitCast(usize, @intCast(isize, @as(c_int, 2))))[@intCast(c_uint, @as(c_int, 1))] + @as(c_int, 1)) >= (@ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), @ptrCast(?*c_void, out))) - @bitCast(usize, @intCast(isize, @as(c_int, 2))))[@intCast(c_uint, @as(c_int, 0))])) stbiw__sbgrowf(@ptrCast([*c]?*c_void, @alignCast(@import("std").meta.alignment(?*c_void), &out)), @as(c_int, 1), @bitCast(c_int, @truncate(c_uint, @sizeOf(u8)))) else null;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ^

I'm not sure if they're separate or related.

@akovaski
Copy link

akovaski commented Oct 9, 2021

I'm not familiar enough with Zig to suggest or make a fix here, but I have generated minimal reproducible examples of the above errors.

error: expected pointer, found '?[*c]c_int'

source code

void *repr_9917_a(int *arr, int check)
{
   void *p = check ? arr : 0;
   return p;
}

translated code

pub export fn repr_9917_a(arg_arr: [*c]c_int, arg_check: c_int) ?*c_void {
    var arr = arg_arr;
    var check = arg_check;
    var p: ?*c_void = @ptrCast(?*c_void, if (check != 0) arr else null);
    return p;
}

error

zig-cache/o/3c589d970733d4f2f8b3fecc1a600c26/cimport.zig:53:42: error: expected pointer, found '?[*c]c_int'
    var p: ?*c_void = @ptrCast(?*c_void, if (check != 0) arr else null);
                                         ^
zig-cache/o/3c589d970733d4f2f8b3fecc1a600c26/cimport.zig:53:23: note: referenced here
    var p: ?*c_void = @ptrCast(?*c_void, if (check != 0) arr else null);

error: no align available for type 'c_void'

source code

void ** repr_9917_b(char **out)
{
   void **r = (void **) out;
   return r;
}

translated code

pub export fn repr_9917_b(arg_out: [*c][*c]u8) [*c]?*c_void {
    var out = arg_out;
    var r: [*c]?*c_void = @ptrCast([*c]?*c_void, @alignCast(@import("std").meta.alignment(?*c_void), out));
    return r;
}

error

/opt/zig-linux-x86_64-0.9.0-dev.1343+75cecef63/lib/std/meta.zig:121:37: error: no align available for type 'c_void'
        .Optional => |info| switch (@typeInfo(info.child)) {
                                    ^
zig-cache/o/d2febc8073dcce122a9eb979e2172b63/cimport.zig:58:90: note: called from here
    var r: [*c]?*c_void = @ptrCast([*c]?*c_void, @alignCast(@import("std").meta.alignment(?*c_void), out));
                                                                                         ^
zig-cache/o/d2febc8073dcce122a9eb979e2172b63/cimport.zig:56:61: note: called from here
pub export fn repr_9917_b(arg_out: [*c][*c]u8) [*c]?*c_void {
                                                            ^
/opt/zig-linux-x86_64-0.9.0-dev.1343+75cecef63/lib/std/meta.zig:119:49: note: referenced here
pub fn alignment(comptime T: type) comptime_int {
                                                ^
/opt/zig-linux-x86_64-0.9.0-dev.1343+75cecef63/lib/std/meta.zig:122:39: note: referenced here
            .Pointer, .Fn => alignment(info.child),
                                      ^
zig-cache/o/d2febc8073dcce122a9eb979e2172b63/cimport.zig:58:90: note: referenced here
    var r: [*c]?*c_void = @ptrCast([*c]?*c_void, @alignCast(@import("std").meta.alignment(?*c_void), out));
                                                                                         ^

Some random references

#4686 - using normal pointers (rather than c pointers) for pointers to opaque types
#2318 - translate-c pointer alignment
ae15022 - translate-c: Fix casting of function pointers

@Vexu
Copy link
Member

Vexu commented Oct 12, 2021

The first issue is a bug in peer type resolution @TypeOf(if (check != 0) arr else null) should be [*c]c_int.
The second one is a bug in translate-c only considering one level of indirection.

@Vexu Vexu added the translate-c C to Zig source translation feature (@cImport) label Oct 12, 2021
@Vexu Vexu added this to the 0.9.0 milestone Oct 12, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 26, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@Vexu Vexu modified the milestones: 0.11.0, 0.12.0 Apr 23, 2023
mlugg added a commit to mlugg/zig that referenced this issue May 16, 2023
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: ziglang#9917
Resolves: ziglang#15644
Resolves: ziglang#15709
mlugg added a commit to mlugg/zig that referenced this issue May 16, 2023
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

The new semantics around PTR for comptime-known fixed-width integers did
require some small changes to parts of the compiler, std, and
compiler_rt. Regarding the MachO changes, I spoke to @kubkon about how
best to do them, and this is what he suggested.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: ziglang#9917
Resolves: ziglang#15644
Resolves: ziglang#15709
mlugg added a commit to mlugg/zig that referenced this issue May 16, 2023
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

The new semantics around PTR for comptime-known fixed-width integers did
require some small changes to parts of the compiler, std, and
compiler_rt. Regarding the MachO changes, I spoke to @kubkon about how
best to do them, and this is what he suggested.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: ziglang#9917
Resolves: ziglang#15644
Resolves: ziglang#15709
mlugg added a commit to mlugg/zig that referenced this issue May 16, 2023
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

The new semantics around PTR for comptime-known fixed-width integers did
require some small changes to parts of the compiler, std, and
compiler_rt. Regarding the MachO changes, I spoke to @kubkon about how
best to do them, and this is what he suggested.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: ziglang#9917
Resolves: ziglang#15644
Resolves: ziglang#15709
mlugg added a commit to mlugg/zig that referenced this issue May 16, 2023
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

The new semantics around PTR for comptime-known fixed-width integers did
require some small changes to parts of the compiler, std, and
compiler_rt. Regarding the MachO changes, I spoke to @kubkon about how
best to do them, and this is what he suggested.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: ziglang#9917
Resolves: ziglang#15644
Resolves: ziglang#15709
mlugg added a commit to mlugg/zig that referenced this issue May 16, 2023
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

The new semantics around PTR for comptime-known fixed-width integers did
require some small changes to parts of the compiler, std, and
compiler_rt. Regarding the MachO changes, I spoke to @kubkon about how
best to do them, and this is what he suggested.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: ziglang#9917
Resolves: ziglang#15644
Resolves: ziglang#15709
mlugg added a commit to mlugg/zig that referenced this issue May 17, 2023
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

The new semantics around PTR for comptime-known fixed-width integers did
require some small changes to parts of the compiler, std, and
compiler_rt. Regarding the MachO changes, I spoke to @kubkon about how
best to do them, and this is what he suggested.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: ziglang#9917
Resolves: ziglang#15644
Resolves: ziglang#15709
mlugg added a commit to mlugg/zig that referenced this issue May 17, 2023
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

The new semantics around PTR for comptime-known fixed-width integers did
require some small changes to parts of the compiler, std, and
compiler_rt. Regarding the MachO changes, I spoke to @kubkon about how
best to do them, and this is what he suggested.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: ziglang#9917
Resolves: ziglang#15644
Resolves: ziglang#15709
mlugg added a commit to mlugg/zig that referenced this issue May 17, 2023
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

The new semantics around PTR for comptime-known fixed-width integers did
require some small changes to parts of the compiler, std, and
compiler_rt. Regarding the MachO changes, I spoke to @kubkon about how
best to do them, and this is what he suggested.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: ziglang#9917
Resolves: ziglang#15644
Resolves: ziglang#15709
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

4 participants