Skip to content

Comparison with undefined has different behavior on Windows and Linux #22010

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

Closed
buzmeg opened this issue Nov 18, 2024 · 4 comments
Closed

Comparison with undefined has different behavior on Windows and Linux #22010

buzmeg opened this issue Nov 18, 2024 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@buzmeg
Copy link

buzmeg commented Nov 18, 2024

Zig Version

0.14.0-dev.2126+e27b4647d

Steps to Reproduce and Observed Behavior

I have the following code:

linfo("C: {} {}", .{foo == undefined, foo != undefined});

foo is a VkFence returned from vkCreateFence in Vulkan.

On Windows I get "false false" on Linux I got "true true".

Expected Behavior

I guess, runtime crash? We're clearly not supposed to be using "undefined" in runtime checks anymore.

@buzmeg buzmeg added the bug Observed behavior contradicts documented or intended behavior label Nov 18, 2024
@xdBronch
Copy link
Contributor

afaik this is more or less expected and allowed. comparing to undefined is only an error and considered UB when branching on it e.g. if (foo == undefined). simply observing the value is fine but the result isnt something to be relied on

@mlugg
Copy link
Member

mlugg commented Nov 18, 2024

Not a bug; runtime safety for branching on undefined (which std.fmt ultimately does, making this example UB) is #63. (That said, std.fmt could be improved to emit a compile error when passed a comptime-known undefined, as it is in this case.)

@mlugg mlugg closed this as not planned Won't fix, can't repro, duplicate, stale Nov 18, 2024
@buzmeg
Copy link
Author

buzmeg commented Nov 18, 2024

This fell out of an assert(), actually. The fmt was simply how I tracked it down. The assert(foo != undefined) passed on Linux but failed on Windows.

@Beyley
Copy link

Beyley commented Nov 18, 2024

This fell out of an assert(), actually. The fmt was simply how I tracked it down. The assert(foo != undefined) passed on Linux but failed on Windows.

You're not supposed to compare against undefined. That assert is undefined behaviour, as it's branching on undefined. undefined has no set value, and it does not make sense to compare against it anyway.

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

4 participants