Skip to content

feat: section 29, effects - #151

Merged
heyJordanParker merged 25 commits into
masterfrom
feat/effects
Oct 17, 2026
Merged

heyJordanParker merged 25 commits into
masterfrom
feat/effects

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Completes: §29

Lands after #136 (hosts), which is on master. #136 builds how a method takes on the effects of a function it is given, and this PR's collection methods build on it (decision 6 of docs/architecture/slices/29-effects.md).

What a PHP# developer gets

  • The nine effect classes ship: Clock, Random, Console, Process, Files, Http, Cache, Mail and Database, with Duration, ProcessResult and the Net request, response and mail types. The standard library's module binds the seven that need no setting.
  • Console works in every host, a web server's included: it writes through php://output and php://stderr and reads php://stdin.
  • A bare call of a function an effect class wraps names the member to write, as in "write clock.now() on the Clock your constructor takes". Every family the spec names has its rows: apcu_*, pg_*, mysqli_*, every curl_*, putenv, mt_srand, and the file handle functions.
  • Two new members: Files.readLinesLazily(string path) streams a file's lines, and Environment.setVariable(string name, string value) replaces putenv.
  • MailAddress and HttpRequest refuse a line break in a name, a header name or a header value.
  • Creating a PHP built-in class the standard library replaces is refused (wrapped-class), with new or a static factory, as "Create no DateTimeImmutable: write Date.parse(…) or clock.now()". Importing, holding, passing, returning and reading one compiles.
  • A foreign object is created only in a module binding (foreign-outside-module), and is an ordinary type in parameters and return values.
  • A standard exception needs no import: a bare ValueError names PHP's, and a class of the file's namespace or an import wins over it. A bare Error always names Sharp.Error, as Namespaces and imports, and keyword case: sections 23 and 2 #145 made it.
  • An object a declared plain PHP call returns carries the call's effect, and the error names that call and the extern on the returned class that overrides it.
  • Every PHP built-in exception class, and DateTimeImmutable and DateTimeZone, come declared pure. The date members that may read the clock, __construct, createFromFormat and modify, have Clock.
  • A use of a class's statics has the effects of their initial values, which run on that first use.
  • A project's extern of what the standard library declares is refused on the project's line, as "LogicException is already declared pure by the standard library: delete this line."
  • A declaration that widens the uses it overrides is refused at the declaration. A sub-interface may narrow it.
  • The collection methods take on the effects of each function they are given, through feat: every host runs one app per worker and one scope per unit of work #136's mechanism, so list code with pure functions stays pure.
  • A law reasons about a List's map, filter and count.
  • A pure method may change its own object and its arguments; readonly promises it changes nothing, and a law still refuses a change, ending "Laws hold only over code that changes nothing."

Harness and tooling files this PR changes

  • sharp/harness/app/Elided.sharp
  • sharp/bin/build (--with-curl)
  • sharp/bin/builtins (exception and date class declarations, and the date members that read the clock; PHP's engine Error is left out, since a bare Error names Sharp.Error)
  • sharp/mago/scripts/regen-analyzer-issue-codes.php (wrapped-class)
  • New test support file sharp/mago/tests/support/effect_classes.rs: the analyzer, bridge and CLI tests read the effect classes from the library through it.
  • sharp/baseline/linux and sharp/baseline/darwin drop SKIPPED Zend/tests/bug71930.phpt. The engine now builds with curl, so that upstream test, which needs the curl extension, runs and passes. Team-lead accepted this: running it is the point of decision 2.

Spec examples, before and after

  • Before (master 849232f): 16 fail, with §29/function-effects, §29/pure-interface and §8/output-and-exit among them.
  • After, merged with master at bda1b92: 5 fail, matching the baseline (§7/variadic-parameters, §8/standard-library-calls, §9/parameter-member-limits, §18/template-to-string, §19/operators). Every section 29 example passes, and sharp/baseline/spec/§29/ is empty. standard-exceptions, wrapped-class and foreign-parameter are new and pass.

…nism

Problem: section 29's effects work on master only in part. Nine of the ten
effect classes do not exist, an object a declared plain PHP call returns
carries no effect, and three rules are silently accepted. Nothing listed
which rules hold and how the rest get built.

Solution: docs/architecture/slices/29-effects.md lists every rule line of
section 29, its state observed with a release mago and the engine built
from master 849232f, the mechanism that builds it, and the code it
reuses, with team-lead's approved decisions and Jordan's open questions.

docs/architecture/slices/
└── 29-effects.md   new: the design note
…effect

Problem: spec section 29 line 3741 says an object a declared plain PHP
call returns carries that call's effect. The checker read every call on
such an object as undeclared, so with `extern DB uses Database;` a real
Laravel facade's `DB.table("orders").where(…).count()` was refused at
`Builder.count`, and so was every chain from a facade or a model.

Solution: Effects::solve builds one map per run of the effects each plain
PHP class carries because a declared call returns it, closed over chains
such as a `static` return, and reads an unknown call into such a class
as those effects. An extern on the returned class wins, because its calls
are never unknown. A class two declared calls return carries both.

sharp/mago/crates/analyzer/
├── src/effects/mod.rs    carried_effects, carried_through
└── tests/sharp.rs        an_object_a_declared_call_returns_carries_its_effect
Problem: pure code that threw a PHP built-in exception was refused.
`public int first(List<int> values) => … : throw new ValueError("empty");`
under a law got "calls `Error`, which has no `extern` declaration", and
the help offered `extern Error;`, a class the code never names. The
harness worked around it with its own `extern LogicException;`.

Solution: sharp/bin/builtins also writes a pure `extern` for each PHP 8.5
exception class Mago's prelude declares, by name, as it does for each
function: `Exception`, `Error` and every class whose parents reach one of
them. A namespaced class whose short name another has, as FFI\Exception,
is imported under an alias, as the checker's help names one. The refusal
of `new` now names the class `new` names, or its nearest plain PHP parent
when PHP# declares it. The harness's `extern LogicException;` would now be
a duplicate, so it goes. Spec line 3744 says exception classes come
declared.

sharp/
├── bin/builtins                         exceptionClasses, checkerNames
├── composer/library/Sharp/Builtins.sharp  regenerated: 57 exception classes
├── harness/app/Elided.sharp             drops its extern LogicException
├── mago/crates/analyzer/
│   ├── src/effects/summary.rs           instantiate names the class written
│   └── tests/sharp.rs                   creating_a_built_in_exception_is_pure
└── README.md
docs/
├── spec.md                              line 3744
└── architecture/slices/29-effects.md
Problem: spec section 29 line 3819 says that for the environment and the
command-line arguments a superglobal's error names `Environment`. Every
superglobal got "PHP# has no superglobals; take a Request", so
`_ENV["HOME"]` and `_SERVER["argv"]` pointed at a request object that
holds neither.

Solution: `_ENV`, through any index, and `_SERVER`'s "argv" or "argc"
now get "take an Environment", or the aliased import when the file's own
class takes the short name. Every other read keeps "take a Request".

sharp/mago/crates/semantics/
├── src/internal/checker/sharp.rs   reads_the_environment, report_superglobal
└── tests/sharp.rs                  an_environment_superglobal_is_an_error_that_names_environment
docs/decisions/038-environment.md
…lare

Problem: spec section 29 lines 3939 to 3942 show the help for a plain PHP
call with no `extern`: "Declare its effect in a .sharp file, such as
`extern Mailchimp uses Http;`". The checker offered the member,
`extern Mailchimp.addListMember;`, as a pure declaration, even where the
refusing declaration says `uses Http`, so the declaration it offered
would be refused again.

Solution: the help names the class, as the spec does, and offers the
effects the refusing `uses` or function type lists. A law or a pure
declaration keeps "when it has no effect, or name its effects after
`uses`". A function keeps its own name.

sharp/mago/crates/analyzer/
├── src/effects/check.rs   impure takes the allowed effects
└── tests/sharp.rs         a `uses Http, Mail` row; 14 tests name the class
Spec lines 3686 and 3997 promised an editor that shows a method's laws and effects. PHP# ships no editor package or language server, so both lines now say what the checker does: its errors name the effect, and an error about a law names the law.
Problem: spec section 29 line 3875 says a `foreign` object is created in
a module binding and nowhere else, so a class's effects stay visible from
its constructor. The checker accepted `public RedisStore make() => new
RedisStore();` and `new Environment()` anywhere in a method, so code
could reach an effect no constructor shows.

Solution: a `.sharp` file's `new` of a class that is `foreign`, or
extends one, gets `foreign-outside-module` outside a module binding's
expression, a lambda in it included. A test module's bindings are
module bindings. Plain PHP creates one freely, so the `environment`
example's claim moves from its run line to a plain PHP block, which
PR #148 lets restate a claim. Three tests that created an `Environment`
in a method stop doing so: the engine's Deploy fixture and its PHP twin
lose `create()`, and environment.phpt creates the object in PHP.

docs/spec.md                              environment example, line 3875
sharp/mago/
├── scripts/regen-analyzer-issue-codes.php  foreign-outside-module
├── crates/analyzer/src/code.rs           regenerated
├── crates/analyzer/src/expression/instantiation.rs  check_foreign_outside_module
├── crates/analyzer/tests/sharp.rs        a_foreign_object_is_created_only_in_a_module_binding
└── crates/sharp-bridge/tests/lower.rs    typeof(Environment) names the class
Zend/tests/sharp/
├── Deploy.sharp, Deploy.inc              no create()
├── environment.phpt                      PHP creates the Deploy
└── opcodes.phpt                          Deploy has 6 op arrays
The example showed its throws claim through a plain PHP block, which taught a reader that creating an Environment takes plain PHP. It now shows a module binding of Deploy, and its run lines take the Deploy through a constructor, as PR #148 lets a run do. With #148's runner and this branch's checker, the environment example passes and the same 16 examples fail.
…orkings

Problem: messages on .sharp files named the tool behind the checker and
its translation, as "Mago does not translate collections to Lean." and
"This is a bug in Mago; report it to Mago's maintainers." A PHP#
developer should not need to know what runs under the checker.

Solution: no message names Mago as the thing acting; it is "the
checker". A refusal about PHP# code names the construct and says plainly
that a law cannot reason about it, as "A law cannot reason about a loop.",
with no promise of later work. Lean stays where the subject is the
developer's own .lean file or the Lean install, as section 28 has them,
and `vendor/bin/mago compile` stays as the command. sharp_words.rs now
refuses `Mago`, and Lean as the translation, in every message a .sharp
test sees.

sharp/mago/
├── crates/sharp-lean/src/issues.rs      every refusal reason and Mago line
├── crates/sharp-lean/src/library.rs     install errors
├── crates/sharp-lean/src/runner.rs      the processor limit
├── crates/analyzer/src/resolver/{method,property}.rs
├── crates/analyzer/src/statement/class_like/{mod,constant}.rs
├── crates/analyzer/src/statement/try.rs
├── crates/analyzer/src/utils/docblock.rs
├── crates/analyzer/tests/sharp.rs
├── tests/compile.rs
└── tests/support/sharp_words.rs         Mago and Lean-as-translation banned
… need

Decision 2 gives the Http effect class a curl backend, and real PHP apps and their HTTP clients depend on curl, but the engine built without it. sharp/bin/build now passes --with-curl, with brew's curl on macOS, the build image installs libcurl4-openssl-dev, and the macOS CI job and the README install brew's curl. The runtime image already installed libcurl4-openssl-dev and configured --with-curl. Observed: the rebuilt engine loads curl 8.18.0.
Clock, Random, Console, Process, Files, Http, Cache, Mail and Database are
foreign classes in sharp/composer/library/Sharp/, with Duration, ProcessResult,
HttpMethod, HttpRequest, HttpResponse, MailAddress and MailMessage, as
docs/architecture/slices/29-effect-classes.md designs them. The library's
module binds the seven an app needs no setting for. NAMED_EFFECTS goes: an
effect is a foreign class or Events.

- A bare call of a function an effect class wraps names the member to write,
  as in "write `clock.now()` on the `Clock` your constructor takes", and the
  errors for echo, print and die name Console and Process.
- A while condition that holds an `is` is analyzed before its formula, as an
  if condition is, so `while (x is Map<string, int> row)` narrows `row`
  instead of panicking.
- Randomizer::shuffleArray's stub returns a list of the array's values.
- The analyzer, bridge and CLI tests read the effect classes from the
  library through tests/support/effect_classes.rs, in place of hand copies
  of Environment.
- Engine tests run each class, one under preloading, and the module's
  classes reach the test projects through project.inc's LIBRARY_MODULE.
- §8/output-and-exit and §29/pure-interface pass and leave the baseline.
The Lean translation writes a List as Lean's List, a list literal as a Lean
list, and `items.map(x => …)`, `items.filter(x => …)` and `items.count()` as
List.map, List.filter and List.length. A lambda there is a Lean `fun` whose
body cannot throw. Any other List method is refused by name, and a Map, a Set,
a lambda whose body can throw and every other lambda stay refused.
A bare ValueError, OutOfRangeException or any other exception class in PHP's
root namespace names PHP's own in a .sharp file, as Sharp's default names do.
A class of the file's namespace, one it imports or one it declares wins over
it by the same rule. The list is the root exception classes Builtins.sharp
declares, and a test keeps the two equal.

- sharp/bin/builtins imports only the exception classes in a namespace, so
  Builtins.sharp drops its 48 root imports, and an extern of a default name
  names that class.
- The standard library drops every exception import.
- ProcessResult, what Process.run gives back, is a default name too.
- Spec section 23 states the rule with a `throw new ValueError("empty")`
  example.
A .sharp file may import and name PDO, DateTimeImmutable and the other PHP
built-in classes the standard library replaces, to hold, pass, return and
read what a library gives. Creating one, with `new` or a static factory that
returns the class, is the error wrapped-class, which names the members to
write instead, as in "write `Date.parse(…)` or `clock.now()` on the `Clock`
your constructor takes". The standard library itself creates them.

WRAPPED_CLASSES sits beside REPLACEMENTS, and the test that holds every
replacement member to the library holds its members too. Spec section 29
states the rule with an example.
Spec section 29 states what the checker already does: a method may take or
return a foreign object such as Clock, a method that calls one it takes has
that effect, and a caller that uses a returned one has it at that call.
Creating one outside a module binding and storing one in a static stay
refused, and their messages no longer say a foreign object reaches code only
through constructors.

DateTimeImmutable and DateTimeZone come declared pure. A .sharp file never
creates one, so holding and reading one has no effect, and Date.format no
longer carries the Clock and Environment of every call that returns one.
Spec section 29 states what the checker already does: pure limits only what
a method reaches outside the program, so a pure method may change its own
object and its arguments, and `readonly` after the parameters promises it
changes nothing. A law still refuses a change, naming the method, because
Lean sees an object as its values and no object two names share. A test
holds each row, and the design note records Jordan's answers.
Import.rows created an SplFileObject, which wrapped-class now refuses in a
.sharp file. It takes the Files a caller gives it and reads the lines with
files.readLines, so its body still runs only when a loop reads it, and a
missing file still throws only then.
- A static call of a wrapped class that is no factory, as
  PDO.getAvailableDrivers(), compiles.
- Creating a wrapped class is refused even in a project without the
  standard library, which has no member to name.
- A catch of a class that does not exist names the checker in its note.
- carried_effects and methods_of read an extern's first declaration and a
  class's methods in one expression each.
The engine builds with curl (decision 2), so bug71930.phpt, which needs the
curl extension, runs and passes instead of skipping. Team-lead accepted the
change, so both baselines drop its SKIPPED line.
Both sides keep their behaviour, with one mechanism where both built one:

- Default names: master generates every class-like directly in Sharp into
  sharp_classes.rs, so the effect classes and ProcessResult come from it, and
  this branch's standard exceptions join that list. NAMED_EFFECTS, which #145
  moved into mago_names, goes, as decision 1 deleted it: each effect is a
  class now.
- `Error` always names Sharp.Error (spec section 31), so sharp/bin/builtins
  no longer declares PHP's engine Error, which PHP# code never names.
- The bridge tests' library takes the effect classes on master's two-pass
  name resolution. Two master tests no longer pass ENVIRONMENT, which every
  analyzer test project holds through EFFECT_CLASSES.
- The yield test loads its Import fixture folder and the effect classes.
- Messages keep this branch's wording: echo names Console, a refused law
  says "A law cannot reason about…", an undeclared call names the class.
… given

Spec section 29, line 4123: the standard library's collection methods take
on the effects of their functions the way a PHP# method does. Decision 6
builds them on #136's mechanism: the scanner flags each \Closure parameter
of the prelude's ListMethods, MapMethods, SetMethods and IterableMethods
TAKES_ON_EFFECTS, so passed_functions takes on what each call passes, as it
does for a PHP# method's function parameter.

prelude_stub_call keeps only its change of the receiver, and callbacks runs
only for plain PHP, which may run any function it is given. The second path
through the stubs is gone.
- Console writes through php://output, php://stderr and php://stdin, so it
  works under every web host. effect_console_web.phpt serves a page that
  calls it through the built-in web server.
- A use of a class's statics has the effects of their initial values, which
  run on that first use.
- DateTimeImmutable's __construct, createFromFormat and modify read the
  clock: sharp/bin/builtins declares them with `uses Clock`.
- A project's extern of what the standard library declares is refused on
  the project's line, naming the standard library.
- Every function family a standard class replaces names its member: apcu_*,
  pg_*, mysqli_*, every curl_*, putenv, mt_srand and srand, and the file
  handle functions, which Files.readLinesLazily replaces.
  Environment.setVariable replaces putenv.
- MailAddress and HttpRequest refuse a line break in a name, a header name
  or a header value.
- wrapped-class reads "Create no `X`", a law refused for a change ends
  "Laws hold only over code that changes nothing.", GLOBALS gets its own
  message, a declaration that widens the `uses` it overrides is refused, and
  a carried effect names the declared call that carries it and the extern
  that overrides it.
- Three stale comments are fixed.
…lue names its declared call

The declaration check runs where the method's metadata is already looked
up, and it shares one lookup of the declaration that holds an override
with the implementation check, and one wording of what that declaration
allows.
@heyJordanParker
heyJordanParker merged commit c78fb5e into master Oct 17, 2026
22 checks passed
heyJordanParker added a commit that referenced this pull request Oct 17, 2026
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.
heyJordanParker added a commit that referenced this pull request Oct 17, 2026
`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.
heyJordanParker added a commit that referenced this pull request Oct 17, 2026
…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.
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 added a commit that referenced this pull request Oct 17, 2026
The spec said shell_exec() stays, while the checker refuses a bare
shell_exec and names process.run, as #151 maps it. The spec now says
so in section 18 and in section 29's list of what Process replaces, and
the replacement test pins the shell_exec row.
@heyJordanParker
heyJordanParker deleted the feat/effects 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