Skip to content

feat: struct impls#30

Merged
schell merged 4 commits into
mainfrom
feat/struct-impls
Jan 1, 2026
Merged

feat: struct impls#30
schell merged 4 commits into
mainfrom
feat/struct-impls

Conversation

@schell

@schell schell commented Jan 1, 2026

Copy link
Copy Markdown
Owner

These changes implement "Option C" from issue #6: support impl blocks for structs where methods use explicit receiver parameters (no self) and are called
with Type::method(receiver, args) syntax. These changes also support defining and referencing constants this way.

Functions

Rust:

  impl Light { pub fn attenuate(light: Light, d: f32) -> f32 { ... } }
  Light::attenuate(light, 2.0)

Generates WGSL:

  fn Light_attenuate(light: Light, d: f32) -> f32 { ... }
  Light_attenuate(light, 2.0)

Constants

Rust:

      impl Light {
          pub const DEFAULT_INTENSITY: f32 = 1.0;
      }
      let x = Light::DEFAULT_INTENSITY;
Generates WGSL:
      const Light_DEFAULT_INTENSITY: f32 = 1.0;
      let x = Light_DEFAULT_INTENSITY;

schell added 2 commits January 1, 2026 10:03
Implement Option C from issue #6: support impl blocks for structs where
methods use explicit receiver parameters (no `self`) and are called
with Type::method(receiver, args) syntax.

Changes:
- Add FnPath enum to support Type::method calls in addition to simple fn calls
- Add ItemImpl struct and parsing for impl blocks
- Transform impl methods to mangled free functions: Light::foo -> Light_foo
- Transform path calls at call sites: Light::foo(x) -> Light_foo(x)
- Reject trait impls, generic impls, and self receivers with helpful errors
- Improve error message for method calls with arguments
- Add comprehensive tests for parsing and code generation
- Add impl_example demonstrating the feature

Example:
  impl Light { pub fn attenuate(light: Light, d: f32) -> f32 { ... } }
  Light::attenuate(light, 2.0)

Generates:
  fn Light_attenuate(light: Light, d: f32) -> f32 { ... }
  Light_attenuate(light, 2.0)
Copilot AI review requested due to automatic review settings January 1, 2026 04:00
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR implements struct impl blocks with explicit receiver parameters (Option C from issue #6). Methods defined in impl blocks use explicit receiver parameters instead of self, and are called using Type::method(receiver, args) syntax, which gets transpiled to mangled function names like Type_method(receiver, args) in WGSL.

Key changes:

  • Added FnPath enum to support both simple function calls and type-qualified method calls
  • Added ItemImpl struct for parsing and generating code from impl blocks
  • Enhanced error messages to guide users toward the supported explicit receiver syntax

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/wgsl-rs-macros/src/parse.rs Adds FnPath enum, ItemImpl struct, parsing logic for impl blocks and Type::method calls, name mangling transformation, comprehensive tests for new functionality
crates/wgsl-rs-macros/src/code_gen/formatter.rs Implements code generation for FnPath and ItemImpl, outputting mangled function names (Type_method) in WGSL
crates/example/src/main.rs Adds impl_example module demonstrating the new impl block feature with Light struct methods
AGENTS.md Documents the SESSION.md ephemeral session file and references to DEVLOG.md for agent context
.gitignore Adds SESSION.md to gitignore as it's an ephemeral file not intended for version control

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread AGENTS.md Outdated
An ephemeral session file is maintained at SESSION.md, which you can use as scratch space for your editing session.
This file is not checked into git and should be used to persist context between editing sessions.
Think of this file as a mini DEVLOG.md, and when the session has ended and goals have been accomplished,
update the [DEVLOG](DEVLOG) with a brief, one line summary of the session, then remove the SESSION.md file.

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

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

The markdown link references [DEVLOG](DEVLOG) but should be [DEVLOG](DEVLOG.md) to properly link to the DEVLOG.md file. The same pattern is used in README.md line 136 with the correct .md extension.

Copilot uses AI. Check for mistakes.
Comment thread AGENTS.md Outdated

## DEVLOG.md file

The [DEVLOG](DEVLOG) is a set of long-lived development notes.

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

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

The markdown link references [DEVLOG](DEVLOG) but should be [DEVLOG](DEVLOG.md) to properly link to the DEVLOG.md file. The same pattern is used in README.md line 136 with the correct .md extension.

Copilot uses AI. Check for mistakes.
Extend impl block support to include associated constants in addition
to methods. Constants are accessed using Type::CONSTANT syntax and
are name-mangled to Type_CONSTANT in WGSL output.

Changes:
- Add ImplItem enum to hold both Fn and Const variants
- Add ItemConst::try_from_impl_const helper for parsing impl constants
- Add Expr::TypePath variant for Type::CONSTANT path expressions
- Update Expr path parsing to handle two-segment paths (Type::member)
- Update GenerateCode for ItemImpl to handle both constants and functions
- Add GenerateCode for Expr::TypePath (mangles to Type_member)
- Add tests for impl constants and TypePath expressions
- Update impl_example with associated constants

Example:
  impl Light {
      pub const DEFAULT_INTENSITY: f32 = 1.0;
  }
  let x = Light::DEFAULT_INTENSITY;

Generates:
  const Light_DEFAULT_INTENSITY: f32 = 1.0;
  let x = Light_DEFAULT_INTENSITY;
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
Comment thread crates/wgsl-rs-macros/src/parse.rs Fixed
@schell schell merged commit ee2d54f into main Jan 1, 2026
4 checks passed
@schell schell deleted the feat/struct-impls branch January 1, 2026 05:03
schell added a commit that referenced this pull request May 29, 2026
* feat: add struct impl blocks with explicit receiver syntax (closes #6)

Implement Option C from issue #6: support impl blocks for structs where
methods use explicit receiver parameters (no `self`) and are called
with Type::method(receiver, args) syntax.

Changes:
- Add FnPath enum to support Type::method calls in addition to simple fn calls
- Add ItemImpl struct and parsing for impl blocks
- Transform impl methods to mangled free functions: Light::foo -> Light_foo
- Transform path calls at call sites: Light::foo(x) -> Light_foo(x)
- Reject trait impls, generic impls, and self receivers with helpful errors
- Improve error message for method calls with arguments
- Add comprehensive tests for parsing and code generation
- Add impl_example demonstrating the feature

Example:
  impl Light { pub fn attenuate(light: Light, d: f32) -> f32 { ... } }
  Light::attenuate(light, 2.0)

Generates:
  fn Light_attenuate(light: Light, d: f32) -> f32 { ... }
  Light_attenuate(light, 2.0)

* other stuff

* feat: add associated constants to impl blocks

Extend impl block support to include associated constants in addition
to methods. Constants are accessed using Type::CONSTANT syntax and
are name-mangled to Type_CONSTANT in WGSL output.

Changes:
- Add ImplItem enum to hold both Fn and Const variants
- Add ItemConst::try_from_impl_const helper for parsing impl constants
- Add Expr::TypePath variant for Type::CONSTANT path expressions
- Update Expr path parsing to handle two-segment paths (Type::member)
- Update GenerateCode for ItemImpl to handle both constants and functions
- Add GenerateCode for Expr::TypePath (mangles to Type_member)
- Add tests for impl constants and TypePath expressions
- Update impl_example with associated constants

Example:
  impl Light {
      pub const DEFAULT_INTENSITY: f32 = 1.0;
  }
  let x = Light::DEFAULT_INTENSITY;

Generates:
  const Light_DEFAULT_INTENSITY: f32 = 1.0;
  let x = Light_DEFAULT_INTENSITY;

* PR fixup
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.

3 participants