Skip to content

boolean-if on optional invalidly allowed at comptime #21742

Description

@rohlem

Zig Version

0.14.0-dev.1924+bdd3bc056

Steps to Reproduce and Observed Behavior

fnb(arg: ?bool) bool {
returnif (arg) trueelsefalse;
}
fnu(arg: ?u0) bool {
returnif (arg) trueelsefalse;
}
test {
_=comptimeb(true); //passes (yields false)_=comptimeu(0); //passes (yields false)//_ = b(true); //correctly issues compile error `expected type 'bool', found '?bool'`//_ = u(0); //correctly issues compile error `expected type 'bool', found '?u0'`
}

Expected Behavior

Optional-unwrapping if should always require a capture, as it does in runtime code.
IMO this should count as a miscompilation, since invalid code (where the intent is not clear) is processed (and yields an unintuitive result).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviormiscompilationThe compiler reports success but produces semantically incorrect code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions