Repository navigation
feat: section 8, functions - #157
Merged
Merged
Conversation
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.
… gaps it showed
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.
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.
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 theglobal.refusal.What a PHP# developer gets
Math.maxandMath.mintakeint, with one value before the..., soMath.max(0, ...prices)gives 0 for an empty list.List<int>hasmax()andmin(), which throwOutOfRangeExceptionon an empty list. A list of another element type reads "maxneeds elements of typeint, and this is aList<string>."max(a, b)andmin(a, b)nameMath.max(…)andMath.min(…),str_getcsv(line)namesline.parseCsv(), and each collection function names the method of the kind it is given:count(items)namesitems.count(),in_array(item, items)on aListnamesitems.contains(item), and on aMapnamesitems.values().contains(item). A kind with no such method names each kind's method onx, 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, ascount(names, 1)namesx.count().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.timeandglobal.file_put_contentsnameconsole.write,process.exit,clock.now()andfiles.write. A function PHP# syntax replaces, asglobal.strval(1), a function with no member, asglobal.levenshtein(a, b), and the standard library itself keepglobal..global.trim, as innames.map(global.trim), namesx.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 bareexit's.global.diegets the baredie's: "PHP# has nodie: write the message withconsole.writeError(text), thenprocess.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.containson a value that may be aListor aMap.Math.max(0, ...[])gives 0.Environmentreplaces every environment function. New member:Map<string, string> variables()reads every variable on each call.getenvnamesenvironment.variable(…)orenvironment.variables(),apache_getenvandapache_setenvnamevariableandsetVariable, andgetoptnamesenvironment.arguments. A read of$argvnamesenvironment.arguments, and$argcnamesenvironment.arguments.count().Listshares with aMapor aSetruns on a value that may be either, aslet held = listed ? names : stock; held.remove("tea");. A value that may also be an object is refused: "List<int>|Boxmay be a collection or an object, andcountruns a different method on each."List<int>and aMap<string, int>."Anyis 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.incgainssay(string $text), so the observer and override fixtures trace a static's hook, which has no object to hold aConsole.sharp/bin/specorsharp/harness/.Spec examples, before and after
§7/variadic-parameters,§8/standard-library-calls,§9/parameter-member-limits,§18/template-to-stringand§19/operators.§7/variadic-parametersand§8/standard-library-calls, and feat: section 29, effects #151 deleted§8/output-and-exit, so every section 8 example passes.[10, 25].map(size => size * 2), and section 7 line 459 names a call that never returns in place ofglobal.exit(…).process.exit(…)ends a block once the singles PR makes it anevermethod.