Skip to content

C Pointer via ptrFromInt of extern struct with array field cannot be dereferenced and indexed #25546

@timholzhey

Description

@timholzhey

Zig Version

0.16.0-dev.726+8efcfeaf1

Steps to Reproduce and Observed Behavior

Hi, while testing the current master branch for an embedded project I noticed a regression versus 0.15.1 that I was previously using.
A C pointer created via ptrFromInt of an extern struct that has an array field cannot be dereferenced and indexed into anymore. It behaves differently than a C pointer created from a struct reference. Similar code is generated with translate-c from MMIO logic that compiles with 0.15.1 but not on master. Target independant.

zig build-exe main.zig

main.zig:

const ExternStruct = extern struct {
    fields: [2]u8,
};

pub fn main() void {
    // This works
    var my_struct = ExternStruct{ .fields = .{0} ** 2 };
    const my_struct_c_ptr1 = @as([*c]ExternStruct, &my_struct);
    my_struct_c_ptr1.*.fields[0] = 1;

    // error: expected type '[2]u8', found 'comptime_int'
    const my_struct_c_ptr2 = @as([*c]ExternStruct, @ptrFromInt(0x1000));
    my_struct_c_ptr2.*.fields[0] = 1;
}

These have the same type for some reason:

@compileLog(my_struct_c_ptr2.*.fields);
@compileLog(my_struct_c_ptr2.*.fields[0]);
Compile Log Output:
@as([2]u8, [runtime value])
@as([2]u8, [runtime value])

Help is appreciated, thank you

Expected Behavior

Compiles

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions