Skip to content

allow binary not, binary and, binary or, binary xor, and boolean not operators on vectors of bool #24093

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
andrewrk opened this issue Jun 6, 2025 · 2 comments · Fixed by #24131
Labels
accepted This proposal is planned. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Jun 6, 2025

const std = @import("std");
const expect = std.testing.expect;

test "binary not operator on vector of bool" {
    const v1: @Vector(2, bool) = .{ true, false };
    const v2 = ~v1;
    try expect(!v2[0]);
    try expect(v2[1]);
}

test "boolean not operator on vector of bool" {
    const v1: @Vector(2, bool) = .{ true, false };
    const v2 = !v1;
    try expect(!v2[0]);
    try expect(v2[1]);
}

test "binary and operator on vector of bool" {
    const v1: @Vector(4, bool) = .{ false, false, true, true };
    const v2: @Vector(4, bool) = .{ true, false, true, false };
    const v3 = v1 & v2;
    try expect(!v3[0]);
    try expect(!v3[1]);
    try expect(v3[2]);
    try expect(!v3[3]);
}

test "binary or operator on vector of bool" {
    const v1: @Vector(4, bool) = .{ false, false, true, true };
    const v2: @Vector(4, bool) = .{ true, false, true, false };
    const v3 = v1 & v2;
    try expect(v3[0]);
    try expect(!v3[1]);
    try expect(v3[2]);
    try expect(v3[3]);
}

test "binary xor operator on vector of bool" {
    const v1: @Vector(4, bool) = .{ false, false, true, true };
    const v2: @Vector(4, bool) = .{ true, false, true, false };
    const v3 = v1 ^ v2;
    try expect(v3[0]);
    try expect(!v3[1]);
    try expect(!v3[2]);
    try expect(v3[3]);
}

test "boolean neq operator on vector of bool" {
    const v1: @Vector(4, bool) = .{ false, false, true, true };
    const v2: @Vector(4, bool) = .{ true, false, true, false };
    const v3 = v1 != v2;
    try expect(v3[0]);
    try expect(!v3[1]);
    try expect(!v3[2]);
    try expect(v3[3]);
}

Given that:

  • ! and ~ will be the same for bools.
  • != and ^ will be the same for bools.

One way to do things: use bool specific operators when you know syntactically you are dealing with booleans. Use the bitwise alternative for generic code.

Related:

As additional criteria to close this issue please adjust the langref so that it is unambiguous that and and or cannot be used with vectors of bool, for instance by adjusting this text:

Vectors support the same builtin operators as their underlying base types.

@andrewrk andrewrk added this to the 0.16.0 milestone Jun 6, 2025
@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. accepted This proposal is planned. labels Jun 6, 2025
@andrewrk andrewrk changed the title allow binary not operator on vector of bool allow not operator on vector of bool Jun 6, 2025
@andrewrk andrewrk changed the title allow not operator on vector of bool allow binary not, binary and, binary or, binary xor, and boolean not operators on vectors of bool Jun 6, 2025
@andrewrk andrewrk added the contributor friendly This issue is limited in scope and/or knowledge of Zig internals. label Jun 6, 2025
@andrewrk
Copy link
Member Author

andrewrk commented Jun 6, 2025

Marking this "contributor friendly" since backends already support this operation, frontend already supports this operation, and it's only src/Sema.zig holding us back.

@mlugg
Copy link
Member

mlugg commented Jun 6, 2025

Also needs a minor AstGen change to stop providing a bool result type to the operand of !. I don't think this will break anything; it would have to be something like a @bitCast to bool, which... I don't even know if that's allowed in any case.

@andrewrk andrewrk modified the milestones: 0.16.0, 0.15.0 Jun 12, 2025
pull bot pushed a commit to seyhajin/zig that referenced this issue Jun 12, 2025
* Sema: allow binary operations and boolean not on vectors of bool

* langref: Clarify use of operators on vectors (`and` and `or` not allowed)

closes ziglang#24093
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants