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

Assignment to swizzle produces unhelpful error message #2052

Closed
jimblandy opened this issue Sep 10, 2022 · 1 comment · Fixed by #2056
Closed

Assignment to swizzle produces unhelpful error message #2052

jimblandy opened this issue Sep 10, 2022 · 1 comment · Fixed by #2056
Labels
area: front-end Input formats for conversion kind: diagnostics Error message should be better lang: WGSL WebGPU shading language

Comments

@jimblandy
Copy link
Member

Given the program:

fn f() {
   var v = vec2(0);
   v.xy = vec2(1);
}

Naga complains:

error: the left-hand side of an assignment must be a reference
  ┌─ assign-to-swizzle.wgsl:2:20
  │  
2 │      var v = vec2(0);
  │ ╭───────────────────^
3 │ │    v.xy = vec2(1);
  │ ╰───────^ expression is not a reference

Could not parse WGSL

This leads to questions in chat like:

Hi. What's the equivalent WGSL code to this?

p.xy = f(p.xy)

I get this error and have no idea how to write this nicely...

The error message did not convey to the user that swizzle assignment is simply unsupported.

@jimblandy jimblandy added lang: WGSL WebGPU shading language area: front-end Input formats for conversion kind: diagnostics Error message should be better labels Sep 10, 2022
@JCapucho
Copy link
Collaborator

We could bake in some detection for this case and add it as note to the diagnostic, it would look something like this:

error: the left-hand side of an assignment must be a reference
  ┌─ test.wgsl:3:4
  │
3 │    v.xy = vec2(1);
  │    ^^^^ expression is not a reference
  │
  = You seem to be trying to assign to a swizzle
    wgsl doesn't support assignments to swizzles

If we wanted something fancier we could try adding suggested fixes like rustc has, but that would be more complicated

error: the left-hand side of an assignment must be a reference
  ┌─ test.wgsl:3:4
  │
3 │    v.xy = vec2(1);
  │    ^^^^ expression is not a reference
  │
  = You seem to be trying to assign to a swizzle
    wgsl doesn't support assignments to swizzles
help: try using assigning each component individually
  ┌─ test.wgsl:3:4
  │
3 │    let a = vec2(1);
4 │    v.xy = a.x;
5 │    v.xy = a.y;
  │    ^^^
  │

jimblandy pushed a commit that referenced this issue Sep 15, 2022
Fixes #2052.

* improved assignment statement errors

* hint immutable binding

* fix clippy

* add diagnostic tests

* cleanup formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: front-end Input formats for conversion kind: diagnostics Error message should be better lang: WGSL WebGPU shading language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants