Skip to content

compiler does not catch comparison with "undefined" #8520

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
mlawren opened this issue Apr 13, 2021 · 5 comments
Closed

compiler does not catch comparison with "undefined" #8520

mlawren opened this issue Apr 13, 2021 · 5 comments
Milestone

Comments

@mlawren
Copy link

mlawren commented Apr 13, 2021

Based on an example from the documentation:

const expect = @import("std").testing.expect;
const Empty = struct {
    pub const PI = 3.14;
    pub const PI2 = undefined;
};
test "struct namespaced variable" {
    expect(Empty.PI == 3.14);
    expect(Empty.PI2 == 3.14); // fails to fail
    expect(@sizeOf(Empty) == 0);

    // you can still instantiate an empty struct
    const does_nothing = Empty{};
}

The above appears to pass a zig test.

@g-w1
Copy link
Contributor

g-w1 commented Apr 14, 2021

This is undefined behavior, anything can happen. I think you might be confused with undefined in zig and undefined in other languages like javascript. In zig, branching on undefined means anything can happen, in fact llvm probably optimizes it out in this case. The real bug is zig not catching the undefined behavior here. #8529 solves a case close to this, but not exactly.

@mlawren mlawren changed the title std.testing.expect() fails to fail (0.7.1 Linux) compiler does not catch comparison with "undefined" Apr 14, 2021
@komuw
Copy link

komuw commented Apr 14, 2021

I think you might be confused with undefined in zig and undefined in other languages like javascript.

Is there an opportunity here for zig to choose another term that doesn't bring with it meaning from other languages?

@xackus
Copy link
Contributor

xackus commented Apr 16, 2021

Just to be sure nobody gets confused: undefined == 3.14 is perfectly legal and the result is undefined.
However, expect(undefined == 3.14) branches on the result of the comparison, which is illegal.

@avdva
Copy link

avdva commented Apr 26, 2021

Zig currently disallows comparison between an int and undefined inside if condition, but it is possible to compare a pointer and undefined.
Also, you can do this

var x: u8 = 0;
var b = x == undefined;
// or
// const c = undefined;
// b = c == undefined;
if (b) {}

which currently compiles fine.
So, the question is: should any ==undefined comparison produce a compilation error? I was working on a fix for #8056, and didn't find the place where this was explained.

@andrewrk
Copy link
Member

duplicate of #63

@andrewrk andrewrk added this to the 0.8.0 milestone Jun 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants