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

error: attempt to dereference non-pointer type '?*. on generated cimport code #6987

Closed
brentp opened this issue Nov 5, 2020 · 2 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@brentp
Copy link

brentp commented Nov 5, 2020

given this code for htslib

const std = @import("std");

const hts = @cImport({
    // htslib.org
    @cInclude("htslib/hts.h");
    @cInclude("htslib/sam.h");
});

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    var v = hts.hts_version();
    try stdout.print("version, {s}!\n", .{v});

    var fh = hts.hts_open("/data/human/hg002.22.bam", "r");
    if (fh == null) {
        std.debug.warn("null file: {}", .{v});
    }
    try stdout.print("eof?:{d}\n", .{hts.hts_check_EOF(fh)});

    var hdr = hts.sam_hdr_read(fh);
    try stdout.print("null header?:{d}\n", .{hdr == null});

    var idx = hts.sam_index_load(fh, "/data/human/hg002.22.bam");
    try stdout.print("null idx?:{d}\n", .{idx == null});

    var qiter = hts.sam_itr_querys(idx, hdr, "22:450000");
    try stdout.print("null itr?:{d}\n", .{qiter == null});

    var b = hts.bam_init1();
    // ? https://github.com/ziglang/zig/issues/1580
    var ret = hts.sam_itr_next(fh, qiter, b);

    try stdout.print("next ret?:{d}\n", .{ret});
}

with this build.zig:

const Builder = @import("std").build.Builder;

pub fn build(b: *Builder) void {
    // Standard target options allows the person running `zig build` to choose
    // what target to build for. Here we do not override the defaults, which
    // means any target is allowed, and the default is native. Other options
    // for restricting supported target set are available.
    const target = b.standardTargetOptions(.{});

    // Standard release options allow the person running `zig build` to select
    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
    const mode = b.standardReleaseOptions();



    const exe = b.addExecutable("testing", "src/main.zig");
    exe.setTarget(target);
    exe.setBuildMode(mode);
    exe.linkSystemLibrary("c");
    exe.linkSystemLibrary("hts");
    exe.linkSystemLibrary("z");
    exe.install();

    const run_cmd = exe.run();
    run_cmd.step.dependOn(b.getInstallStep());

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}

I see:

$ zig build run
./zig-cache/o/dcfbafb6d0153b3f88717a785e6d092a/cimport.zig:863:16: error: attempt to dereference non-pointer type '?*.cimport:3:13.struct_unnamed_7'
    if (!(htsfp.*.is_bgzf != 0) and !(htsfp.*.is_cram != 0)) {
               ^
testing...The following command exited with error code 1:
/home/brentp/src/zig/zig-linux-x86_64-0.6.0+dbc11be03/zig build-exe /home/brentp/src/zig/zig-linux-x86_64-0.6.0+dbc11be03/t/src/main.zig --library c --library hts --library z --cache-dir /home/brentp/src/zig/zig-linux-x86_64-0.6.0+dbc11be03/t/zig-cache --name testing --enable-cache 
error: the following build command failed with exit code 1:
/home/brentp/src/zig/zig-linux-x86_64-0.6.0+dbc11be03/t/zig-cache/o/8e1f36000a4ecc95fc3c8c1975b6999b/build /home/brentp/src/zig/zig-linux-x86_64-0.6.0+dbc11be03/zig /home/brentp/src/zig/zig-linux-x86_64-0.6.0+dbc11be03/t /home/brentp/src/zig/zig-linux-x86_64-0.6.0+dbc11be03/t/zig-cache run

This happens only after I add the lines starting with: var ret = hts.sam_itr_next(fh, qiter, b);

There is a similar error reported in one of the comments in #1580

@alexnask alexnask added bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport) labels Nov 7, 2020
@alexnask alexnask added this to the 0.8.0 milestone Nov 7, 2020
@brentp
Copy link
Author

brentp commented Nov 21, 2020

I think this happens when the struct is opaque, but then the header still accesses the fields. In this case, htsFile is opaque:

const struct_unnamed_7 = @Type(.Opaque); // /usr/local/include/htslib/hts.h:235:14: warning: struct demoted to opaque type - has bitfield
pub const htsFile = struct_unnamed_7;

but then the header still accesses fields within htsFile

@Vexu Vexu modified the milestones: 0.8.0, 0.9.0 Jun 4, 2021
@Vexu
Copy link
Member

Vexu commented Jun 13, 2021

With #9100 the function with the dereference is should now be demoted to extern, bitfield support is tracked in #1499.

@Vexu Vexu closed this as completed Jun 13, 2021
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

3 participants