impr: Strict type checking on logical and relational binary expressions operands#2647
impr: Strict type checking on logical and relational binary expressions operands#2647cieplypolar wants to merge 8 commits into
Conversation
|
pkg.pr.new packages benchmark commit |
Bundle size comparison (
|
| 🟢 Decreased | ➖ Unchanged | 🔴 Increased (max 0.24%) | ❔ Unknown |
|---|---|---|---|
| 0 | 300 | 21 | 0 |
import { ... } in PR vs import * as ... in PR (is the library tree-Shakeable?):
| Test | tsdown |
|---|---|
| tgpu_init.ts | 257.48 kB ( |
| tgpu_initFromDevice.ts | 256.97 kB ( |
| tgpu_resolve.ts | 188.19 kB ( |
| tgpu_resolveWithContext.ts | 188.13 kB ( |
| tgpu_bindGroupLayout.ts | 69.09 kB ( |
| tgpu_mutableAccessor.ts | 66.09 kB ( |
| tgpu_accessor.ts | 66.08 kB ( |
| tgpu_privateVar.ts | 65.38 kB ( |
| tgpu_workgroupVar.ts | 65.38 kB ( |
| tgpu_const.ts | 64.60 kB ( |
| tgpu_fragmentFn.ts | 38.04 kB ( |
| tgpu_fn.ts | 38.01 kB ( |
| tgpu_vertexFn.ts | 37.85 kB ( |
| tgpu_computeFn.ts | 37.56 kB ( |
| tgpu_vertexLayout.ts | 26.78 kB ( |
| tgpu_comptime.ts | 14.47 kB ( |
| tgpu_unroll.ts | 1.66 kB ( |
| tgpu_slot.ts | 1.57 kB ( |
| tgpu_lazy.ts | 1.22 kB ( |
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.
Resolution Time Benchmark---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.97, 2.02, 4.24, 6.77, 7.54, 12.67, 23.30, 27.69]
line [0.99, 2.02, 4.43, 7.35, 8.47, 12.85, 23.66, 26.95]
line [0.95, 1.92, 4.46, 6.54, 7.49, 9.92, 20.82, 21.90]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.28, 0.55, 0.76, 0.92, 1.17, 1.23, 1.46, 1.64]
line [0.31, 0.55, 0.74, 0.90, 1.19, 1.28, 1.53, 1.67]
line [0.30, 0.58, 0.75, 0.91, 1.29, 1.22, 1.44, 1.61]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.99, 2.09, 4.49, 6.76, 13.38, 26.58, 55.62, 115.17]
line [0.92, 2.31, 5.02, 7.27, 13.79, 27.67, 59.08, 120.08]
line [0.82, 2.09, 3.95, 6.55, 12.98, 26.54, 59.00, 113.66]
|
There was a problem hiding this comment.
Reviewed changes — strict operand type checking for relational and logical binary expressions, plus forced boolean conversion for ternary conditions.
- Added
binaryRelationalOpToStdMapand runtime type checks inwgslGenerator.tsfor<,<=,>,>=,===,!==,&&, and||. - Refactored comptime relational comparisons to use the same std-function suggestion map.
- Forced ternary conditions to
boolusingtryConvertSnippet. - Added
binaryLogicalOps.test.tsand moved short-circuit evaluation tests fromwgslGenerator.test.ts. - Added ternary non-bool condition failure test.
Important
Two concrete issues to address before merging: an unreachable error branch in the new ternary handling, and a typo in the comptime vector-comparison suggestion message.
⚠️ Unreachable ternary error branch
tryConvertSnippet either returns a Snippet or throws a WgslTypeError; it never returns a falsy value. The added if (!convertedTest) branch is dead code, so the custom "Cannot convert condition to bool" message is never shown.
Technical details
# Unreachable ternary error branch
## Affected sites
- `packages/typegpu/src/tgsl/wgslGenerator.ts:920` — `if (!convertedTest)` is always false.
## Required outcome
- Remove the unreachable branch, or replace `tryConvertSnippet` with a conversion helper that returns `null`/`undefined` on failure so the custom error message can be surfaced.
## Suggested approach
Drop the `if (!convertedTest)` guard and rely on `tryConvertSnippet`'s own error. If the custom message is required, wrap the conversion in a `try/catch` and rethrow with the desired text.⚠️ Typo in comptime vector suggestion message
The comptime path appends an extra single quote to the suggested std-function name. The output reads use 'std.ge''. instead of use 'std.ge'..
Technical details
# Typo in comptime vector suggestion message
## Affected sites
- `packages/typegpu/src/tgsl/wgslGenerator.ts:412` — template ends with `''.` instead of `'.`.
- `packages/typegpu/tests/tgsl/binaryLogicalOps.test.ts:271` — inline snapshot matches the typo.
## Required outcome
- Fix the trailing quote in the error template and update the matching inline snapshot.
## Suggested approach
Change `` ` For component-wise comparison, use 'std.${stdBinaryRelationalOp}''.` `` to `` ` For component-wise comparison, use 'std.${stdBinaryRelationalOp}'.` `` in `wgslGenerator.ts`, then update the snapshot in `binaryLogicalOps.test.ts`.Kimi K2 (free via Pullfrog for OSS) | 𝕏
|
Just a few notes from what we talked about in person:
|
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — the review-fix commit addresses both issues flagged in the prior review.
- Removed the unreachable
if (!convertedTest)branch in the ternary operator path; conversion failures now surface throughtryConvertSnippet's ownWgslTypeError. - Fixed the extra trailing single quote in the vector-vector comparison suggestion message (
std.ge''→std.ge). - Updated the matching inline snapshot in
binaryLogicalOps.test.ts.
I also ran the affected unit tests locally (binaryLogicalOps.test.ts, ternaryOperator.test.ts, wgslGenerator.test.ts) and all 122 tests passed.
Kimi K2 (free via Pullfrog for OSS) | 𝕏
7f8469e to
36dd367
Compare

Part of the #2645.
Addressed: <, >, >=, <=, ===, !==, || and &&.
Also, forced ternary operator to check if condition is boolean.