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

wgsl-in: Improve assignment diagnostics #2056

Merged
merged 5 commits into from
Sep 15, 2022

Conversation

SparkyPotato
Copy link
Contributor

This improves the diagnostics on invalid assignment statements.
Fixes #2052.

Given:

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

Naga used to error with:

error: the left-hand side of an assignment must be a reference
  ┌─ test.wgsl:3:5
  │
3 │     v.xy = vec2(1);
  │     ^^^^ expression is not a reference

Now:

error: invalid left-hand side of assignment
  ┌─ test.wgsl:3:5
  │
3 │     v.xy = vec2(1);
  │     ^^^^ cannot assign to this expression
  │
  = note: WGSL does not support assignments to swizzles
  = note: consider assigning each component individually

Given:

fn f() {
    3 + 5;
}

Naga used to error with:

error: the left-hand side of an assignment must be a reference
  ┌─ test.wgsl:2:5
  │
2 │     3 + 5;
  │     ^ expression is not a reference

Now:

error: expected assignment or increment/decrement, found '3 + 5'
  ┌─ test.wgsl:2:5
  │
2 │     3 + 5;
  │     ^^^^^ expected assignment or increment/decrement

Given:

fn f() {
    3 + 5 = 10;
}

Naga used to error with:

error: the left-hand side of an assignment must be a reference
  ┌─ test.wgsl:2:5
  │
2 │     3 + 5 = 10;
  │     ^ expression is not a reference

Now:

error: invalid left-hand side of assignment
  ┌─ test.wgsl:2:5
  │
2 │     3 + 5 = 10;
  │     ^^^^^ cannot assign to this expression

Given:

fn f() {
    let a = 10;
	a = 20;
}

Naga used to error with:

error: invalid left-hand side of assignment
  ┌─ test.wgsl:3:2
  │
3 │     a = 20;
  │     ^ cannot assign to this expression

Now:

error: invalid left-hand side of assignment
  ┌─ test.wgsl:3:2
  │
3 │     a = 20;
  │     ^ cannot assign to this expression
  │
  = note: 'a' is an immutable binding
  = note: consider declaring it with `var` instead of `let`

Error has breaking changes made to it.

@jimblandy
Copy link
Member

@SparkyPotato This needs a rebase.

Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get the examples you put in the PR description into the test suite. It seems like they'd fit well in tests/wgsl-errors.rs, as a check-based test.

The simplification to Error::Unexpected is great. In the future, see if it's possible to separate out changes like that, that make a bunch of changes all over the place, into their own commits. This makes the series easier to review, because it makes it easier to see how simple the other changes are. But don't worry about that for this PR.

src/front/wgsl/mod.rs Outdated Show resolved Hide resolved
src/front/wgsl/mod.rs Outdated Show resolved Hide resolved
src/front/wgsl/mod.rs Outdated Show resolved Hide resolved
@jimblandy
Copy link
Member

@SparkyPotato This looks fantastic, btw - thanks very much!

Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - thank you!

@jimblandy jimblandy merged commit 5166c2a into gfx-rs:master Sep 15, 2022
@jimblandy
Copy link
Member

Okay, here's why cargo fmt -- --check wasn't complaining about the long lines in front/wgsl/mod.rs: rust-lang/rustfmt#3863

I'll push a workaround for that.

@SparkyPotato SparkyPotato deleted the improve-assigment-diagnostics branch February 1, 2023 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assignment to swizzle produces unhelpful error message
2 participants