zig icon indicating copy to clipboard operation
zig copied to clipboard

stage2: zero sized global variable forces const pointer

Open leecannon opened this issue 3 years ago • 0 comments

Zig Version

0.10.0-dev.3362+e863292fe

Steps to Reproduce

const ZERO_SIZED = true;

var might_be_zero_sized: MightBeZeroSized = .{};

pub fn main() void {
    might_be_zero_sized.aFunctionThatTakesAPointer();
}

const MightBeZeroSized = struct {
    a_field_that_might_be_void: if (ZERO_SIZED) void else usize = if (ZERO_SIZED) {} else 1,

    pub fn aFunctionThatTakesAPointer(self: *MightBeZeroSized) void {
        _ = self;
    }
};

Expected Behavior

Compiles fine both with ZERO_SIZED equals true and false.

Actual Behavior

When ZERO_SIZED is true:

/home/lee/src/fff/src/main.zig:6:24: error: expected type '*main.MightBeZeroSized', found '*const main.MightBeZeroSized'
    might_be_zero_sized.aFunctionThatTakesAPointer();
    ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lee/src/fff/src/main.zig:6:24: note: cast discards const qualifier

Moving the var to be a local prevents the issue:

pub fn main() void {
    var might_be_zero_sized: MightBeZeroSized = .{};
    might_be_zero_sized.aFunctionThatTakesAPointer();
}

leecannon avatar Jul 28 '22 18:07 leecannon