这是indexloc提供的服务,不要输入任何密码
Skip to content

Have "var" as syntactic sugar for explicit pointer refs/derefs #1456

@kvark

Description

@kvark

This is a follow-up from the discussion in #1334 (comment) as well as #1455

Having real pointers with explicit "take reference" (operator &) and de-reference (operator *) is future-proof, and it opens the doors for use-cases where a local variable needs to be modified by a called function:

fn fun(inout_ptr: ptr<function, i32>) {
  *inout_ptr = 4;
}

However, we also don't want the noise of references/de-references to be required when working with function-local variables, such as loop iteration indices:

for (var i: i32 = 0; i<5; i = i + 1) {
  array[i] = 1.0;
}

I think we should have both. var would be syntactic sugar for a function-allocated piece of data that is de-referenced. We don't have to describe, at least at start, the actual way of declaring this piece of data ("decl_storage").

var x: f32;
x = 5.0; // works as l-value
const y: f32 = x; // works as r-value

When working with compound structures, the . operator takes precedence over de-referencing:

struct Foo {
  x: i32,
  y: f32,
};
fn bar(my_foo: ptr<function, Foo>) {
  *my_foo.x = 5;
  *my_foo.y = 1.0;
  const z: f32 = *my_foo.y; // r-value
}

When working with var, it works the same way, just with dereferencing hidden behind the sugar:

var foo: Foo; // function-local var
foo.x = 5; // is sugar for `*(&foo).x = 5;`
foo.y = 1.0; // is sugar for `*(&foo).y = 1.0;`
fun(&foo.x); // taking explicit pointer of a variable

Metadata

Metadata

Assignees

No one assigned

    Labels

    wgslWebGPU Shading Language Issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions