Repository navigation
feat: expected failures as values, Result, try and Error (section 31) - #143
Merged
Merged
Conversation
Section 31 has three failing examples on master: an enum takes no type parameters, `try` before a value is a parse error, and there is no Error interface while an enum holds no property. The note lists every line of the section, its state on master 39b3053, its mechanism and the code it reuses, with team-lead's answers to its seven questions. docs/architecture/slices/ └── 31-result.md new: the design and the answers in force
The standard library gains `Result<T, TError : Error>`, a generic enum with
the cases `Ok(T value)` and `Err(TError error)`, and the interface `Error`
with `string message { get; }`. `try result` gives the `Ok` value and
returns the `Err` from the method it stands in.
- An enum declares type parameters. Its cases are generic case classes, a
case factory fixes their type arguments as a generic call does, and a
plain case is one object per list of type arguments.
- Every enum may declare a computed property. The engine accepts a virtual
PHP# property on an enum.
- A bare `Error` always names the standard interface. `import Error;` and
`catch (Error ...)` are refused.
- `==` on a type parameter compares by its type argument's `operator ==`
through `\Sharp\Internal\equals`.
- A case pattern's field tests no longer share a span, which made the bridge
panic on `Result.Ok(Order order)` when `Order` has fields.
- SHARP_LEAVE frees a `new` still live where it returns, which crashed a
`try` that returns its `Err` inside a lambda.
- The harness gains `PaymentError` and `CheckoutGateway`, and
`Order.receipt()`, which section 31's examples call.
- The three section 31 baseline entries are removed.
\Sharp\Internal\equals keeps master's shape, which already compares any two values by value; section 31 adds a type parameter's value to its callers. The bridge sends a type parameter's `==` through it beside section 24's unchecked `Any?`.
…Result The patch-coverage job found that no row tested a case pattern whose subject type also holds a type that is no generic enum. `Result<Order, PaymentError>?` holds `null` beside the `Result`.
- A case call in a `match` arm takes the declared type the `match` goes into. - A case pattern a field holds takes the field's type arguments, so `Result.Ok(Option.Some(int value))` reads an `Option<int>`, and `match` counts nested cases toward exhaustiveness. - An enum refuses a computed `name`, and a backed enum a computed `value`. - An enum gets an interface's computed default, as a class does. The bridge gives the default's property its type text, which tells the engine an enum may hold it. - A computed property's type reaches its body, and `try` works in a `get` accessor, returning into the property's `Result` type. - An enum's static computed property runs. - `try` in a constructor or a `set` accessor reports the `void` message.
Master brings #142, #146, #147, #148 and #149. Each conflict keeps both sides' behavior: - A lambda's result type: master's `AnalysisArtifacts::lambda_result` is the one mechanism. `try` in a lambda reads it, and #143's `BlockContext::lambda_result_type` with its setters is gone. - A `match`'s declared type reaching its PHP and its arms: master's code, with `Expression::Match` kept in `takes_declared_type`, without which the PHP `match` takes no declared type. - `==` on a struct or a case: master's per-side type arguments for a generic struct, and a generic enum's case fields read the enum's type arguments from the side that gives them, through `with_type_arguments`. - A call's declared type: master's `declared_return_type` goes through `as_declared_ancestor`, so `Result.Ok(…)` into a `Result<int, E>` fixes the case's type arguments for the arguments and for the result. - The bridge: master's `generic_statics`, with `class_metadata` taking the class it describes, as a generic enum's case needs. - Spec section 11 keeps master's inference bullets and #143's rule for a static call of a generic class.
heyJordanParker
added a commit
that referenced
this pull request
Oct 16, 2026
Problem #145 was behind master, which gained #142, #143, #144, #146 to #150. Both sides changed the default names: this branch generates them from the standard library into `SHARP_CLASSES`, and #143 added `Result` to a hand-written list and made `Error` a default name nothing shadows. Solution One mechanism: `default_classes()` stays `NAMED_EFFECTS` plus the generated `SHARP_CLASSES`, regenerated from the merged library, which now holds `Sharp\Error` and `Sharp\Result`. #143's `fixed_default_class` stays, and `default_classes()` leaves out the class it fixes, so `Error` is never shadowable while `Result` is. sharp/mago/crates/names/src/ ├── lib.rs default_classes and fixed_default_class ├── sharp_classes.rs regenerated: Error and Result join └── internal/walker.rs both imports kept docs/architecture/slices/23-names-imports.md Error is the one fixed default name
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: §31
What users get
PHP# code returns expected failures as values. The standard library declares
Result<T, TError : Error>with the casesOk(T value)andErr(TError error), and the interfaceErrorwithstring message { get; }.try resultgives theOkvalue and returns theErrfrom the method it stands in.Rules this PR adds
Erroralways names the standard interface.import Error;andcatch (Error ...)are refused.trytakes aResultin a method whose result type is aResultthat holds the operand's error type. A lambda takes its result type from the function type it is written for.==on a value of a type parameter compares by its type argument'soperator ==.Bugs fixed on the way
Result.Ok(Order order)whenOrderhas fields.SHARP_LEAVEleft anewlive where it returned, which crashed atrythat returns itsErrinside a lambda.Harness files
Approved for this PR:
sharp/harness/app/PaymentError.sharp(new)sharp/harness/app/CheckoutGateway.sharp(new)sharp/harness/app/Order.sharpgainsreceipt()sharp/bin/specdoes not change.Spec
docs/spec.mdstates each approved answer. Thetry-propagationexample now writestry this.gateway.charge(cart), because a member is always read throughthis.. The three section 31 entries leavesharp/baseline/spec/.Verification
sharp/bin/spec: every section 31 example passes. The 13 failures left are the baseline's own.Zend/tests/sharp/results.phptandresults_preload.phptpass in every opcache mode, each optimizer pass, both JITs, and ten runs underUSE_ZEND_ALLOC=0 MallocScribble=1.build/gen_stub.php,run-tests.phpand the SDK's sources, is the same before and after.