Skip to content

feat: static constructors, loop names, and the enum case field clash - #178

Merged
heyJordanParker merged 5 commits into
masterfrom
feat/classes-and-statements
Oct 19, 2026
Merged

heyJordanParker merged 5 commits into
masterfrom
feat/classes-and-statements

Conversation

@heyJordanParker

Copy link
Copy Markdown
Owner

Builds Jordan's rulings of 10-18 on static constructors (proposal 10, A) and loop names (proposal 7, A), and fixes the enum case field clash a review found.

Static constructors

static Rates() { … } runs once, right after the statics' initial values, in the first use of the class's statics. A throw there reaches that use, and every later use throws an Error that names the PHP# class and the failure, which it chains as the previous throwable:

The static constructor of App.Feeds.Feed threw RuntimeException: offline

Precedent: C#. No access modifier (CS0515), no parameters (CS0132), one per class, initial values run first, TypeInitializationException on each later use. Each type argument of a generic class runs its own.

Loop names

orders: for (…), and the same on while, do and for … of. break orders; and continue orders; reach it from an inner loop. Plain break and continue mean the innermost loop. An unused name is a compile error, and break 2 stays the typo parse error.

Precedent: Java, Kotlin, Swift and TypeScript labels, and Go for the unused-label error.

The enum case field clash

int cents => … beside case Some(int cents) is now a compile error that names both. Master compiled it, and the engine failed with "Cannot redeclare non-readonly property". Precedent: Kotlin (a property that hides a supertype's) and C# CS0102.

Leftovers of #176

  • Closure in a .sharp file gets what an unknown class name gets, at every use.
  • The analyzer test renamed to the_php_sharp_form_of_each_php_form_compiles, with its doc fixed.
  • Two stale doc comments in the checker's sharp.rs fixed.

Verification

  • sharp/bin/test (398 pass), --opcache --repeat (each mode 0 fail), and 14 optimizer passes 0 fail.
  • sharp/bin/spec: 250 examples, 0 fail. The self-test: 24 of 24.
  • New engine rows run in every mode: static_constructors*.phpt, loop_names*.phpt (no JIT, function JIT, tracing JIT, each with and without preloading), and enum_case_fields.phpt.
  • cargo fmt --check and cargo clippy --workspace --all-targets --all-features -D warnings are clean.

Static constructors, Jordan's ruling A on proposal 10. `static Rates() { … }`
runs once, right after the statics' initial values, in the same first use
of the class's statics, and never while no code uses one. The bridge lowers
it as the hidden `#statics`, after the initial value writes, and as the end
of `#init` for each type argument of a generic class. A throw there reaches
the first use, and every later use of the class's statics throws an Error
that names the PHP# class and the failure and chains the failure as its
previous throwable. The engine keeps the outcome in a hidden static, refuses
uses after a failure on the slow path, and caches no static's place while
the constructor runs, so no cached place skips the refusal.

Precedent: C#. A static constructor takes no access modifier (CS0515) and no
parameters (CS0132), a class has one, its type initializer runs the field
initializers first, and a throw makes every later use throw
TypeInitializationException with the failure as InnerException. Each closed
generic type runs its own, as in C#.

Loop names, Jordan's ruling A on proposal 7. `orders: for (…)`, `while`, `do`
and `for … of` take an optional name that only `break orders;` and
`continue orders;` read. Plain `break` and `continue` still mean the innermost
loop, and `break 2` stays the typo parse error. The bridge counts PHP loops
per PHP# loop, so a `for` whose counter moves into a fresh variable after
each pass still moves it on `continue name`.

Precedent: Java, Kotlin, Swift and TypeScript labels. An unused label is a
compile error, as in Go. A duplicate name around a loop and a jump to a name
no loop around it has are compile errors, as in Java.

The enum case field clash. A case field named like a property or a method of
its enum is a compile error that names both, where master compiled it and the
engine failed to link the case class. A case field is a member of its case
(spec section 21) and a case has every member of its enum, so one name means
one member (spec sections 4 and 14.3).

Precedent: Kotlin refuses a subclass property that hides its parent's, and C#
refuses two members of one name (CS0102). Swift's and Rust's associated
values are no members, so they set no precedent here.

The leftovers of #176. In a `.sharp` file, `Closure` gets what an unknown
class name gets at every use. The analyzer test of the PHP# form of each PHP
form is renamed, and two doc comments of the checker say a PHP form fails as
a typo does.
The patch-coverage check found six lines no Rust test ran.

The control-flow scan of a loop reads the loop's name in the match that
already reads its body, so the second match and its fallback arm go. A plain
PHP `break $levels;` now runs the arm for a level that is no integer literal.

The checker walks the statements around a jump up to the first ancestor that
is no statement, which is the lambda that holds it, so one `map_while` replaces
the `take_while` and its fallback arm.

A bridge test lowers a generic class's static constructor, which ends the
class's `#init` and gets no `#statics`.
The patch-coverage check found that no test scanned a `foreach` inside a loop,
so the line that reads its name never ran. The plain PHP row's inner loop is
now a `foreach`.
Each fix has a row that failed first.

- A static constructor that returns a value is a compile error, "A static
  constructor sets up its class's statics and returns nothing: write
  `return;`.", where `mago compile` crashed. C# refuses a value in a void
  member's `return` (CS0127).
- A static constructor runs at the first use of its class's statics only, so
  `new`, a computed constant and a field default that names a constant run
  none. Swift's static properties and Rust's `LazyLock` start at their first
  use the same way.
- A failed static constructor keeps failing through an inherited static: the
  engine checks the class the code names and each class above it, and never
  caches the place of a static while one of their static constructors has not
  returned.
- `Closure` gets exactly what `Nope` gets in every position. The binder
  resolves a PHP# name that would mean PHP's `Closure` to `\Closure`, a form no
  resolved name takes, so it names no class in a type, an `is` test, a static
  call, `new`, a constant read, `typeof`, a pattern, a `catch` or a header. The
  type-only exception in report_undefined_types goes.
- A static constructor writes its class's get-only statics, as C# assigns a
  `static readonly` field in one, and "Give `region` an initial value." drops
  its false clause.
- An enum case that gives no value to a property an interface of its enum
  states is a compile error that names the case and the interface's property,
  and an enum that gives no method an interface states gets the class error.
  The engine used to fail at load with "contains 1 abstract method". Kotlin
  and Swift refuse an enum that misses an interface member the same way.
- The hosts fixture's Report class has a static constructor, and each host
  prints how many times it ran: once for the app, under FrankenPHP's worker
  mode across requests too.
The patch-coverage check found that no test scanned a `do`-`while` inside
another loop, so the line that reads its name never ran. The named-jump test
gains a method whose `while` holds one.
@heyJordanParker
heyJordanParker merged commit 0dbcda3 into master Oct 19, 2026
24 checks passed
heyJordanParker added a commit that referenced this pull request Oct 19, 2026
…eside the review fixes

The one conflict is the note that lists what a method body takes: it
keeps #178's loop names and this branch's `new Self.name(...)` and
`new TItem.name(...)` with a `required` named constructor, and `Self.name`.
@heyJordanParker
heyJordanParker deleted the feat/classes-and-statements branch November 3, 2026 22:08
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.

1 participant