Repository navigation
Functions as values: every rule of spec section 14 - #155
Merged
Merged
Conversation
…mechanism Every example of spec section 14 passes on master, but seven gaps sit behind them: a function value is called only through a local, a parameter or a field; a static field is not called like a method; a member named like an inherited one compiles; a lambda parameter nothing types is Any?; a collection's or an extension's method is no function value; a loop variable that changes can't be captured; and two PHP forms get "not supported yet". The note records each rule line's state on master c4adf74, the mechanism that closes each gap, the code it reuses, team-lead's nine questions with their proposals, and the files the Slice changes. docs/architecture/slices/ └── 14-functions-as-values.md <- added
…spec says - Any expression that gives a function is called: a list element, a call's result, a conditional, a parenthesized field and a lambda. - A static field or property holding a function is called through its class, a generic class's with the type arguments the call writes. - Each pass of a `for ... of` and a `for` loop gives a lambda its own captured variable; the "not supported yet" refusal and its help are gone. - A lambda parameter nothing types is refused, and a field's initial value gives its lambda the declared type. - A lambda fits a function type only with every parameter a call passes, and a lambda for a `void` type gives no value. - A member named like a visible inherited member of the other kind is refused, from PHP# and plain PHP ancestors; the standard library's InvalidFields renames its private `message` method `describe`. - `(...)`, a `void` parameter, `import Closure;` and `&` are refused in PHP# words, and a wrong call of a function value names it as the code writes it, never PHP's `Closure`. - The spec gains section 14.1's `function-calls` example and the wording of the captured-collection and needless-`?` rules.
…alues # Conflicts: # sharp/mago/crates/semantics/tests/sharp.rs
…136 keeps a function parameter from escaping
…on named without parentheses are function values Spec section 14.3: `allowed.contains`, `prices.has`, `name.trim`, an imported extension method and `global.strtoupper` are function values, typed by their method's or function's signature without the receiver. A collection's value holds the collection as it was when the value was made (Q5), so a method that changes the collection is refused with the lambda that changes it (Q10). A function's value has the effects its `extern` declares (Q6), and an extension method's value has its body's. The bridge writes each as a closure over its receiver, read once, or as PHP's first-class callable of the function. The engine and the standard library do not change.
…llow the new rules A function read without a call now compiles, so the compile test expects `global.strlen` and `Pdf.render` to compile. `&` on a parameter now has section 14's words, so the spec checker's self-test shows a "not supported yet" refusal with `new Cart`. New rows cover a write of an extension method, a nullable collection's value, an `Iterable`'s value, a generic extension's and a generic plain PHP function's value, an extension whose block bound the receiver breaks, a generic class named without its type arguments, a null-safe collection value and an unpacked call of a plain PHP function that takes none. Lines no test can reach are `unreachable!` with their reason, and the inherited member check loses a visibility test the codebase's inherited members already make.
…d an extension value drops its receiver with no branch The coverage job's nightly instrumentation counts each branch of a line. A test now reads `numbers.missing`, which no collection method has, so the read keeps master's refusal. An extension method's value drops its first parameter, the receiver every extension method declares, without an `if`, and its block's type parameters are inferred from that parameter's type the same way.
- A captured `for` counter inside a loop runs its unset before the `for`, and the header holds the assignment alone, so the engine no longer crashes. - A block lambda for a `void` type returns no value, and a `void` call's result is no value to use. One message covers both lambda forms. - A lambda in a `List` or `Map` literal takes the collection's element type. - A plain PHP function that changes the variable it is given, or reads the caller's variables, is no function value. A direct call that passes a PHP# variable by reference is refused, naming the method that does the work. - A bare `Closure` type, a call of no function, a nullable function and an unparenthesized immediate call are refused in PHP#'s words. - A method value runs as code as a field's initial value. - The too-few-parameters refusal names the function type and the function in a field, a local, an argument, a return and a property write. - A method value's closure takes the method's parameters by name with their PHP types, so plain PHP callbacks see them, and a function value passes to an undocumented `callable` or `\Closure` parameter or property. - An untyped `\Closure` return and a `callable` return are refused with why.
The while-narrowing test reads its `Any?` from a plain PHP method instead of `global.array_shift`, which PHP# now refuses since it changes its argument.
A method value's parameter types reuse the union a declared type lowers to, instead of a copy of it. A namespace function call is PHP#'s alone, so its by-reference check needs no dialect test. A `void` lambda's bare `return` and its `-n` body are rows of the void test.
heyJordanParker
added a commit
that referenced
this pull request
Oct 17, 2026
Resolution: - `analyze_namespace_constant` keeps #155's function value, and refuses first a read of a function the standard library replaces, as `global.trim`, naming its member. - `analyze_namespace_function_call` runs #155's by-reference refusal only when no `global.` refusal names the member, so `global.array_push(numbers, 4)` and `global.shuffle(numbers)` are refused once. #155's test pins that. - `collection_call` keeps #155's `inlined_collection_method`, with the `is_array` test of a value that may be a `List`, a `Map` or a `Set`, the `List`'s element type, and `max` and `min` beside `last`. - `exit_status_refusal` stays deleted, as Queue 6 left it. - Section 29's line keeps `global.usort` and `global.call_user_func`, which compile. - `a_bare_call_of_a_plain_php_function_names_the_call_to_write` compares only the refusal, since #155's check of `array_any`'s two-parameter callback also reports on the refused call. `global.strlen` and `global.strval` read as values are #155's function values.
heyJordanParker
added a commit
that referenced
this pull request
Oct 17, 2026
Resolved by hand: - parameter.rs: #155's `int &value` reference reading, inside this branch's default-after-accessors order and its behaviors field. - semantics sharp.rs: #155 deleted the loop-capture refusal, and this branch's parameter-default read check stays. - analyzer invocation/analyzer.rs: master's takes_on_effects doc beside this branch's takes_php_sharp_types. - analyzer property.rs: master's expanded declared type, with this branch's block-context arguments to check_sharp_literal_kind. - tests: #151 removed the ENVIRONMENT copies, and the Map crossing rows take #155's "function value `counter`" wording. Follow-through so #151's library compiles under this branch: - Sharp/Net HttpRequest, MailAddress and MailMessage write a constructor property's default after its accessors. - Files.sharp and Process.sharp drop the `@` on 13 built-in calls, each of which declares `false` or `bool`, so the bridge silences it.
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.
Every rule line of spec section 14, Functions as values, now compiles, refuses or runs as the spec says, in the checker, the bridge and the engine, with no "not supported yet" left in it.
Completes: §14
Problem
On master, seven gaps sat behind section 14's passing examples:
steps[0](1),this.make()(1)and(flag ? a : b)(1)were "This expression is not supported yet in PHP#."Rates.round(5)was "MethodRates.rounddoes not exist."this.discount(5)ran the parent's method.Any?, as inconst identity = x => x;.numbers.filter(allowed.contains)was "Attempting to access a property on a non-object type (List<int>)."Item.doubled(...),Function<int(void)>,import Closure;and(&value) => valuegot "not supported yet" instead of section 14's words.What changes
function-callsexample.InvalidFieldsrenames its privatemessagemethoddescribe.missing-parameter-type. A field's initial value now gives its lambda the field's type.void: a lambda with fewer parameters than a call passes, and a value for avoidfunction type, are refused, as C#, Kotlin, Swift and Java do.List,Map,SetorIterable, an extension method and a plain PHP function are function values, asallowed.contains,name.trimandglobal.strtoupper. A collection's value holds the collection as it was when the value was made, aMapincluded. A function's value has the effects itsexterndeclares, and an extension method's value has its body's.seen.addchanges the collection, and a function value holds a copy of it: write a lambda that calls it, as invalue => seen.add(value)."for … ofand infor (let i = …), and the "not supported yet" refusal and its help are deleted.(...):Item.doubled.", avoidparameter,import Closure;and&. The two messages that named PHP'sClosurenow name the function value as written.method-value-kindsandchanging-method-valuesexamples, and Q7's and Q8's wording.What the review changed
forcounter inside a loop no longer crashes the engine. Itsunsetruns as a statement before thefor, and the header holds the assignment alone.NestedCountersgives[0,2,10,12,30,32].voidtype returns no value, and avoidcall's result is no value to use: "This lambda gives a value, andFunction<void(int)>returns nothing: remove the value, or give the function type a return type." and "this.add(1)returns nothing, so it has no value to use: call it as a statement of its own."ListorMapliteral takes the collection's element type, at any depth.global.array_pushchanges the variable it is given, and PHP# passes every argument by value: writenumbers.add(…)." The standard library keeps its by-reference calls, such asRegex'spreg_match, since it writes PHP#'s own results. The spec gains thefunction-fitandby-reference-functionsexamples, and section 29 passesglobal.array_filterinstead ofglobal.usort.Closuretype, a call of no function, a nullable function (?.(…)), an unparenthesized immediate call and a typedint &value.Item.doubled.Function<int(int, int)>takes 2 parameters, andReport.onetakes 1."string $tail,int $valueandstring $key. A function value passes to an undocumentedcallableor\Closureparameter and into a?\Closurestatic property. Acallablereturn and an untyped\Closurereturn assigned to a function type are refused with why. The engine test reads them through the new harness classZend/tests/sharp/harness/Callbacks.inc.Q10, open with team-lead
Q5's copy means
numbers.forEach(seen.add)would add to a copy and leaveseenempty. This pull request refuses it, as Swift refuses the value of amutatingmethod. The other option is to allow it, as C# allows a delegate of a struct's method, which changes the copy. A refusal can be lifted later without breaking any code that compiles.Verification
sharp/bin/teston macOS, after merging master with feat: section 29, effects #151: 315 pass, none fail.sharp/bin/test --opcache --repeat --passesonfunction_values.phpt,function_values_preload.phptandfunction_types.phpt: 3 of 3 pass in each of the 22 modes, andfunction_values2 of 2 in each of the 4 modes underUSE_ZEND_ALLOC=0 MallocScribble=1.cargo test -p mago-analyzer -p mago-semantics -p mago-sharp-bridge: every test passes, 986 in the analyzer'ssharp, 2,578 in itsmod, 418 in semantics'sharpand 408 in the bridge'slower.-p mago-syntax -p mago-codex -p mago-orchestratorpass too.cargo test -p mago --test sharp --test compile: 81 and 71 pass.cargo llvm-covof the three packages, withdiff-coveragainst master: every added line of theirsrcruns, but for 10 lines that only callunreachable!, which thepatch-coveragejob leaves out.cargo fmt --check, andcargo clippy --all-targets --all-features -- -D warningson every changed crate: clean.sharp/bin/spec --self-test: 24 of 24 examples give the problems they expect.sharp/bin/spec: 212 examples checked, 1 ignored, 5 fail, each onsharp/baseline/spec.BODY="Completes: §14" sharp/bin/spec --completes: "§14: all 13 checked examples pass."NestedCounters [0,2,10,12,30,32],ForInsideWhile [0,1,0,1],ForInsideForOf [10,11,20,21],NestedPlain [0,1,10,11].build/gen_stub.php,run-tests.phpand the SDK's sources: this branch'smagogives the same issues as another branch's built from master, one line in another order.