Skip to content

undefined is a valid target for comparisons #14019

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
zxubian opened this issue Dec 21, 2022 · 2 comments
Closed

undefined is a valid target for comparisons #14019

zxubian opened this issue Dec 21, 2022 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@zxubian
Copy link

zxubian commented Dec 21, 2022

Zig Version

0.11.0-dev.892+3db8cffa3

Steps to Reproduce and Observed Behavior

test "undefined should not be used in comparisons" {
    var a: u8 = undefined;
    while (a == undefined) {}
    @panic("unreachable");
}

Actual Behaviour

test compiles and when run reaches the panic.

Expected Behavior

According to the language reference,

undefined means the value could be anything, even something that is nonsense according to the type. Translated into English, undefined means "Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used."

Thus, the expected behaviour for the above test is to return a compile message along the lines of:

Cannot use `undefined` in a comparison. Consider using an optional type instead.
@zxubian zxubian added the bug Observed behavior contradicts documented or intended behavior label Dec 21, 2022
@squeek502
Copy link
Collaborator

Possible duplicate of #597

@zxubian
Copy link
Author

zxubian commented Dec 22, 2022

Thanks @squeek502 !
After digging a bit more, I think this is a duplicate of #63, and will be considered in #1947 also.

I'll close this issue now.

@zxubian zxubian closed this as completed Dec 22, 2022
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

2 participants