Skip to content

feat: readonly values and readonly methods (spec section 13) - #128

Merged
heyJordanParker merged 17 commits into
masterfrom
feat/readonly
Oct 14, 2026
Merged

heyJordanParker merged 17 commits into
masterfrom
feat/readonly

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Completes: §13

Problem

Section 13 gives readonly one rule: before a type, the value is only read, and after a method's parameters, this is. On master no part of it worked. readonly before a type was a parse error or "This modifier is not supported yet in PHP#.", readonly after the parameters was a parse error, and nothing in the analyzer read a readonly value. order.markPaid() and order.number = "A-1" on a readonly Order would have compiled once the refusals went.

Solution

Two marks, and the checks that read them. The engine runs the PHP it runs today.

  • readonly before a type is part of the type. The parser reads readonly Order as a type, as it reads Order?. The object type carries a readonly flag, and a read through a readonly value gives a readonly value, so "readonly reaches everything read through it" is one rule in the type. A narrowed, combined or inferred value keeps the flag.
  • readonly after the parameters marks the method, as its last clause, after on and uses. this is readonly in its body, in a lambda written there, and in int hash() and string toString() with no parameters. An operator reads its operands readonly, and one that returns an operand declares its return type readonly.

What a readonly value refuses, with the messages the rulings approved:

render cannot set order.number: order is readonly.
render cannot call order.markPaid: order is readonly.
  help: markPaid changes Order: it writes `this.paid`.
render cannot call order.totalIn: order is readonly.
  help: totalIn does not change Order, so declare it `totalIn(string currency) readonly`.
render cannot call order.save: order is readonly.
  help: save is a plain PHP method of Model, which PHP# does not check.
keep cannot pass order to Keeper.save: order is readonly. Declare the parameter `readonly Order order`.
keep cannot store (Order o) => { o.markPaid(); } in pay: it takes a writable `Order` where a readonly one is passed. Declare its parameter `readonly Order`.
settle cannot call order.pay: order is readonly. Declare its receiver `extension(readonly Order order)`.
operator + cannot return a: a is readonly. Declare the return type `readonly Tag`, or return a new value.
  • A write through a readonly value is invalid-property-write, a collection change written back through set included.
  • A call of an unmarked method is the new missing-readonly-marker. Only the effects solve knows whether the method changes this, so the call is recorded and reported after the solve, with the marker or the write that keeps it off.
  • A call of a plain PHP method is the same error at the call: a declared method, one a @method tag declares, and one a plain PHP class's __call runs.
  • A readonly value never goes where a writable one is expected. It is refused at an argument, a spread's elements, a return, a typed local, a field and a property, with the code each place reports today and the readonly to declare (question 1). The same holds inside types:
    • A method value, order.markPaid without the call, runs its method, so it is checked as the call.
    • A function value fits a function type only when its parameters take what the type passes, readonly included, and its return gives what the type promises.
    • A type parameter takes a readonly value only when written readonly T. A class's type parameter stays as the receiver fills it.
    • A value of readonly T stays readonly when passed, stored or narrowed.
    • A collection read through a readonly value stays readonly in its method calls, as order.lines.map(...).
  • An override or implementation of a readonly method must be readonly (incompatible-readonly-modifier, question 3).
  • A readonly that can't matter is an error that says why: before a type nothing can change through (int, string, Any, an enum, a struct, a function type, and a collection of those, as readonly List<int>), twice, in is or as, after a static method, a constructor, an enum method, an extension member, int hash() or string toString(), and before an operator's operand. readonly List<Order> still matters.
  • A plain PHP class under readonly is refused at its top and in any type argument, as readonly List<Model>.
  • Plain PHP messages never print readonly. A PHP# type reaches plain PHP as the type it runs as.
  • readonly on storage names { get; }: a field, a stored property, a member-declaring parameter and a class.

The bridge lowers readonly Order as Order and drops the method mark. Plain PHP passes any Order, may change it, and Reflection shows Order. A readonly service parameter takes the service of its type.

Section 13 states the three rulings the checker enforces, each with one sentence and one example: readonly-stays-readonly, readonly-overrides and readonly-cannot-matter. It also gains readonly-last-clause, and readonly-by-definition shows an operator that returns its operand with a readonly return type. Decision 051 now follows spec line 1193.

Section 13's type checks live in crates/analyzer/src/readonly_types.rs. readonly.rs keeps PHP's write-once properties only, as on master.

Harness changes

  • sharp/harness/app/Product.sharp: public readonly Money? priceIn(string currency) readonly => this.price;. Section 14.2's lambdas calls it on a readonly Line.
  • Zend/tests/sharp/TypeArgumentsChecks.sharp: Purse's operator + returned its operand, which is readonly by definition, so it builds a new Purse. The test that reads it checks only the class.

Verification

  • sharp/bin/spec checks 190 examples and matches sharp/baseline/spec/, with the three §13 entries, §14.1/function-types and §14.2/lambdas removed. BODY="Completes: §13" sharp/bin/spec --completes prints §13: all 8 checked examples pass.
  • sharp/bin/test --opcache passes all 238 PHP# engine tests in each of its three modes. readonly_values.phpt and type_arguments_entry_argument.phpt also pass with --repeat --passes: 20 modes, every optimizer pass alone and both JITs.
  • mago analyze on sharp/mago/composer/, a plain PHP project, prints the same 94 issues with master's mago and this branch's.
  • cargo test passes for syntax, semantics, names, codex, analyzer, sharp-bridge, hir, fingerprint and formatter, and cargo clippy -D warnings is clean on each.

Files

docs/
├── spec.md                                  section 13: the clause order and the readonly members
├── decisions/051-readonly.md                plain PHP under readonly follows spec line 1193
└── architecture/slices/13-readonly.md       the design note and the rulings
sharp/
├── baseline/spec/                           −§13 ×3, −§14.1/function-types, −§14.2/lambdas
├── harness/app/Product.sharp                priceIn is readonly
└── mago/
    ├── composer/src/Sdk/Syntax/NodeKind.php + ReadonlyHint
    ├── scripts/regen-analyzer-issue-codes.php + missing-readonly-marker
    └── crates/
        ├── syntax/        ReadonlyHint, Method::readonly, the clause order
        ├── semantics/     where readonly goes; the { get; } and can't-matter errors
        ├── codex/         the readonly flag on objects and class references; to_readonly, without_readonly,
        │                  is_readonly_value, function types compared, MetadataFlags::OPERATOR
        ├── analyzer/      reads, writes, calls, method values, spreads, overrides
        │   └── src/readonly_types.rs   new: plain PHP under readonly, and a readonly that can't matter
        ├── sharp-bridge/  lowers readonly to its type
        ├── names/         int hash() and string toString(), and operator names
        └── hir/, fingerprint/, formatter/   a Readonly arm each
Zend/tests/sharp/
├── ReadonlyValues.sharp                     new
├── readonly_values.inc                      new: the plain PHP caller
├── readonly_values.phpt                     new
└── TypeArgumentsChecks.sharp                operator + returns a new Purse

Problem

Section 13's three examples fail on master c6a673c: readonly before a
type is a parse error or refused as not supported yet, readonly after a
method's parameters is a parse error, and no checker code reads a readonly
marker on a value. So PHP# code cannot promise "I only read this" and have
the checker hold it.

Solution

The note states every line of section 13, its state observed with a release
mago built from master, its mechanism and the code it reuses: readonly as a
type hint and a flag on the object type, readonly after the parameters as a
method mark, the effects solve for the message that names the fix, and a
bridge that erases both. Seven questions wait for team-lead: chief among
them, whether a readonly value fits a type without readonly.

docs/
└── architecture/slices/13-readonly.md   new: the design, for approval
Problem

Spec section 13 writes `readonly` before a type, as in
`render(readonly Order order)`, and after a method's parameters, as in
`totalIn(string currency) readonly`. The parser read the first as PHP's
modifier, refused everywhere but on a promoted property, and the second as a
parse error, so none of the section's examples could reach the checker.

Solution

In a `.sharp` file, `readonly` followed by a type is a `ReadonlyHint`, a
type as `Function<R(P)>` is, wherever a type goes: a parameter, a return
type, a property, a local, a type argument and a function type's parameter.
Before `class` or another modifier it stays a modifier, and plain PHP is
unchanged. `readonly` after the parameters is `Method::readonly`, before `on`
and `uses`. The scanner builds a readonly type as its inner type with
`TNamedObject::is_readonly` on every object in it, and `this` in a method
with `MetadataFlags::READONLY` has the flag. The checks that read the flag
follow.

sharp/mago/
├── composer/src/Sdk/Syntax/NodeKind.php     + ReadonlyHint, regenerated
└── crates/
    ├── syntax/      ReadonlyHint, Method::readonly, the parser, node, walker, tests
    ├── names/       MethodParts::readonly
    ├── codex/       TNamedObject::is_readonly, TUnion::to_readonly, the scanner arm
    ├── analyzer/    get_this_type reads the method's flag
    └── fingerprint/, formatter/, hir/   a ReadonlyHint arm each
Problem

Spec section 13 allows `readonly` before a type a value has, and after a
method's parameters, and makes it a compile error on storage, which keeps
its value with `{ get; }`. Jordan ruled that a `readonly` that can't matter
is an error, that operators, `hash()` and `toString()` are readonly by
definition, and that the method's `readonly` is always its last clause. The
checker refused every `readonly` as "not supported yet".

Solution

The parser reads a method's `readonly` after `on` and `uses`, and one
written before them is an error that names the last place. Semantics
allows `readonly` before a parameter's, a return's, a computed property's,
a local's, a type argument's and a function type's type, and after the
parameters of a method with `this`. On a field, a stored property or a
member-declaring parameter, the error names the get-only property to write,
with the member's own access, type and name; on a class it names `{ get; }`.
`readonly` before `int`, `float`, `bool`, `string` or `Any`, twice, in an
`is` pattern, on an operator's operand, and after the parameters of a static
method, a constructor, an enum method, an extension member, `hash()` or
`toString()` is an error that says why it can't matter. The scanner marks
`hash()` and `toString()` readonly and an operator's operands readonly.

sharp/mago/crates/
├── syntax/      ReadonlyNotLastInSharp; readonly is the last clause; readonly readonly reads as two
├── semantics/   where readonly goes, the { get; } messages, the needless-readonly errors
├── names/       MethodParts::is_operator
├── codex/       the scanner marks hash(), toString() and operator operands
└── hir/         the new parse error
Problem

Spec section 13 makes everything read through a readonly value readonly,
and refuses a write, a call of a method without `readonly` and a hand-off
to a place that could change the value. Nothing in the analyzer read the
mark, so `order.markPaid()` on a `readonly Order` compiled.

Solution

A class reference keeps the `readonly` written before it until it
resolves to a class. A read through a readonly object gives its declared
type made readonly, and a write is `invalid-property-write`: "render
cannot set order.number: order is readonly." `this` is readonly in a
method marked `readonly`, in `hash()` and `toString()`, and in a lambda
written in one; an operator reads its operands readonly. A narrowed or
combined value keeps the mark.

A call of a PHP# method without `readonly` through a readonly value is
recorded, and the new `missing-readonly-marker` reports it after the
effects solve with the fix: the marker when the method changes nothing in
`this`, or the write that keeps it off. A plain PHP method is reported at
the call. An override or an implementation of a readonly method must keep
`readonly` (`incompatible-readonly-modifier`).

A readonly value passed, returned, or stored in a typed local, a field or
a property that takes a writable one is refused with the code that place
reports today, and the message names the `readonly` to declare. A
`readonly` before a plain PHP class or an enum is refused at the type.
The comparator ignores the mark, so a plain PHP file analyzes as before.

sharp/mago/crates/
├── codex/      TReference::Symbol keeps readonly; to_readonly, drops_readonly_of
├── names/      MethodParts loses is_operator, which nothing reads now
└── analyzer/
    ├── resolver/        reads give readonly, writes and unmarked calls refused
    ├── effects/         ReadonlyCall, reported after the solve
    ├── readonly.rs      readonly before a plain PHP class or an enum
    ├── invocation/, statement/return.rs, expression/assignment/   the four places
    ├── reconciler/      a narrowed value keeps readonly
    └── statement/       this, lambdas, operators, overrides
Problem

The bridge refused `readonly` before a type as a type the slice does not
hold, so no PHP# file that writes it could run. A readonly parameter also
asked the container for a service named `readonly Flags`, which no module
binds.

Solution

The bridge lowers `readonly Order` as `Order` and `Method::readonly` as
nothing, and its shape checks see through the mark: a readonly `Set`
parameter still makes its `Set`, and a readonly collection of services
still carries its service id. The service id drops the mark, because
`readonly` promises only what the constructor does with the service.
The checker no longer panics on a readonly collection, whose name
resolves to no class.

Operator fixtures that returned an operand now build a new value: an
operand is readonly in the body, so returning it where a writable value
goes is the refusal question 1 asks for.

sharp/mago/crates/
├── sharp-bridge/   Hint::Readonly lowers to its type; set_parameter and dropped_service see through it
├── codex/          TUnion::without_readonly; service_id drops the mark
└── analyzer/       readonly before a collection name is no class
# Conflicts:
#	sharp/mago/composer/src/Sdk/Syntax/NodeKind.php
#	sharp/mago/crates/analyzer/src/expression/assignment/mod.rs
#	sharp/mago/crates/analyzer/src/resolver/method.rs
#	sharp/mago/crates/analyzer/src/statement/class_like/method_signature.rs
#	sharp/mago/crates/analyzer/src/statement/class_like/mod.rs
#	sharp/mago/crates/codex/src/scanner/ttype.rs
#	sharp/mago/crates/names/src/binding.rs
#	sharp/mago/crates/sharp-bridge/src/lower.rs
#	sharp/mago/crates/syntax/src/cst/cst/class_like/method.rs
#	sharp/mago/crates/syntax/src/cst/cst/mod.rs
#	sharp/mago/crates/syntax/src/cst/node.rs
#	sharp/mago/crates/syntax/src/parser/internal/class_like/method.rs
#	sharp/mago/crates/syntax/src/parser/internal/statement.rs
#	sharp/mago/crates/syntax/src/parser/internal/type_hint.rs
Problem

Spec section 13's `readonly` is a promise the checker keeps about PHP#
code, so the engine must run a readonly parameter, return, method and
lambda as it runs them without the mark, and plain PHP must see the type
as written.

Solution

ReadonlyValues.sharp marks a parameter, a lambda parameter, a collection
parameter, a method and a return. readonly_values.phpt shows that a
constructor's readonly parameter declares no property, that plain PHP
passes and changes the value between calls and through a readonly
return, and that Reflection shows the type without `readonly`. It passes
without opcache, with the file cache, with each optimizer pass alone and
under both JITs.

TypeArgumentsChecks.sharp's `operator +` returned its operand, which is
readonly by definition, so it now builds a new Purse; the test that reads
it checks only the class.

Zend/tests/sharp/
├── ReadonlyValues.sharp      new
├── readonly_values.inc       new: the plain PHP caller
├── readonly_values.phpt      new
└── TypeArgumentsChecks.sharp operator + returns a new Purse
Problem

Jordan ruled that a method's `readonly` is its last clause, after `on`
and `uses`, and that any other place is a compile error that says to
write it last. `on OrderPaid readonly uses Mail` gave a generic parse
error instead. `value as readonly Order` compiled, though question 4
makes it an error as `is readonly Order` is.

Solution

The parser reads `readonly` after the parameters, after `on` and after
`uses`, and reports `ReadonlyNotLastInSharp` at each one a later clause
follows. Semantics refuses `readonly` in an `as` with "`as` keeps a
value readonly: write `as Order`." The analyzer test shows that a value
converted with `as` from a readonly one stays readonly.

sharp/mago/crates/
├── syntax/      readonly after on, before uses, names the last place
├── semantics/   readonly in an as is an error
└── analyzer/    a row for as, narrowing through generics and lambdas
Problem

Jordan ruled two rules section 13 did not state: a method's `readonly` is
its last clause, and operators, `hash()` and `toString()` are readonly by
definition. Decision 051 still said any plain PHP method runs through a
readonly value, which spec line 1193 contradicts. The design note still
listed its questions as open.

Solution

Section 13 states both rules, each with an example the spec check runs.
Decision 051 now chooses the spec's rule, with the platform-types option
rejected. The design note records the rulings and what was built.

The harness's `Product.priceIn` is a readonly method with a readonly
return, so `lambdas` (§14.2) calls it through a `readonly Line`. The
three §13 entries leave the failing list, and so do `function-types`
(§14.1) and `lambdas`, which pass now.

docs/
├── spec.md                              section 13: readonly-last-clause, readonly-by-definition
├── decisions/051-readonly.md            plain PHP under readonly follows spec line 1193
└── architecture/slices/13-readonly.md   the rulings
sharp/
├── harness/app/Product.sharp            priceIn is readonly
└── baseline/spec/                       −§13 ×3, −§14.1/function-types, −§14.2/lambdas
… readonly line

Problem

CI's patch coverage found 15 lines no test ran. One of them hid a gap:
a call that a plain PHP class's `__call` runs, with no `@method` tag,
skipped the readonly check, so `order.publish()` on a readonly `Order :
Model` compiled. The `readonly` before a union with a plain PHP class
named only that class as the fix, `write Model`, where the fix is
`Order|Model`.

Solution

A method no class declares is checked through the `__call` that runs it,
so the call is the plain PHP error. The plain PHP fix names the type
written after `readonly`. A resolved method always names a declared
method, so the lookup that can't fail is `unreachable!` with its reason.
The bridge's readonly arm in `nullable_hint` goes: only an operator
reaches it, and semantics refuses `readonly` on an operand. An untyped
parameter, a parse error, shows by its name in one expression.

Tests now run the rest: an external analyzer's class decodes without
readonly, a readonly value passed to a type parameter and to plain PHP,
top-level code, `readonly Order|Model`, `readonly Self`, a protected
field and `toString() readonly`.
# Conflicts:
#	sharp/mago/crates/analyzer/src/invocation/analyzer.rs
#	sharp/mago/crates/sharp-bridge/src/lower.rs
#	sharp/mago/crates/syntax/src/parser/internal/class_like/method.rs
Problem

The review of #128 found seven paths that hand a readonly value to code
that can change it: a method value (`const pay = order.markPaid`), a
lambda that takes its parameter writable where a function type passes it
readonly, a method's own type parameter not written `readonly`, a
`readonly T` passed, stored or narrowed, and a spread into a variadic
parameter. A collection read through a readonly value lost its readonly
in a method call, as `order.lines.map(...)`, because the call took the
declared collection type. `readonly List<Model>` was not refused,
`readonly List<int>` and `readonly Function<...>` passed though nothing
can change through them, and `string hash(string text)` was refused
though only `int hash()` and `string toString()` are readonly by
definition. Plain PHP messages printed `readonly`, and an extension call
through a readonly value read as a pass of an argument.

Solution

- `TAtomic::is_readonly_value` is the one predicate for a readonly value:
  a readonly object, or a value of `readonly T`. `drops_readonly_of` and
  the reconciler both use it, and a type parameter not written `readonly`
  takes no readonly value.
- `TCallableSignature::drops_readonly_of` compares a function value's
  parameters in reverse and its return forward, and the message names
  the parameter or the return.
- A method's own type parameter is compared as declared. A spread's
  elements meet the variadic parameter.
- `check_call_through_readonly` checks a call and a method value alike.
- `get_declared_collection` keeps a readonly value's collection readonly.
- `readonly_types.rs` holds section 13's type checks, out of
  `readonly.rs`, which checks PHP's write-once properties. It refuses a
  plain PHP class in a type argument, and a `readonly` before a type that
  holds no PHP# object: a struct, a function type, a collection of values.
- The scanner marks an operator with `MetadataFlags::OPERATOR`, so its
  name comes from the metadata, not the source text.
- `get_php_received_type` drops readonly, so plain PHP sees PHP types.
- An operator that returns an operand names both fixes.
- The top-level test and its `This code` text go: semantics refuses a
  statement at file level.

Section 13 states the three rulings it enforces, each with an example
line, and the operator that returns a readonly operand.

sharp/mago/crates/
├── codex/      is_readonly_value, function types, OPERATOR, received types
├── names/      is_hash_or_to_string, sharp_operator_symbol
├── semantics/  readonly after hash or toString with parameters is allowed
├── analyzer/
│   ├── src/readonly_types.rs    new: the types readonly holds
│   ├── src/readonly.rs          back to master
│   └── resolver/, invocation/, statement/return.rs, utils/
└── sharp-bridge/tests/lower.rs  a readonly Set holds objects
docs/
├── spec.md                      section 13: three rulings and the operator
└── architecture/slices/13-readonly.md
Clippy refuses a one-letter lifetime name, and rustfmt wraps the bridge
test's longer Order source.
`report_dropped_readonly` skips a declared member that is not a function
type, as `int` in `int|Function<void(readonly Order)>`. A row stores a
lambda that takes a writable `Order` there and gets the parameter's fix.
@heyJordanParker
heyJordanParker merged commit ea472ad into master Oct 14, 2026
24 of 26 checks passed
heyJordanParker added a commit that referenced this pull request Oct 14, 2026
… 25's class values

Problem:
#129 is at the head of the landing queue, and master gained the events
engine (#127) and readonly (#128) since its last merge.

Solution:
Both conflicts keep both sides:
- The parser starts a local declaration at `self`, which section 25's
  `Self copy = …` refusal needs, and at `readonly`, which #128 adds.
- The extension protocol's tests keep the class value test and #128's
  test that an external analyzer's class is never readonly.

sharp/mago/crates/
├── syntax/src/parser/internal/statement.rs   "self" | "readonly"
└── analyzer/src/external/protocol.rs         both tests
@heyJordanParker
heyJordanParker deleted the feat/readonly 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