Skip to content

feat: section 8, functions - #157

Merged
heyJordanParker merged 11 commits into
masterfrom
feat/output-calls
Oct 17, 2026
Merged

heyJordanParker merged 11 commits into
masterfrom
feat/output-calls

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Completes: §8

Lands after #151 (effects) and #155 (functions as values), which are on master. The design is docs/architecture/slices/08-functions.md, approved by team-lead in Queue 1, with Queues 2, 3, 4, 6 and 7 as its decisions 8, 9 and 10. Decision 80 records the global. refusal.

What a PHP# developer gets

  • Math.max and Math.min take int, with one value before the ..., so Math.max(0, ...prices) gives 0 for an empty list.
  • A List<int> has max() and min(), which throw OutOfRangeException on an empty list. A list of another element type reads "max needs elements of type int, and this is a List<string>."
  • A bare call of a function the standard library replaces names what to write: max(a, b) and min(a, b) name Math.max(…) and Math.min(…), str_getcsv(line) names line.parseCsv(), and each collection function names the method of the kind it is given: count(items) names items.count(), in_array(item, items) on a List names items.contains(item), and on a Map names items.values().contains(item). A kind with no such method names each kind's method on x, as `x.indexOf(…)` on a `List`. An argument that is no name is written …, and a call with an argument the method drops writes none, as count(names, 1) names x.count().
  • A global. call of a function the standard library replaces is refused with its bare call's message, so PHP# code never reaches around the library. That covers feat: section 29, effects #151's effect rows: global.printf, global.exit, global.time and global.file_put_contents name console.write, process.exit, clock.now() and files.write. A function PHP# syntax replaces, as global.strval(1), a function with no member, as global.levenshtein(a, b), and the standard library itself keep global..
  • The value global.trim, as in names.map(global.trim), names x.trim() and says to pass a lambda that makes the call. Any other plain PHP function read without parentheses is Functions as values: every rule of spec section 14 #155's function value.
  • global.exit("failed") gets one refusal, the bare exit's. global.die gets the bare die's: "PHP# has no die: write the message with console.writeError(text), then process.exit(1)."
  • @global.trim(name) reports both refusals in one run.
  • process.exit(255) throws "ValueError: Process.exit: the code 255 is not from 0 to 254", as does a negative code.
  • A method only some kinds of a collection value have is one error, as contains on a value that may be a List or a Map.
  • Math.max(0, ...[]) gives 0.
  • Environment replaces every environment function. New member: Map<string, string> variables() reads every variable on each call. getenv names environment.variable(…) or environment.variables(), apache_getenv and apache_setenv name variable and setVariable, and getopt names environment.arguments. A read of $argv names environment.arguments, and $argc names environment.arguments.count().
  • Every method a List shares with a Map or a Set runs on a value that may be either, as let held = listed ? names : stock; held.remove("tea");. A value that may also be an object is refused: "List<int>|Box may be a collection or an object, and count runs a different method on each."
  • A declared union of two collections is refused in PHP# words: "A type can't be both a List<int> and a Map<string, int>."
  • A method call on an unchecked Any is refused once, with nothing more on the return or the operator that uses it.

Harness and tooling files this PR changes

  • Zend/tests/sharp/harness/Witness.inc gains say(string $text), so the observer and override fixtures trace a static's hook, which has no object to hold a Console.
  • No change to sharp/bin/spec or sharp/harness/.

Spec examples, before and after

  • Master's baseline holds 5 entries: §7/variadic-parameters, §8/standard-library-calls, §9/parameter-member-limits, §18/template-to-string and §19/operators.
  • After, merged with master at 2e8f86f: "Checked 212 examples, ignored 1, and 3 fail", matching the baseline. This PR deletes §7/variadic-parameters and §8/standard-library-calls, and feat: section 29, effects #151 deleted §8/output-and-exit, so every section 8 example passes.
  • Section 6 line 262 initializes its static with [10, 25].map(size => size * 2), and section 7 line 459 names a call that never returns in place of global.exit(…). process.exit(…) ends a block once the singles PR makes it a never method.

Section 8's two examples fail on master: standard-library-calls because
Math.max does not exist, and output-and-exit because Console and Process
do not exist yet (#151). Probing every rule line found bare calls that
name global.… where a library member exists, and global. calls that
reach the functions the library replaces.

The note records each rule line's state as observed with a release mago
and the engine built from master, the mechanism of each fix, what waits
on #151, and the answers team-lead gave in Queue 1.

docs/architecture/slices/
└── 08-functions.md    new: the design of section 8
…mber

Spec section 8 says a bare call of a PHP built-in names the standard library method to write, and output goes through the library. This builds every part that does not need #151:

- Sharp.Math.Math gains max and min on int, in PHP's list form, so Math.max(0, ...[]) gives 0.
- REPLACEMENTS gains the max, min and str_getcsv rows, and Replacement::CollectionMethod with a row for each collection function whose method section 12 ships: a bare count(items) reads "write `items.count()`", and in_array names `items.contains(item)` on a `List`.
- A call through global. of a function a Members or CollectionMethod row replaces is refused with its bare call's message, outside the standard library. Syntax rows keep their global. form.
- A list pattern's own count call stays unreported.
- Every fixture, PHP twin and test source that called a replaced function through global. now calls the member. Spec section 12 line 1119 and section 29 line 4064 stop naming global.count and global.array_map.
- library_math.phpt and the new library_math_preload.phpt run the max and min rows, and the two spec examples that needed Math.max leave sharp/baseline/spec.
…unt did

Refusing global. on the functions the library replaces took away what code could do only through them. This gives each one back:

- count on a value that may be a List or a Map compiles and runs. A List's count is PHP's \count, which counts a Map and a Set too, so the "not supported by the engine yet" refusal no longer covers it.
- count on an empty value with no collection type, an empty literal or an emptied plain PHP array property, is typed int.
- A method call on an unchecked Any reports mixed-method-access once, and its result is refused, so nothing that uses it reports again.
- Sharp\Internal\ListMethods gains max() and min() with @where T is int. The bridge lowers them as last() is, so an empty list throws OutOfRangeException("The list is empty."). A list of another element type reads "`max` needs elements of type `int`, and this is a `List<string>`."
- REPLACEMENTS gains sizeof and key_exists.
- Tally.sharp writes Pager.sizes().max(), and collection_methods runs max, min and count on a List or a Map.
… may be either

A value that may be a List or another collection, as `let held = listed ? names : stock;`, was refused for every List method the bridge lowers to PHP: "Calling `remove` on `List<string>|Map<string, int>` is not supported by the engine yet." That refusal is gone:

- count is PHP's \count, which counts a Map and a Set too.
- remove, contains and clear test the value, `(\is_array($held) ? <the List's PHP> : $held->remove("tea"))`, so a List runs its PHP and a Map or a Set its own method.
- A List search compares only the List's elements.
- A value that may also be an object keeps a refusal, as invalid-method-access in PHP# words: "`List<int>|Box` may be a collection or an object, and `count` runs a different method on each."

collection_methods runs remove, contains and clear on a List-or-Map and a List-or-Set local, holding each kind. The declared `List<int>|Map<string, int>` refusal stays, pinned by its semantics tests.
A declared `List<int>|Map<string, int>` read "`List<int>` and `Map<string, int>` both run as a PHP array, so a union can't hold both.", which named PHP and how each type runs. The refusal stays, as C#, Java, Kotlin and Swift have no such union either, and now reads "A type can't be both a `List<int>` and a `Map<string, int>`.", with the help "Take the `List<int>` and the `Map<string, int>` as two parameters, or as an enum with one case that holds each." Spec section 24 states the rule the same way.
`report_bare_function_call` keeps this branch's `replacement_of` and `replacement_refusal`, and `replacement_of` takes
#151's family rows, as `curl_*`, so a bare call and a `global.` call find the same row. The bare-call test keeps both
doc comments, and #151's effect-class test stays beside the `global.` test.
…member, and Environment replaces every environment function

#151's REPLACEMENTS rows join the global. refusal unchanged, so `global.printf`, `global.exit`, `global.time` and
`global.file_put_contents` name `console.write`, `process.exit`, `clock.now()` and `files.write`, as their bare calls
do. The global.exit status check is gone: outside the standard library every global.exit gets the bare exit's
message, and Process.exit types the status inside it. global.die keeps its own refusal.

Environment gains `Map<string, string> variables()` for getenv() with no name. getenv, apache_getenv, apache_setenv and
getopt get rows, and a row names a property with `$`, as `$arguments`. A read of `$argv` or `$argc` names
`environment.arguments`.

Section 6 line 262 and section 7 line 459 name no global call, section 29 lists variables(), and every fixture and test
that called a replaced function through global. moves to the member, to a plain PHP never method, or to
Lib\Witness::say for a static hook's trace.
….sharp file writes one

The one caller of `analyze_namespace_function_call` matches `Name.member(…)` only in a `.sharp` file, so its two
`is_sharp()` tests were never false and left a line no test could run.
- A replaced function read through `global` as a value, as `names.map(global.trim)`, names its member, as its call
  does, and says to pass a lambda that makes the call.
- A collection function's refusal runs once the call's arguments are analyzed, and names the method of the kind it is
  given: `in_array(item, stock)` on a `Map` names `stock.values().contains(item)`. A kind with no such method names
  each kind's method on `x`, never on the user's collection.
- An argument that is no name is written `…`, and a call with an argument the method drops writes none of them.
- A method one kind of a collection value lacks is one error, with no "Could not infer" after it.
- `Process.exit` throws `ValueError` for a code outside 0 to 254.
- `@global.trim(name)` reports both refusals: the analyzer alone refuses `@`, since only it knows a file's package.
- `Math.max(0, ...[])` gives 0: an empty list adds nothing to a spread.
- `apache_getenv(name);` gets no "pure function" note, since its extern declares `uses Environment`.
- `global.die` is refused as every `global.` call is, and both `die` notes say the same.
- Decision 80 records the `global.` refusal, decisions 34, 43, 63, 76 and 77 are superseded by it in part, and spec
  section 8 states it.
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
heyJordanParker merged commit a26090f into master Oct 17, 2026
24 checks passed
heyJordanParker added a commit that referenced this pull request Oct 17, 2026
Resolved by hand:
- Process.sharp: #157's 0 to 254 check and comment, on this branch's
  never return type.
- unary.rs, semantics sharp.rs and its tests, mago tests/sharp.rs:
  #157 edited the silence-outside-library rule and its tests, which this
  branch deletes. `@` before a value stays refused in every file, and
  the extern-method test already covers the native body refusal.
- analyzer tests: #157's array_sum call in the Map crossing test, with
  this branch's Pure row and Status crossing. #157's test of @ beside
  the call it silences goes with the rule.
- Accessors/Product, Collections/Cart, PhpForms and Signatures twins:
  #157 rewrote their .sharp sources, so its twins hold.
- spec: #157's global. bullets with this branch's Int.MAX constants
  bullet, and the backed-enum Map rule with #157's count wording.

Follow-through:
- global.hex2bin, global.time and global.count are refused outside the
  library now, so the @ help, the built-in-failures example and three
  tests use global.iconv, global.levenshtein and global.array_sum.
- 08-functions.md says @ before a value is refused.

Fix the merge showed: a built-in that takes a function, as
call_user_func and array_reduce, runs that function's code inside the
call, so silencing it hid the callback's own warnings. Such a built-in
keeps its warnings now, and the Cashier twin and its opcodes are back
to master's.
@heyJordanParker
heyJordanParker deleted the feat/output-calls 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