Skip to content

fix: rewrite self/static/parent in variadic parameter types#427

Merged
nahime0 merged 4 commits into
illegalstudio:mainfrom
chadmandoo:fix/variadic-self-param-type
Jul 8, 2026
Merged

fix: rewrite self/static/parent in variadic parameter types#427
nahime0 merged 4 commits into
illegalstudio:mainfrom
chadmandoo:fix/variadic-self-param-type

Conversation

@chadmandoo

Copy link
Copy Markdown
Contributor

What

A self-typed variadic parameter fails to compile:

final class Bag {
    public function __construct(public string $x) {}
    public static function concat(self ...$items): self { /* ... */ }   // error
}

Cannot use 'self' as a type outside of a class

self/static/parent already work everywhere else — regular parameter types, return types, property types.

Why

substitute_relative_class_types_in_methods rewrites the relative class types self/static/parent to the concrete enclosing class before type resolution. It walks each method's regular parameters (method.params) and its return type (method.return_type) — but not the variadic parameter's type (method.variadic_type), which is a separate field. So self on a variadic param survives to resolve_type_expr, which rejects a bare self as "outside of a class" (relative types are expected to have been rewritten already).

How

Rewrite method.variadic_type alongside the regular params and the return type. Three lines.

Tests

tests/codegen/oop/relative_types.rs — new regression test test_self_typed_variadic_param: a static concat(self ...$items): self compiles and runs (Bag::concat(...)abc). Existing relative-types and variadic codegen tests still pass.

🤖 Generated with Claude Code

chadmandoo and others added 4 commits June 28, 2026 20:30
A `self`-typed variadic parameter (`concat(self ...$items)`) failed with
"Cannot use 'self' as a type outside of a class". The pass that rewrites the
relative class types `self`/`static`/`parent` to the enclosing class visited
each method's regular parameters and its return type, but not the variadic
parameter's type, so `self` survived to type resolution and was rejected.

Rewrite the variadic parameter's type annotation alongside the regular
parameters and the return type.

Adds a codegen regression test for a `self ...$items` variadic parameter.
@nahime0
nahime0 merged commit ea5a54a into illegalstudio:main Jul 8, 2026
58 checks passed
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.

2 participants