Skip to content

feat: uses none states that a function type is pure, so a setter keeps a pure function - #164

Merged
heyJordanParker merged 5 commits into
masterfrom
feat/uses-none
Oct 17, 2026
Merged

heyJordanParker merged 5 commits into
masterfrom
feat/uses-none

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

A PHP# method takes on the effects of each function it is given, with nothing written, so it can't store, return or let an escaping lambda capture a function parameter unless the parameter's type states its effects. No type said "pure", so a setter, a registration method or a builder could not keep a pure function: only a constructor parameter that declares a property could. Jordan approved Option A on 10-17: uses none states that a function type is pure. Decision 82 records it, with the override rule the review added.

What a PHP# developer can now write

public class Pricing
{
    private Function<int(int)> rate = (cents) => cents;

    public void setRate(Function<int(int) uses none> rate)
        => this.rate = rate;                                      // compiles

    public void apply(Function<int(int)> step)
        => this.rate = step;                                      // compile error: take a `Function<int(int) uses none>`
}

pricing.setRate((cents) => cents * 2);                            // compiles
pricing.setRate((cents) => this.rates.convert(cents));            // compile error: so a pure `Function<int(int) uses none>` cannot hold it
  • uses none is the empty effect list, wherever a function type's uses goes: a parameter, a field, a property, a return type, a type argument, an interface method's parameter.
  • A uses none parameter may be stored, returned and captured, as one whose type states effects may.
  • Passing a function with an effect to it is refused at the call, naming the effect and the type as the parameter writes it:
    • a lambda: "This lambda calls global.fetch, which has the effect Http, so a pure Function<int(int) uses none> cannot hold it."
    • a function value: "mailer has the effect Mail, so a pure Function<int(int) uses none> cannot hold it." Every argument whose function type fits but for its effects gets these words, in place of "Invalid argument type … expected … but found …". A parameter that states effects names only those it does not list: "both has the effect Mail, so a Function<int(int) uses Http> cannot hold it."
    • a union names its function member: "so the pure Function<int(int) uses none> of int|Function<int(int) uses none> cannot hold it."
  • A function inside a List or a Map is held to the element type, in a collection literal passed as an argument, in a field's initial value, and in a write through an index.
  • uses none beside another effect is refused: "none states that the function has no effect, so uses lists nothing beside it." A misspelled effect beside it, as uses none, Htp, is still reported.
  • uses none on a method is refused, with or without a body: "A method without uses is pure already, so delete uses none."
  • A field's Function<int(int) uses none> is the pure type its Function<int(int)> already is, with no warning.
  • The refusal to store an untyped function parameter says why and names the fix: "step can't be stored in a field: it takes on each caller's effects." Its help names the place's own type when it states effects, Function<int(int) uses none> when the place is pure, and otherwise "Take a Function<int() uses none>, or state its effects in place of none."

An override takes on effects wherever the method it overrides does

A call through Rater takes on the function's effects at the call, so an implementation that keeps that function would run them later, where no caller counts them. The override's parameter is now refused when it states effects, uses none included, and a plain PHP callable or Closure parameter counts as one that takes them on:

`PureRater.setRate` can't take a `Function<int(int) uses none>`: `Rater.setRate`'s `rate` takes on each caller's effects, so a call through `Rater` may pass a function with any effect.
Help: State the effects on `Rater.setRate`'s `rate`, as `Function<int(int) uses none>`, so every implementation may keep it, or take a `Function<int(int)>` here and call it only inside `setRate`.

Over a plain PHP interface the help keeps only "Take a Function<…> here and call it only inside setRate." The PHP# note of a narrowed parameter now says which calls the override must take: "A call through Base may pass item any Order, so OrderBase.put must take every Order too." Plain PHP keeps upstream's note.

Checker

  • crates/codex/src/scanner/ttype.rs: a function type's uses none reads as no effect, so the type is the pure function type.
  • crates/analyzer/src/statement/class_like/method_signature.rs: validate_method_signature_compatibility refuses an override parameter that states effects where the overridden one takes them on, as its own issue, TakesOnEffectsDropped. report_signature_compatibility_issue in class_like/mod.rs words it, and its parent_method parameter is renamed child_method, the method every caller passes.
  • crates/analyzer/src/effects/summary.rs: record_function_values holds each function in a collection to the element type, at any depth, and takes how the place writes its type. array_assignment.rs records a write through an index, and the property write records only = and ??=, so one refusal names the value.
  • crates/analyzer/src/invocation/arguments.rs: verify_argument_type refuses a function value whose effects are all that keep it out, in the lambda's words.
  • crates/analyzer/src/utils/names.rs: display_sharp_parameter_type writes a parameter's pure function type with uses none, and display_function_holder names the function member of a union.
  • crates/analyzer/src/effects/check.rs and crates/semantics/src/internal/checker/sharp.rs: uses none on a method, and a name beside none.
  • The bridge and the engine need nothing: a uses none type lowers to the pure type text Function<int(int)>, which the engine tests.
  • docs/spec.md section 29 states the rules with the checked example pure-function-parameters, docs/decisions/082-pure-function-types.md records them, and docs/architecture/slices/32-hosts.md describes the escape error as it now reads.

