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

General protection exception (no address available), or compiler crash, when accessing global anonymous struct variable from other file #22210

Open
TheZipCreator opened this issue Dec 12, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@TheZipCreator
Copy link

TheZipCreator commented Dec 12, 2024

Zig Version

0.14.0-dev.2253+3a6a8b8aa

Steps to Reproduce and Observed Behavior

a.zig:

const f = @import("./b.zig").f;

pub var foo = .{
    .x = @as(i32, 5)
};

pub fn main() !void {
    f();
}

pub fn setX(new: i32) void {
    foo.x = new;
}

b.zig:

const a = @import("./a.zig");

pub fn f() void{
    a.setX(10);
}

causes

General protection exception (no address available)
/tmp/test/a.zig:12:8: 0x1039835 in setX (a)
    foo.x = new;
       ^
/tmp/test/b.zig:4:11: 0x1037ecd in f (a)
    a.setX(10);
          ^
/tmp/test/a.zig:8:6: 0x1037eb8 in main (a)
    f();
     ^
/home/z/things/compilers/zig/lib/std/start.zig:617:37: 0x1037e91 in posixCallMainAndExit (a)
            const result = root.main() catch |err| {
                                    ^
/home/z/things/compilers/zig/lib/std/start.zig:248:5: 0x1037a8f in _start (a)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)

This doesn't happen if you give foo a type, like so:

// a.zig
pub var foo: struct { x: i32 } = .{
    .x = 5
};

or if you call the function from the same file:

// a.zig
pub fn main() !void {
    setX(10);
}

when trying to set the property directly, it causes a compiler crash:

// b.zig
pub fn f() void{
    a.foo.x = 10;
}
fish: Job 1, 'zig build-exe a.zig' terminated by signal SIGTRAP (Trace or breakpoint trap)

Expected Behavior

There shouldn't be a crash and the property should be updated correctly.

@TheZipCreator TheZipCreator added the bug Observed behavior contradicts documented or intended behavior label Dec 12, 2024
@TheZipCreator TheZipCreator changed the title General protection exception (no address available) when accessing global anonymous struct variable from other file General protection exception (no address available), or compiler crash, when accessing global anonymous struct variable from other file Dec 12, 2024
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
Projects
None yet
Development

No branches or pull requests

1 participant