Skip to content

Conversation

@aleksanderkatan
Copy link
Contributor

@aleksanderkatan aleksanderkatan commented Aug 14, 2025

Changes:

  • added an error when an identifier is "_", starts with "__" or contains whitespace,
  • added auto renaming of identifiers for var/let declarations and function parameter declaration when needed,
  • added an error when a struct prop is named with an WGSL reserved word.

WGSL has a set of words that cannot appear as variable identifiers. Furthermore, "_" and identifiers starting with "__" are invalid as well, those are difficult to rename so we just throw in that case. Otherwise, makeValid just calls makeUnique when needed.

Identifiers not detected (because they are valid variable names):

  • types like u32, vec4f, mat2x2f, int, texture_2d etc.,
  • address spaces like storage, handle etc.,
  • builtin functions like textureSample, sin, dpdx etc.

@aleksanderkatan aleksanderkatan linked an issue Aug 14, 2025 that may be closed by this pull request
@github-actions
Copy link

github-actions bot commented Aug 14, 2025

pkg.pr.new

packages
Ready to be installed by your favorite package manager ⬇️

https://pkg.pr.new/software-mansion/TypeGPU/typegpu@b4d1a9c7e2724969b675b1585b673981cfbe26d1
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/noise@b4d1a9c7e2724969b675b1585b673981cfbe26d1
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@b4d1a9c7e2724969b675b1585b673981cfbe26d1

benchmark
view benchmark

commit
view commit

This comment was marked as outdated.

This comment was marked as outdated.

@aleksanderkatan aleksanderkatan marked this pull request as ready for review August 18, 2025 10:32
Copy link
Contributor

@reczkok reczkok left a comment

Choose a reason for hiding this comment

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

I wonder if there isn't a more straightforward way to handle the prefixing. RandomNameRegistry already will handle the names since the suffix _{someIdx} should always be enough. And for StrictNameRegistry we could just initialize the _usedNames with the keywords:

export class StrictNameRegistry extends NameRegistryImpl {
  /**
   * Allows to provide a good fallback for instances of the
   * same function that are bound to different slot values.
   */
  private readonly _usedNames: Set<string>;

  constructor() {
    super();
    this._usedNames = new Set<string>(keywordsAndReservedTokens);
  }
...
}

I applied this change and changed makeValid to:

  makeValid(primer: string): string {
    if (
      keywordsAndReservedTokens.has(primer)
    ) {
      return `${this.makeUnique(primer)}`;
    }
    return primer;
  }

And it seems to work? What do you think? Did I miss something? This does not handle the _ (but I would argue if someone tries to name his variable this the runtime error is deserved) and __ prefix case but that could be handled inside the primer validation.

@aleksanderkatan aleksanderkatan marked this pull request as draft August 22, 2025 14:51

This comment was marked as outdated.

@aleksanderkatan aleksanderkatan marked this pull request as ready for review August 25, 2025 08:17
Copy link
Contributor

@reczkok reczkok left a comment

Choose a reason for hiding this comment

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

Looks great 🚀🏗️

aleksanderkatan and others added 2 commits September 11, 2025 16:56
Co-authored-by: Konrad Reczko <66403540+reczkok@users.noreply.github.com>
@aleksanderkatan
Copy link
Contributor Author

@iwoplaza can you take a look at this? I don't know whether makeNameValid should call the listeners.

@aleksanderkatan aleksanderkatan merged commit 4b7edc9 into main Sep 22, 2025
5 checks passed
@aleksanderkatan aleksanderkatan deleted the fix/wgsl-keywords-among-identifiers branch September 25, 2025 11:22
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.

fix: WGSL keywords among identifiers

4 participants