ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.16k stars 2.57k forks source link

packed struct field manpulation produces wrong results. #18914

Closed eddineimad0 closed 9 months ago

eddineimad0 commented 9 months ago

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

Code

const std = @import("std");

pub const WGLExt = packed struct {
    ARB_multisample: bool,
    ARB_framebuffer_sRGB: bool,
    EXT_framebuffer_sRGB: bool,
    ARB_create_context: bool,
    ARB_create_context_profile: bool,
    EXT_create_context_es2_profile: bool,
    ARB_create_context_robustness: bool,
    ARB_create_context_no_error: bool,
    EXT_swap_control: bool,
    EXT_colorspace: bool,
    ARB_pixel_format: bool,
    ARB_context_flush_control: bool,
};
var ext = WGLExt{
    .ARB_multisample = false,
    .ARB_framebuffer_sRGB = false,
    .EXT_framebuffer_sRGB = false,
    .ARB_create_context = false,
    .ARB_create_context_profile = false,
    .EXT_create_context_es2_profile = false,
    .ARB_create_context_robustness = false,
    .ARB_create_context_no_error = false,
    .EXT_swap_control = false,
    .EXT_colorspace = false,
    .ARB_pixel_format = false,
    .ARB_context_flush_control = false,
};
pub fn main() !void {
    ext.ARB_multisample = true;
    ext.ARB_framebuffer_sRGB = true;
    ext.ARB_create_context = true;
    ext.ARB_pixel_format = true;
    ext.ARB_context_flush_control = true;
    ext.ARB_create_context_profile = true;
    ext.ARB_create_context_robustness = true;
    ext.ARB_create_context_no_error = true;
    ext.EXT_swap_control = true;
    ext.EXT_colorspace = true;
    ext.EXT_framebuffer_sRGB = true;
    ext.EXT_create_context_es2_profile = true;

    std.debug.print("ext={}\n", .{ext});
}

Output:

ext=main.WGLExt{ 
  .ARB_multisample = true,
  .ARB_framebuffer_sRGB = true, 
  .EXT_framebuffer_sRGB = true, 
  .ARB_create_context = true, 
  .ARB_create_context_profile = true, 
  .EXT_create_context_es2_profile = true, 
  .ARB_create_context_robustness = true, 
  .ARB_create_context_no_error = true, 
  .EXT_swap_control = false, 
  .EXT_colorspace = true, 
  .ARB_pixel_format = true, 
  .ARB_context_flush_control = true 
}

Platform

I encountered the bug on windows x86_64, i don't know if it's reproducible on other platforms.

Expected Behavior

ext.EXT_swap_control should be set to true

eddineimad0 commented 9 months ago

moving ext variable from global scope to function local scope seems to produce the correct output.

Vexu commented 9 months ago

Fixed in master.