Repository navigation
feat: readonly values and readonly methods (spec section 13) - #128
Merged
Merged
Conversation
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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Completes: §13
Problem
Section 13 gives
readonlyone rule: before a type, the value is only read, and after a method's parameters,thisis. On master no part of it worked.readonlybefore a type was a parse error or "This modifier is not supported yet in PHP#.",readonlyafter the parameters was a parse error, and nothing in the analyzer read a readonly value.order.markPaid()andorder.number = "A-1"on areadonly Orderwould have compiled once the refusals went.Solution
Two marks, and the checks that read them. The engine runs the PHP it runs today.
readonlybefore a type is part of the type. The parser readsreadonly Orderas a type, as it readsOrder?. 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.readonlyafter the parameters marks the method, as its last clause, afteronanduses.thisis readonly in its body, in a lambda written there, and inint hash()andstring toString()with no parameters. An operator reads its operands readonly, and one that returns an operand declares its return typereadonly.What a readonly value refuses, with the messages the rulings approved:
invalid-property-write, a collection change written back throughsetincluded.missing-readonly-marker. Only the effects solve knows whether the method changesthis, so the call is recorded and reported after the solve, with the marker or the write that keeps it off.@methodtag declares, and one a plain PHP class's__callruns.readonlyto declare (question 1). The same holds inside types:order.markPaidwithout the call, runs its method, so it is checked as the call.readonly T. A class's type parameter stays as the receiver fills it.readonly Tstays readonly when passed, stored or narrowed.order.lines.map(...).incompatible-readonly-modifier, question 3).readonlythat 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, asreadonly List<int>), twice, inisoras, after a static method, a constructor, an enum method, an extension member,int hash()orstring toString(), and before an operator's operand.readonly List<Order>still matters.readonlyis refused at its top and in any type argument, asreadonly List<Model>.readonly. A PHP# type reaches plain PHP as the type it runs as.readonlyon storage names{ get; }: a field, a stored property, a member-declaring parameter and a class.The bridge lowers
readonly OrderasOrderand drops the method mark. Plain PHP passes anyOrder, may change it, and Reflection showsOrder. 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-overridesandreadonly-cannot-matter. It also gainsreadonly-last-clause, andreadonly-by-definitionshows an operator that returns its operand with areadonlyreturn type. Decision 051 now follows spec line 1193.Section 13's type checks live in
crates/analyzer/src/readonly_types.rs.readonly.rskeeps 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'slambdascalls it on areadonly Line.Zend/tests/sharp/TypeArgumentsChecks.sharp:Purse'soperator +returned its operand, which is readonly by definition, so it builds a newPurse. The test that reads it checks only the class.Verification
sharp/bin/specchecks 190 examples and matchessharp/baseline/spec/, with the three §13 entries,§14.1/function-typesand§14.2/lambdasremoved.BODY="Completes: §13" sharp/bin/spec --completesprints§13: all 8 checked examples pass.sharp/bin/test --opcachepasses all 238 PHP# engine tests in each of its three modes.readonly_values.phptandtype_arguments_entry_argument.phptalso pass with--repeat --passes: 20 modes, every optimizer pass alone and both JITs.mago analyzeonsharp/mago/composer/, a plain PHP project, prints the same 94 issues with master'smagoand this branch's.cargo testpasses for syntax, semantics, names, codex, analyzer, sharp-bridge, hir, fingerprint and formatter, andcargo clippy -D warningsis clean on each.Files