Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Spec doesn't allow local variables with the same names as parameters #3502

@jimblandy

Description

@jimblandy

I think functions like this should be allowed:

fn f(a: i32) -> bool {
    let a = true;
    return a;
}

However, the spec says:

Two declarations in the same WGSL source program must not simultaneously:

  • introduce the same identifier name, and

  • have the same end-of-scope.

"§9.1. Declaring a User-defined Function" says:

The identifier is in scope until the end of the function.

"§6. Variable and Value Declarations" says:

The name is available for use in the statement immediately after its declaration until the end of the brace-delimited list of statements immediately enclosing the declaration.

These seem the same to me. They're definitely not clearly different.


I think this is uncontroversial, but here's why I think the code at top should be permitted:

  • Shadowing is definitely permitted in inner blocks, so it seems consistent to permit it at the top-level block too.
  • Such shadowing is permitted in many popular languages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    wgslWebGPU Shading Language Issues

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions