Skip to content

[naga] Disallow logical operators && and || on vectors #7368

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

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ By @wumpf in [#7144](https://github.com/gfx-rs/wgpu/pull/7144)
- Allows override-sized arrays to resolve to the same size without causing the type arena to panic. By @KentSlaney in [#7082](https://github.com/gfx-rs/wgpu/pull/7082).
- Allow abstract types to be used for WGSL switch statement selector and case selector expressions. By @jamienicol in [#7250](https://github.com/gfx-rs/wgpu/pull/7250).
- Apply automatic conversions to `let` declarations, and accept `vecN()` as a constructor for vectors (in any context). By @andyleiserson in [#7367](https://github.com/gfx-rs/wgpu/pull/7367).
- The `&&` and `||` operators are no longer allowed on vectors. By @andyleiserson in [#7368](https://github.com/gfx-rs/wgpu/pull/7368).

#### General

Expand Down
7 changes: 5 additions & 2 deletions naga/src/proc/constant_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2227,10 +2227,13 @@ impl<'a> ConstantEvaluator<'a> {
| BinaryOperator::And
| BinaryOperator::ExclusiveOr
| BinaryOperator::InclusiveOr
| BinaryOperator::LogicalAnd
| BinaryOperator::LogicalOr
| BinaryOperator::ShiftLeft
| BinaryOperator::ShiftRight => left_ty,

BinaryOperator::LogicalAnd | BinaryOperator::LogicalOr => {
// Not supported on vectors
return Err(ConstantEvaluatorError::InvalidBinaryOpArgs);
}
};

let components = components
Expand Down
18 changes: 15 additions & 3 deletions naga/src/proc/typifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,8 @@ impl<'a> ResolveContext<'a> {
| crate::BinaryOperator::Less
| crate::BinaryOperator::LessEqual
| crate::BinaryOperator::Greater
| crate::BinaryOperator::GreaterEqual
| crate::BinaryOperator::LogicalAnd
| crate::BinaryOperator::LogicalOr => {
| crate::BinaryOperator::GreaterEqual => {
// These accept scalars or vectors.
let scalar = crate::Scalar::BOOL;
let inner = match *past(left)?.inner_with(types) {
Ti::Scalar { .. } => Ti::Scalar(scalar),
Expand All @@ -607,6 +606,19 @@ impl<'a> ResolveContext<'a> {
};
TypeResolution::Value(inner)
}
crate::BinaryOperator::LogicalAnd | crate::BinaryOperator::LogicalOr => {
// These accept scalars only.
let bool = Ti::Scalar(crate::Scalar::BOOL);
let ty = past(left)?.inner_with(types);
if *ty == bool {
TypeResolution::Value(bool)
} else {
return Err(ResolveError::IncompatibleOperands(format!(
"{op:?}({:?}, _)",
ty.for_debug(types),
)));
}
}
crate::BinaryOperator::And
| crate::BinaryOperator::ExclusiveOr
| crate::BinaryOperator::InclusiveOr
Expand Down
45 changes: 45 additions & 0 deletions naga/tests/naga/wgsl_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3155,6 +3155,51 @@ fn matrix_vector_pointers() {
);
}

#[test]
fn vector_logical_ops() {
// Const context
check(
"const and = vec2(true, false) && vec2(false, false);",
r###"error: Cannot apply the binary op to the arguments
┌─ wgsl:1:13
1 │ const and = vec2(true, false) && vec2(false, false);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ see msg

"###,
);

check(
"const or = vec2(true, false) || vec2(false, false);",
r###"error: Cannot apply the binary op to the arguments
┌─ wgsl:1:12
1 │ const or = vec2(true, false) || vec2(false, false);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ see msg

"###,
);

// Runtime context
check(
"fn foo(a: vec2<bool>, b: vec2<bool>) {
let y = a && b;
}",
r#"error: Incompatible operands: LogicalAnd(vec2<bool>, _)

"#,
);

check(
"fn foo(a: vec2<bool>, b: vec2<bool>) {
let y = a || b;
}",
r#"error: Incompatible operands: LogicalOr(vec2<bool>, _)

"#,
);
}

#[test]
fn issue7165() {
// Regression test for https://github.com/gfx-rs/wgpu/issues/7165
Expand Down