Tests

  • a_function_parameter_typed_uses_none_takes_only_a_pure_function_and_may_escape has one row per form: store, return, capture by an escaping lambda, a field typed uses none, an interface method parameter, a pure lambda, a method value with Http, a lambda with Http, and uses none, Http.
  • an_override_takes_on_effects_wherever_the_method_it_overrides_does refuses the reviewer's seven shapes: an interface method with uses none and with uses Http, a virtual class method, an abstract class method, a generic interface, a plain PHP interface with a \Closure parameter, and a sub-interface that narrows its parent's parameter. It also refuses a nullable Function<int(int) uses none>? and a plain PHP int|\Closure parameter's int|Function<int(int) uses none>. An implementation that only calls the function compiles.
  • a_function_in_a_collection_is_held_to_the_collection_s_element_type, a_function_value_refused_only_for_its_effects_is_named_with_them and uses_none_is_refused_on_a_method_and_beside_another_effect cover the rest of the review.
  • Every new row failed before its change.
  • The reviewer's engine probe: mago compile refuses all eight hole files, so each run is no longer reachable, and the engine stops at "app/Holes/PureRater.sharp isn't compiled".
  • a_test_of_a_function_type_that_uses_none_is_the_engine_test_of_the_pure_type_text in the bridge, and function_values.phpt and function_values_preload.phpt, which run a uses none setter and call the stored function.
  • Plain PHP analysis is unchanged: master's and this branch's mago analyze give the same 125 lines on sharp/mago, and the same 4125 lines on the analyzer's plain PHP cases.

Found, not fixed

  • A readonly refusal points at the wrong method. "Declared readonly here." lands on the override that is not readonly, because report_signature_compatibility_issue annotated the method its callers pass, which is the override. Probe:

    public interface Reader { int count() readonly; }
    public class Book : Reader { public int count() => 1; }   // the annotation points here, not at Reader.count
  • After v1: a plain PHP \Closure that a docblock types passes as pure when a method that an extern declares pure returns it. This branch's mago compile accepts the probe below, with extern Typed; and this plain PHP class:

    final class Typed
    {
        /** @return \Closure(int): int */
        public static function make(): \Closure
        {
            return static fn (int $n): int => \fetch($n);
        }
    }
    public class TypedClosure
    {
        private Function<int(int)> rate = (int n) => n;
    
        public void setRate(Function<int(int) uses none> rate) => this.rate = rate;
    
        public void plainTyped() { this.setRate(Typed.make()); }   // compiles, and the kept closure runs fetch
        public void field() { this.rate = Typed.make(); }          // compiles
    }

…eps a pure function

Problem

A method takes on the effects of each function it is given, and a function
parameter whose type states no effects can't be stored, returned or
captured by an escaping lambda. Only a parameter whose type states its
effects may escape, and no type said "pure". So a setter, a registration
method or a builder could not keep a pure function: only a constructor
parameter that declares a property could. `Function<int(int) uses none>`
read `none` as a class, and refused it as no effect.

The refusal for storing such a parameter named `Function<int() uses …>`,
which leaves the reader to guess what goes after `uses`, even where the
field it goes to is pure and takes no effect at all.

Solution

`uses none` is the empty effect list in a function type, so the type is the
pure function type a field's `Function<int(int)>` already is. The codex
reads no effect from it, so a parameter of that type caps its effects at
none: it may be stored, returned and captured, and a function with an
effect passed to it is refused at the call through the same function value
check as any cap. That refusal now says the type is pure: "so a pure
`Function<int(int)>` cannot hold it". The bridge writes the type text the
engine tests, `Function<int(int)>`, as for any pure function type, so the
engine needs nothing new.

`check_uses` reads a function type's `uses` on its own: `none` alone names
no effect, and `none` beside another effect is refused.

The escape refusal names the fix: the type of the place when it states
effects, `Function<int() uses none>` when the place is a pure function
type, and otherwise `Function<int() uses none>`, or its effects in place of
`none`.

php-sharp/
├── docs/
│   ├── spec.md                                   section 29: the rule and the checked example pure-function-parameters
│   └── architecture/slices/14-functions-as-values.md  uses none no longer waits on Jordan
├── sharp/mago/crates/
│   ├── codex/src/scanner/ttype.rs                uses none is no effect
│   ├── analyzer/src/effects/check.rs             check_uses, the escape help, and "a pure" in the function value refusal
│   ├── analyzer/tests/sharp.rs                   one row per form, and the changed messages
│   └── sharp-bridge/tests/lower.rs               uses none lowers to the pure type text
└── Zend/tests/sharp/
    ├── FunctionValues/FunctionValues.sharp       a uses none setter
    ├── function_values.inc                       the setter's row
    ├── function_values.phpt
    └── function_values_preload.phpt
An override takes on effects wherever the method it overrides does, a plain
PHP callable or Closure parameter included, so an implementation can no longer
keep a function its declaration only lends it. A function inside a List or a
Map is held to the element type. A function value refused only for its effects
is refused in the lambda's words, with the parameter's type as it is written.
The escape error says the parameter takes on each caller's effects. uses none on
a method says to delete it, and a name beside none is still checked. Decision 82
records uses none and the override rule.
An override's nullable function parameter and a plain PHP int|Closure
parameter are refused, and an argument whose effects are partly listed names
only the others. The override refusal's two lookups that cannot fail say why.
@heyJordanParker
heyJordanParker merged commit 9454d80 into master Oct 17, 2026
24 checks passed
@heyJordanParker
heyJordanParker deleted the feat/uses-none 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