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

const value changes unexpectedly #19927

Open
e0328eric opened this issue May 10, 2024 · 0 comments
Open

const value changes unexpectedly #19927

e0328eric opened this issue May 10, 2024 · 0 comments
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code.
Milestone

Comments

@e0328eric
Copy link

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

The bug occurs at the const variable has_bug in the following code.

const std = @import("std");

const NewlineHandler = struct {
    source: []const u8,
    chr0: ?u8 = null,
    chr1: ?u8 = null,
    idx: usize = 0,

    const Self = @This();

    pub fn init(source: []const u8) Self {
        var output = Self{ .source = source };

        output.nextChar();
        output.nextChar();

        return output;
    }

    pub fn next(self: *Self) ?u8 {
        const has_bug: ?u8 = if (self.chr0 == '\r') blk: {
            if (self.chr1 == '\n') {
                self.nextChar();
                break :blk self.chr0;
            } else break :blk '\n';
        } else self.chr0;

        std.debug.print("value of `has_bug`: {?c}, type: {s}\n", .{ has_bug, @typeName(@TypeOf(has_bug)) });
        self.nextChar();
        std.debug.print("value of `has_bug`: {?c}, type: {s}\n", .{ has_bug, @typeName(@TypeOf(has_bug)) });

        return has_bug;
    }

    fn nextChar(self: *Self) void {
        self.chr0 = self.chr1;
        self.chr1 = if (self.idx >= self.source.len) null else self.source[self.idx];
        self.idx += 1;
    }
};

pub fn main() void {
    var nh = NewlineHandler.init("apple");
    std.debug.print("{?c}\n", .{nh.next()});
}

The output of this code is

value of `has_bug`: a, type: ?u8
value of `has_bug`: p, type: ?u8
p

Expected Behavior

Since the variable has_bug marked as const, and its type is ?u8, it should print

value of `has_bug`: a, type: ?u8
value of `has_bug`: a, type: ?u8
a
@e0328eric e0328eric added the bug Observed behavior contradicts documented or intended behavior label May 10, 2024
@Vexu Vexu added backend-llvm The LLVM backend outputs an LLVM IR Module. miscompilation The compiler reports success but produces semantically incorrect code. labels May 10, 2024
@Vexu Vexu added this to the 0.13.0 milestone May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code.
Projects
None yet
Development

No branches or pull requests

2 participants