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

Undefined usage before assignment not detected if variable is mutable and assignment is deferred #19306

Closed
jmfrank63 opened this issue Mar 14, 2024 · 1 comment
Labels
question No questions on the issue tracker, please.

Comments

@jmfrank63
Copy link

Zig Version

0.12.0-dev.3290+b9cca3b63

Steps to Reproduce and Observed Behavior

I searched to my best ability but could not find any reports, so I hope this is genuine new:
The following correctly throws a compile time error:

pub fn main() void {
    const result: i32 = undefined;
    defer result = -555;
    if (result == -555) {
        std.debug.print("Result is: {}\n", .{result});
    }
    else {
        std.debug.print("Result is not -555 but {}\n", .{result});
    }
}

while the following is accepted:

pub fn main() void {
    var result: i32 = undefined;
    defer result = -555;
    if (result == -555) {
        std.debug.print("Result is: {}\n", .{result});
    }
    else {
        std.debug.print("Result is not -555 but {}\n", .{result});
    }
}

I am new to the language, and thus I appreciate the fact that the above might be a skill issue instead of a bug.
However, my expectation would be that undefined behaviour should be undefined behaviour independent of
Mutability of variables.
One run on my system lead to -1431655766, I doubt it will be the same on yours.
I also tested the above against the enum std.heap.Check and got the same undefined behaviour, though with an enum of only two members it is sort of predictable.

I am also not sure if for a newcomer like me, it is appropriate to provide suggestions, so feel free to ignore my thoughts.
Having said this, I would find it perfectly acceptable to disallow var x: type = undefined.
If, for some reason, it is necessary to have a mutable variable with an uninitialised state, one can always do:

const x: i32 = undefined;
...
x = 5;
var y = x;

As said, I am new and my first issue, feel free to close if not relevant.
Thank you for your hard work on this interesting language.

Expected Behavior

Either disallow var x: type = undefined
or
throw an error if used before initialisation. I appreciate with defer this might not trivial.

@jmfrank63 jmfrank63 added the bug Observed behavior contradicts documented or intended behavior label Mar 14, 2024
@Vexu
Copy link
Member

Vexu commented Mar 14, 2024

The reason it is detected with const is that the value of const variables is comptime known unlike with var, you would get the same error by using comptime var.

The issue for runtime safety for branching on undefined is #63 and #211

Detecting the issue in examples like yours would be simpler than to fully implement those other issues but it is also not that common.

@Vexu Vexu closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2024
@Vexu Vexu added question No questions on the issue tracker, please. and removed bug Observed behavior contradicts documented or intended behavior labels Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question No questions on the issue tracker, please.
Projects
None yet
Development

No branches or pull requests

2 participants