Repository navigation
feat: uses none states that a function type is pure, so a setter keeps a pure function - #164
Merged
Merged
Conversation
…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.
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.
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 nonestates that a function type is pure. Decision 82 records it, with the override rule the review added.What a PHP# developer can now write
uses noneis the empty effect list, wherever a function type'susesgoes: a parameter, a field, a property, a return type, a type argument, an interface method's parameter.uses noneparameter may be stored, returned and captured, as one whose type states effects may.global.fetch, which has the effectHttp, so a pureFunction<int(int) uses none>cannot hold it."mailerhas the effectMail, so a pureFunction<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: "bothhas the effectMail, so aFunction<int(int) uses Http>cannot hold it."Function<int(int) uses none>ofint|Function<int(int) uses none>cannot hold it."Listor aMapis 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 nonebeside another effect is refused: "nonestates that the function has no effect, souseslists nothing beside it." A misspelled effect beside it, asuses none, Htp, is still reported.uses noneon a method is refused, with or without a body: "A method withoutusesis pure already, so deleteuses none."Function<int(int) uses none>is the pure type itsFunction<int(int)>already is, with no warning.stepcan'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 aFunction<int() uses none>, or state its effects in place ofnone."An override takes on effects wherever the method it overrides does
A call through
Ratertakes 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 noneincluded, and a plain PHPcallableorClosureparameter counts as one that takes them on:Over a plain PHP interface the help keeps only "Take a
Function<…>here and call it only insidesetRate." The PHP# note of a narrowed parameter now says which calls the override must take: "A call throughBasemay passitemanyOrder, soOrderBase.putmust take everyOrdertoo." Plain PHP keeps upstream's note.Checker
crates/codex/src/scanner/ttype.rs: a function type'suses nonereads as no effect, so the type is the pure function type.crates/analyzer/src/statement/class_like/method_signature.rs:validate_method_signature_compatibilityrefuses an override parameter that states effects where the overridden one takes them on, as its own issue,TakesOnEffectsDropped.report_signature_compatibility_issueinclass_like/mod.rswords it, and itsparent_methodparameter is renamedchild_method, the method every caller passes.crates/analyzer/src/effects/summary.rs:record_function_valuesholds each function in a collection to the element type, at any depth, and takes how the place writes its type.array_assignment.rsrecords 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_typerefuses 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_typewrites a parameter's pure function type withuses none, anddisplay_function_holdernames the function member of a union.crates/analyzer/src/effects/check.rsandcrates/semantics/src/internal/checker/sharp.rs:uses noneon a method, and a name besidenone.uses nonetype lowers to the pure type textFunction<int(int)>, which the engine tests.docs/spec.mdsection 29 states the rules with the checked examplepure-function-parameters,docs/decisions/082-pure-function-types.mdrecords them, anddocs/architecture/slices/32-hosts.mddescribes the escape error as it now reads.Tests
a_function_parameter_typed_uses_none_takes_only_a_pure_function_and_may_escapehas one row per form: store, return, capture by an escaping lambda, a field typeduses none, an interface method parameter, a pure lambda, a method value withHttp, a lambda withHttp, anduses none, Http.an_override_takes_on_effects_wherever_the_method_it_overrides_doesrefuses the reviewer's seven shapes: an interface method withuses noneand withuses Http, avirtualclass method, anabstractclass method, a generic interface, a plain PHP interface with a\Closureparameter, and a sub-interface that narrows its parent's parameter. It also refuses a nullableFunction<int(int) uses none>?and a plain PHPint|\Closureparameter'sint|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_themanduses_none_is_refused_on_a_method_and_beside_another_effectcover the rest of the review.mago compilerefuses 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_textin the bridge, andfunction_values.phptandfunction_values_preload.phpt, which run auses nonesetter and call the stored function.mago analyzegive the same 125 lines onsharp/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
readonlyhere." lands on the override that is not readonly, becausereport_signature_compatibility_issueannotated the method its callers pass, which is the override. Probe:After v1: a plain PHP
\Closurethat a docblock types passes as pure when a method that anexterndeclares pure returns it. This branch'smago compileaccepts the probe below, withextern Typed;and this plain PHP class: