Skip to content

Functions as values: every rule of spec section 14 - #155

Merged
heyJordanParker merged 10 commits into
masterfrom
feat/functions-as-values
Oct 17, 2026
Merged

heyJordanParker merged 10 commits into
masterfrom
feat/functions-as-values

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

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:

  • A function value was called only through a local, a parameter or a field. steps[0](1), this.make()(1) and (flag ? a : b)(1) were "This expression is not supported yet in PHP#."
  • A static field holding a function was not called like a method. Rates.round(5) was "Method Rates.round does not exist."
  • A field named like an inherited method compiled, and this.discount(5) ran the parent's method.
  • A lambda parameter nothing typed was silently Any?, as in const identity = x => x;.
  • A method of a collection, of a built-in type or of an extension was no function value. numbers.filter(allowed.contains) was "Attempting to access a property on a non-object type (List<int>)."
  • A lambda could not capture a loop variable that changes, and the help told the user to work around PHP's closures.
  • Item.doubled(...), Function<int(void)>, import Closure; and (&value) => value got "not supported yet" instead of section 14's words.

What changes

  • Any expression that gives a function is called, as in C#, TypeScript, Kotlin and Swift. Section 14.1 gains the line and the function-calls example.
  • A static field or property holding a function is called through its class, a generic class's included, which reads the statics of its type arguments.
  • A class can't declare a member named like an inherited member it can see, a plain PHP ancestor's included. The standard library's InvalidFields renames its private message method describe.
  • A lambda parameter nothing types is missing-parameter-type. A field's initial value now gives its lambda the field's type.
  • A lambda fits a function type only with all its parameters and its void: a lambda with fewer parameters than a call passes, and a value for a void function type, are refused, as C#, Kotlin, Swift and Java do.
  • A method of a List, Map, Set or Iterable, an extension method and a plain PHP function are function values, as allowed.contains, name.trim and global.strtoupper. A collection's value holds the collection as it was when the value was made, a Map included. A function's value has the effects its extern declares, and an extension method's value has its body's.
  • A method that changes a collection is no function value (Q10, below): "seen.add changes the collection, and a function value holds a copy of it: write a lambda that calls it, as in value => seen.add(value)."
  • Each loop pass has its own variable, in for … of and in for (let i = …), and the "not supported yet" refusal and its help are deleted.
  • PHP forms get section 14's words: "PHP# writes a method value without (...): Item.doubled.", a void parameter, import Closure; and &. The two messages that named PHP's Closure now name the function value as written.
  • The spec gains the method value kinds with the method-value-kinds and changing-method-values examples, and Q7's and Q8's wording.

What the review changed

  • A captured for counter inside a loop no longer crashes the engine. Its unset runs as a statement before the for, and the header holds the assignment alone. NestedCounters gives [0,2,10,12,30,32].
  • A block lambda for a void type returns no value, and a void call's result is no value to use: "This lambda gives a value, and Function<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."
  • A lambda in a List or Map literal takes the collection's element type, at any depth.
  • 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 to a by-reference parameter is refused, naming the method that does the work: "global.array_push changes the variable it is given, and PHP# passes every argument by value: write numbers.add(…)." The standard library keeps its by-reference calls, such as Regex's preg_match, since it writes PHP#'s own results. The spec gains the function-fit and by-reference-functions examples, and section 29 passes global.array_filter instead of global.usort.
  • PHP#'s words for a bare Closure type, a call of no function, a nullable function (?.(…)), an unparenthesized immediate call and a typed int &value.
  • A method value runs as code as a field's initial value, as Item.doubled.
  • The too-few-parameters refusal names both sides in a field, a local, an argument, a return and a property write: "Function<int(int, int)> takes 2 parameters, and Report.one takes 1."
  • Plain PHP callbacks: a method value's closure takes the method's parameters by name with their PHP types, so reflection finds string $tail, int $value and string $key. A function value passes to an undocumented callable or \Closure parameter and into a ?\Closure static property. A callable return and an untyped \Closure return assigned to a function type are refused with why. The engine test reads them through the new harness class Zend/tests/sharp/harness/Callbacks.inc.

Q10, open with team-lead

Q5's copy means numbers.forEach(seen.add) would add to a copy and leave seen empty. This pull request refuses it, as Swift refuses the value of a mutating method. 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/test on macOS, after merging master with feat: section 29, effects #151: 315 pass, none fail.
  • sharp/bin/test --opcache --repeat --passes on function_values.phpt, function_values_preload.phpt and function_types.phpt: 3 of 3 pass in each of the 22 modes, and function_values 2 of 2 in each of the 4 modes under USE_ZEND_ALLOC=0 MallocScribble=1.
  • cargo test -p mago-analyzer -p mago-semantics -p mago-sharp-bridge: every test passes, 986 in the analyzer's sharp, 2,578 in its mod, 418 in semantics' sharp and 408 in the bridge's lower. -p mago-syntax -p mago-codex -p mago-orchestrator pass too. cargo test -p mago --test sharp --test compile: 81 and 71 pass.
  • cargo llvm-cov of the three packages, with diff-cover against master: every added line of their src runs, but for 10 lines that only call unreachable!, which the patch-coverage job leaves out.
  • cargo fmt --check, and cargo clippy --all-targets --all-features -- -D warnings on 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 on sharp/baseline/spec. BODY="Completes: §14" sharp/bin/spec --completes: "§14: all 13 checked examples pass."
  • Every probe of the review compiles or is refused as above, and the runtime probes run: NestedCounters [0,2,10,12,30,32], ForInsideWhile [0,1,0,1], ForInsideForOf [10,11,20,21], NestedPlain [0,1,10,11].
  • The plain PHP corpus, build/gen_stub.php, run-tests.php and the SDK's sources: this branch's mago gives the same issues as another branch's built from master, one line in another order.

…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
…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
heyJordanParker merged commit 2e8f86f into master Oct 17, 2026
22 checks passed
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
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.
@heyJordanParker
heyJordanParker deleted the feat/functions-as-values 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