Skip to content

WGSL: Const. eval. short-circuiting #6302

@ErichDonGubler

Description

@ErichDonGubler

Description

Naga currently rejects programs with constant expressions such as this one:

const asdf = false && (123 / 0 > 0);

An uninformed reader might assume that dividing by zero yielding an error is expected. Indeed, Naga will happily report an error here:

Could not parse WGSL:
error: Division by zero
  ┌─ in.wgsl:1:24
  │
1 │ const asdf = false && (123 / 0 > 0);
  │                        ^^^^^^^ see msg

And yet, this is not standard behavior! The WGSL spec. clearly states that we should only be attempting const. eval. on the RHS of expressions when it cannot short-circuit. In the current WGSL spec.'s section 8.1.1:

The evaluation rule implies that short-circuiting operators && and || guard evaluation of their right-hand side subexpressions unless there is a subexpression that requires evaluation to determine a [static type](https://www.w3.org/TR/WGSL/#static-type).

Repro steps

Validate the above shader via naga as a file, i.e., in.wgsl. One can do this with a repo. checkout as follows:

$ cargo run -p naga-cli -- in.wgsl

Expected vs observed behavior

Already described, I hope!

Extra materials

Platform

Metadata

Metadata

Assignees

Labels

area: correctnessWe're behaving incorrectlyarea: ctsIssues stemming from the WebGPU Conformance Test Suitearea: naga processingPasses over IR in the middlearea: validationIssues related to validation, diagnostics, and error handlingtype: bugSomething isn't working

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions