feat: struct impls#30
Conversation
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)
There was a problem hiding this comment.
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
FnPathenum to support both simple function calls and type-qualified method calls - Added
ItemImplstruct 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.
| 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. |
There was a problem hiding this comment.
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.
|
|
||
| ## DEVLOG.md file | ||
|
|
||
| The [DEVLOG](DEVLOG) is a set of long-lived development notes. |
There was a problem hiding this comment.
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.
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;
* 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
These changes implement "Option C" from issue #6: support impl blocks for structs where methods use explicit receiver parameters (no
self) and are calledwith Type::method(receiver, args) syntax. These changes also support defining and referencing constants this way.
Functions
Rust:
Generates WGSL:
Constants
Rust: