Skip to content

feat: expected failures as values, Result, try and Error (section 31) - #143

Merged
heyJordanParker merged 7 commits into
masterfrom
feat/result
Oct 16, 2026
Merged

heyJordanParker merged 7 commits into
masterfrom
feat/result

Conversation

@heyJordanParker

Copy link
Copy Markdown
Owner

Completes: §31

What users get

PHP# code returns expected failures as values. The standard library declares Result<T, TError : Error> 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.

public Result<Receipt, PaymentError> checkout(Cart cart)
{
    const order = try this.gateway.charge(cart);   // an Err returns from checkout here
    return Result.Ok(order.receipt());
}

Rules this PR adds

  • An enum declares type parameters. A case factory fixes them from its arguments and from the declared type it goes into, as a generic call does. A plain case of a generic enum is one object per list of type arguments.
  • Every enum may declare a computed property. An enum declares no stored property.
  • A bare Error always names the standard interface. import Error; and catch (Error ...) are refused.
  • try takes a Result in a method whose result type is a Result that 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's operator ==.

Bugs fixed on the way

  • A case pattern's field tests shared one span, so the bridge panicked on Result.Ok(Order order) when Order has fields.
  • SHARP_LEAVE left a new live where it returned, which crashed a try that returns its Err inside a lambda.

Harness files

Approved for this PR:

  • sharp/harness/app/PaymentError.sharp (new)
  • sharp/harness/app/CheckoutGateway.sharp (new)
  • sharp/harness/app/Order.sharp gains receipt()

sharp/bin/spec does not change.

Spec

docs/spec.md states each approved answer. The try-propagation example now writes try this.gateway.charge(cart), because a member is always read through this.. The three section 31 entries leave sharp/baseline/spec/.

Verification

  • sharp/bin/spec: every section 31 example passes. The 13 failures left are the baseline's own.
  • Zend/tests/sharp/results.phpt and results_preload.phpt pass in every opcache mode, each optimizer pass, both JITs, and ten runs under USE_ZEND_ALLOC=0 MallocScribble=1.
  • The analysis of a plain PHP corpus, build/gen_stub.php, run-tests.php and the SDK's sources, is the same before and after.

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
heyJordanParker merged commit 45644db into master Oct 16, 2026
20 checks passed
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
heyJordanParker added a commit that referenced this pull request Oct 16, 2026
The Results fixture of #143 becomes the Results folder, one type per file, which fixture() loads and its preload
requires in declaration order. Four #143 tests in semantics and the bridge give each type its own file, and hir maps
the new partial-type parse error.
@heyJordanParker
heyJordanParker deleted the feat/result 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