Skip to content
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

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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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 @@ -229,6 +229,7 @@ By @wumpf in [#7144](https://github.com/gfx-rs/wgpu/pull/7144)
- Allow WGSL const declarations to have abstract types. By @jamienicol in [#7055](https://github.com/gfx-rs/wgpu/pull/7055) and [#7222](https://github.com/gfx-rs/wgpu/pull/7222).
- 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).
- 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 @@ -2102,10 +2102,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
17 changes: 14 additions & 3 deletions naga/src/proc/typifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,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 @@ -598,6 +597,18 @@ 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:?}, _)"
)));
}
}
crate::BinaryOperator::And
| crate::BinaryOperator::ExclusiveOr
| crate::BinaryOperator::InclusiveOr
Expand Down
45 changes: 45 additions & 0 deletions naga/tests/wgsl_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2837,3 +2837,48 @@ fn reject_utf8_bom() {
"#,
);
}

#[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(Vector { size: Bi, scalar: Scalar { kind: Bool, width: 1 } }, _)

"#,
);

check(
"fn foo(a: vec2<bool>, b: vec2<bool>) {
let y = a || b;
}",
r#"error: Incompatible operands: LogicalOr(Vector { size: Bi, scalar: Scalar { kind: Bool, width: 1 } }, _)

"#,
);
}