Skip to content

feat: a .sharp file declares one type, named like the file, and a Position.current() default gives the caller's position - #152

Merged
heyJordanParker merged 11 commits into
masterfrom
feat/files
Oct 16, 2026
Merged

heyJordanParker merged 11 commits into
masterfrom
feat/files

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Completes: §27

A PHP# developer finds every type by its namespace and its file, and a Position.current() default tells a method who called it. Section 27's two failing examples, one-type-per-file and position, now pass, and every rule line of the section holds in the checker, the bridge, the engine and the Composer plugin. docs/architecture/slices/27-files.md lists each line with its state and mechanism.

A .sharp file declares one type, named like the file

  • A second type is a compile error: "A .sharp file declares one type: move StoreHelper to StoreHelper.sharp."
  • A type named unlike its file, case included, as in Java, is a compile error: "Bill.sharp declares Invoice: rename the file Invoice.sharp, or the type Bill."
    • The type named like the file is the file's own wherever it sits, so in Second.sharp holding First then Second, First moves.
    • "or the type {name}" shows only when the file's name can name a type, so store-helper.sharp, Order.Extra.sharp and invoice.sharp get "rename the file Invoice.sharp." alone.
  • C#'s partial before a type is a compile error: "PHP# has no partial classes: a class lives in one file."
  • An extern declaration file and a Module.sharp file hold no type, so any name is theirs.
  • The Composer plugin's class map lists a file only when it declares a type named like it, on a line that may start with attributes, as [Attribute] public class Marker, and never on an extern line. Before, it listed Sharp\Module for the standard library's Module.sharp, a class for every misnamed file, nothing for Marker, and App\Failed for a Failed.sharp holding extern event Failed;.
  • \Sharp\Internal\testModule takes one class name, since a test file's module now covers the one class its file declares.

PHP tags in a .sharp file are named parse errors

  • "<?php is PHP syntax: a .sharp file has no opening tag, so delete it." <? gets the same, <?= gets "a .sharp file has no tags, and writes output with global.printf", and ?> gets "a .sharp file has no closing tag, so delete it". The file parses on, so its other errors still show.
  • List<?int> still gets "? before a type is PHP syntax", and List<int?> still parses.
  • A leading UTF-8 byte order mark is skipped.

A Position.current() default gives the caller's position

As C++20's std::source_location::current() and Swift's #file defaults do. MetadataFlags::CALLER_POSITION marks the parameter, and each direct PHP# call that leaves it out passes caller: new \Sharp\Position(__FILE__, 12, 9, 'App.Reports.run'), the start of the call: a static or instance call, new, a named constructor, : this(…), : super(…), a generic call, an interface's default body and an extension call. Types::sharp_method(class, method) finds the callee of each, and replaces sharp_constructor.

Reports.logSlow("slow query");                // the position of this call, in App.Reports.run
Reports.logSlow("x", caller: given);          // given
const log = Reports.logSlow;
log("slow query");                            // the position where logSlow declares caller, as C++ and Swift fill only a direct call
Reports.logSlow(...["a"]);                    // this call's position: a List<string> can't fill caller
Reports.logSlow("a", ...anything);            // a List<Any> may fill caller, so the declaration's position
int line = Position.current().line            // compile error: Take the whole position: `Position caller = Position.current()`.

Any default computed from Position.current(), as Position.current().line + 1, gets "Take the whole position". A plain PHP caller gets the position where the parameter is declared, which is what the default lowers to.

Tests follow the rule

Every test source declares one type per file, with no exemption. The engine fixtures are one folder each, loaded through fixture() in Zend/tests/sharp/harness/fixture.inc, which maps each file by its namespace line and its name, as the Composer plugin does. #143's Results.sharp is the Results/ folder. The spec's one-body-per-method and sub-interface-defaults write one block per file, and the two section 27 baseline entries are gone.

Verification

  • Spec: sharp/bin/spec gives Checked 202 examples, ignored 1, and 9 fail, the 9 of master's baseline that this PR leaves, and exits 0. one-type-per-file, position and file-header pass. sharp/bin/spec --self-test gives 23 of 23 self-test examples give the problems they expect.
  • One test per rule, with its forms as rows:
    • The one-type rule: a_sharp_file_declares_one_type, a_sharp_file_is_named_like_its_type and a_sharp_file_without_a_type_takes_any_name in crates/semantics/tests/sharp.rs.
    • The tags, the byte order mark and partial: an_opening_tag_is_a_php_syntax_error, every_php_tag_is_a_php_syntax_error, a_byte_order_mark_is_skipped and a_partial_type_is_an_error in crates/syntax/tests/sharp.rs.
    • The caller's position: position_current_as_a_parameter_default_is_the_callers_position and a_default_computed_from_position_current_is_an_error in semantics, the two position tests in crates/sharp-bridge/tests/lower.rs, and position_caller.phpt, whose 14 rows cover each call form above.
    • The class map: library_autoload.phpt prints the class map the plugin writes, and loads App\Stamp, whose type line starts with an attribute.
  • Engine: sharp/bin/test --opcache Zend/tests/sharp passes 300 of 300 in each of its 3 modes on macOS.
    • position_caller.phpt, position_caller_preload.phpt, results.phpt, results_preload.phpt and the library autoload tests pass in all 22 modes.
    • position_caller passes 200 runs and results 100 runs under USE_ZEND_ALLOC=0 MallocScribble=1, each with and without preloading.
  • Crates: the syntax, semantics, codex, names, hir, bridge, analyzer, formatter, linter, orchestrator and fingerprint crates and the mago binary pass their tests, with the PHP SDK's tests required. cargo clippy --workspace --all-targets -D warnings and cargo fmt --check are clean.
  • Plain PHP: every change reads only a .sharp file, and the analyzer's tests pass unchanged.

Section 27 holds on master except four gaps: one type per file is
silently accepted, a Position.current() default is refused, preloading
a Composer project crashes the engine in readModule, and <?php in a
.sharp file gets a generic parse error. The note lists every rule line
with its state on master, the mechanism that builds it, the code it
reuses, and the files it changes. team-lead approved decisions 1 to 10.

docs/architecture/slices/
└── 27-files.md    new: section 27's rules, states, decisions and files
…ition.current() default gives the caller's position

Spec section 27, PHP# files:

- The checker refuses a second type in a .sharp file, "A `.sharp` file declares
  one type: move `X` to `X.sharp`.", and a type named unlike its file, case
  included, as in Java. An extern file and a module file hold no type.
- `<?php` in a .sharp file is one parse error that names the fix.
- As a parameter's default, `Position.current()` gives a PHP# call's own
  position: MetadataFlags::CALLER_POSITION marks the parameter, and
  Lowering::sharp_arguments passes `caller: new \Sharp\Position(…)` for each one
  a call leaves out. A call through a function value and a plain PHP caller get
  the position of the declaration. A default that reads one part of the
  position is an error.
- The Composer plugin's class map lists a file only when it declares a type
  named like it.
- \Sharp\Internal\testModule takes one class name.
- Every test source declares one type per file. The engine fixtures are one
  folder each, loaded through fixture() in Zend/tests/sharp/harness/fixture.inc.
- The spec's one-body-per-method and sub-interface-defaults examples write one
  block per file, and the section 27 baseline entries are gone.
Master's fixture changes go into the split files, one type per file:
Inheritance/Ranked.sharp, TypeArgumentsCalls/{Wrapped,Tally,Tallies,Fragile,Runs}.sharp,
the Structs fixture becomes the Structs folder, and #149's Orchard files move into
Orchard/. CALLER_POSITION moves to bit 62 beside master's RUNTIME_INITIAL_VALUE.
Master's new test sources split per type.
…o files of their own

Section 27's one-type rule refused their one-file source. analyze_files_with_fixture
analyzes several files as one project, and analyze_with_fixture is its one-file form.
…nd the file rules name their fixes

- A named constructor, `: this(…)` and an extension call pass the caller's position, through one lookup,
  Types::sharp_method, which replaces sharp_constructor. A call that spreads a list passes it unless the list
  may fill the Position parameter.
- Any parameter default computed from Position.current() is "Take the whole position".
- The type named like its file is the file's own, and "or the type" is offered only for a name a type can take.
- `<?`, `<?=` and `?>` are named PHP syntax errors, a leading byte order mark is skipped, and `partial` before a type
  says a class lives in one file.
- The class map reads a type line that starts with attributes, and never an extern line.
- The one-type check and the declaration check read the file's statements in one walk.
The Results fixture of #143 becomes the Results folder, one type per file, which fixture() loads and its preload
requires in declaration order. Four #143 tests in semantics and the bridge give each type its own file, and hir maps
the new partial-type parse error.
…'s position

A bridge test runs Types::spread_can_fill for a List<string> spread and a List<Any> one. Only a List spreads into a
call and a PHP# parameter has a type, so the function keeps no branch for either.
… checker and the bridge

Each of the three kept its own copy of the Position.current() test, so they could disagree on which default
takes the caller's position. ResolvedNames::is_current_position, beside static_call_class, replaces all three.
sharp_arginfo.h is regenerated from the merged stub, which holds #153's statics signature and this branch's
testModule. Two #153 bridge tests keep #153's names and expectations, with Order and Tally in files of their own.
#145's NamesImports.sharp becomes the NamesImports folder, one type per file, which names_imports.inc requires
for the request and for preloading. hir's comment names both new parse errors.
@heyJordanParker
heyJordanParker merged commit 605ac2e into master Oct 16, 2026
20 checks passed
heyJordanParker added a commit that referenced this pull request Oct 16, 2026
Problem
- #152, PHP# files, landed, and #136 conflicted with master in four
  files.
- Both sides took bit 62 of MetadataFlags: #152's CALLER_POSITION and
  this branch's TAKES_ON_EFFECTS.

Solution
- codex/src/metadata/flags.rs keeps both flags. TAKES_ON_EFFECTS moves
  to bit 63, the last free bit, and the accessor and the flag test list
  cover both.
- ext/sharp/sharp.c's container globals comment keeps #152's one class
  per test file and this branch's per-fiber scopes.
- ext/sharp/sharp_arginfo.h is regenerated from the merged stub. Against
  master it adds runScoped and drops Container::scope, as before.
- hir/src/lower/error/mod.rs names both `partial` before a type and a
  function parameter a method names in `uses`.
- Every .sharp file this branch adds already declares one type, so no
  fixture splits.

ext/sharp
├── sharp.c            container globals comment
└── sharp_arginfo.h    regenerated
sharp/mago/crates
├── codex/src/metadata/flags.rs   TAKES_ON_EFFECTS at bit 63
└── hir/src/lower/error/mod.rs    both comments
heyJordanParker added a commit that referenced this pull request Oct 16, 2026
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.
@heyJordanParker
heyJordanParker deleted the feat/files 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