Skip to content

Namespaces and imports, and keyword case: sections 23 and 2 - #145

Merged
heyJordanParker merged 8 commits into
masterfrom
feat/names-imports
Oct 16, 2026
Merged

heyJordanParker merged 8 commits into
masterfrom
feat/names-imports

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Completes: §2, §23

Problem

Section 2's keyword-case example failed: a keyword in another case is a parse error, and the checker ran no PHP# check on a file with a parse error, so TRUE on the next line went unreported. THIS, This, SUPER, SELF, Typeof(…), Nameof(…), Exit(…), value IS int, Case Paid; in an enum and Public class got no suggestion, and public Struct Point got a second, generic parse error.

Section 23 held for its examples, but the checker let through or misreported the rest of it:

  • An import that names nothing, a namespace of classes only and a single constant compiled.
  • A full class name in a type, new, nameof, typeof, catch, a class header, an attribute or an extern got "Cannot find type", "Class App not found" or a bare parse error, instead of the import to add.
  • Behavior, ClassMembers, Dispatcher, ImmediateDispatcher, InvalidFields and Listening are public classes directly in Sharp, yet were no default name, because the name resolver kept a hand-written list.
  • import Vendor.Pdf uses Files; was two generic parse errors.
  • A plain PHP class named like a PHP# namespace compiled, because the error landed on the .php file, which mago compile never prints.
  • Math.max(a, b) without its import was "Method Math.max does not exist.", and a bare library constant was "Undefined constant".

Solution

The design is docs/architecture/slices/23-names-imports.md, updated to what was built.

  • A miscased keyword no longer hides the file's other errors. The checker and the bridge run past parse errors that are all MiscasedKeywordInSharp, since that error reads the token as the keyword and leaves the tree whole. Where no such name exists, the analyzer suggests the keyword for a value, a type or a call, as "Write the keyword this in lowercase.", and "Write the keyword Self with a capital S and the rest in lowercase." A class named This, Return or If keeps its name. The parser reads IS, As, Case in an enum, Public class and public Struct as their keywords with one error.
  • Every PHP# import is checked, whatever check_use_statements says. A class, a standard effect, or a namespace that holds a plain PHP function or constant compiles. A single constant is constant-import, "An import names a class or a namespace, not a constant: import Vendor.Pdf, and read Pdf.DEFAULT_DPI." A namespace of classes only is class-namespace-import, and lists every class to import but a module's class and the default names. import Sharp; is "Sharp holds only default names, which every file names with no import: remove the import." Anything else, import Log; included, is "Imported class, interface, trait, or enum Log does not exist."
  • A full class name anywhere in code names the import to add: "Full names appear only in import lines: add import App.Order; and write Order." The parser reads a dotted name in every place a type or class stands, is, match type patterns and on clauses included, and the semantic checks pass a dotted bound and a new that a member chain reads through on to the analyzer, which refuses the name before the bridge sees it. A name relative to the file's namespace, as Shop.Order inside App, gets the same error. A library function or constant read through a namespace of two or more parts names the namespace import, and an extern target of three or more parts is refused. The refused name reports nothing more: a pattern's variable, a call and a member read through it, and its typeof have the error type. So has a PHP# file's typeof(Missing), while a plain PHP file's Missing::class keeps its issues.
  • A class named like a keyword is never told to write the keyword. A short name that one class has gets "Event is not imported.", and the keyword is suggested only when no class has that name. A class of Sharp.Internal is never suggested as an import outside the standard library.
  • Every public class directly in Sharp is a default name, from one source. sharp/mago/scripts/regen-sharp-classes.php reads the standard library and the prelude and writes crates/names/src/sharp_classes.rs. A test runs it and fails when the file disagrees. The prelude's ListMethods, MapMethods, SetMethods and IterableMethods move to Sharp\Internal, so they are no default name. Every default name can be shadowed by a class of the file's namespace or an import.
  • import X uses Y; is one error: "An import never carries uses: declare the library's effect once, with extern."
  • A PHP# class whose namespace is a plain PHP class is refused on the PHP# file: "App.Tenant.Store is both a plain PHP class and a namespace: rename the class or the folder." The warm path re-analyzes the namespace's files when such a class appears or goes.
  • Two errors name their fix: "Math is not imported.", with the import as help, and "Read the library's constant through its namespace: write Pdf.DEFAULT_DPI."
  • An engine test pins the runtime meaning of a library's function and constant through its namespace, a renamed import from PHP# and from plain PHP, an import and a class of the file's namespace that shadow a default, and classes named Return and If. It runs in every opcache mode and under preloading.

Names beyond the design note

  • The parse error ParseError::UsesOnImportInSharp.
  • mago_names::NAMED_EFFECTS, moved from the analyzer, which now imports it.
  • sharp/mago/scripts/regen-sharp-classes.php, the generated crates/names/src/sharp_classes.rs, and the Justfile recipe regen-sharp-classes.
  • Zend/tests/sharp/harness/Names.inc, the engine test's plain PHP library, where mago compile reads plain PHP.
  • The issue codes constant-import and class-namespace-import, listed in sharp/mago/scripts/regen-analyzer-issue-codes.php.
  • On.events holds Identifiers in place of LocalIdentifiers, so a dotted event is a name.
  • mago-semantics is a dev-dependency of mago-analyzer, so a_full_name_anywhere_in_code_names_the_import_to_add runs the semantic checks and the analysis, as mago compile does.

Test fixtures

  • The bridge's method_in template imported Lib.Calc with no Lib\Calc declared, which is now an error. It imports Lib.Calc only when the code names Calc, and writes a blank line otherwise, so every line number stays.
  • A missing class that is no keyword, no full name and the short name of no class the file could import keeps the error of its place: an attribute, a catch, a type named by import Log;, and a class directly in Sharp that a newer standard library declares than the generated list holds.
  • a_sharp_class_cannot_implement_the_struct_marker and an_extern_target_that_names_nothing_is_a_non_existent_class_method_or_function import a missing class, and now also report the import.
  • a_data_case_is_a_type_of_its_own and a_case_pattern_on_no_case_reports_only_that refused Shop.Order and Shapes.Box as "no case of an enum", and now get the full-name error. a_full_name_inside_code_names_the_import_to_add and a_member_read_through_a_full_name_names_the_import_to_add lose their mixed-return-statement. typeof_is_the_class_name_as_in_php asserts the PHP# and the PHP issues apart, since typeof(Missing) no longer reports where it is passed.
  • The CLI's structure rule tests import Sharp.Json.Json and Vendor.Package.Class0 to Class4071, which now must exist, so their projects declare them.
  • PHP's preloading keeps no constant, so names_imports_preload.phpt defines the library's constant in the request, as a Composer files entry would.

Merge with master

The Result Slice added Result to a hand-written default list and made Error a default name nothing shadows. The merge keeps one mechanism: the generated SHARP_CLASSES, regenerated so it holds Sharp\Error and Sharp\Result, and the Result Slice's fixed_default_class, whose Error default_classes() leaves out.

Left for later

  • A PHP# override whose parent's parameter is named after a PHP# keyword, as handle(object $event), keeps master's behavior until Jordan decides it.

Verification

  • sharp/bin/spec on the merge with master 45644dbb3a: "Checked 202 examples, ignored 1, and 10 fail", each in sharp/baseline/spec. Every example of sections 2 and 23 passes.
  • sharp/bin/spec --base origin/master: "0 entries join sharp/baseline/spec, 1 leave it."
  • sharp/bin/spec --self-test: 20 of 20 self-test examples pass.
  • sharp/bin/test on the merge: 300 of 300 pass without opcache, and the names and Result engine tests pass with the opcache file cache.
  • names_imports.phpt and names_imports_preload.phpt pass with --opcache --repeat --passes: without opcache, with the file cache primed and used, twice in one process, each optimizer pass alone, and both JITs. They pass with freed memory scribbled (USE_ZEND_ALLOC=0 MallocScribble=1) in three rounds of --opcache --repeat, and 25 times in one process with MallocPreScribble=1 too.
  • mago: the syntax (226 PHP# tests), names, semantics, hir, codex, prelude, analyzer (2578 PHP and 931 PHP# tests), sharp-bridge (388 lowering tests) and orchestrator suites, clippy -D warnings on those crates with --all-targets, and cargo fmt --all --check. Each new rule's test was seen failing before its code.
  • mago compile of Zend/tests/sharp (175 files) and of the standard library (31 files) is accepted.
  • A plain PHP project's analysis is unchanged: mago analyze of build/gen_stub.php, run-tests.php, the Composer plugin's src and the PHP SDK's src gives the same 2104 issues with this branch and with its base 0b98cd4a46.

…ord case

Problem: section 2's keyword-case example fails, and probing every rule
line of section 23 found imports the spec refuses that compile, full names
and miscased keywords whose errors don't say what the spec says, six
classes directly in Sharp that are no default name, and a plain PHP class
named like a PHP# namespace that mago compile accepts.

Solution: the design note holds every rule line of section 23 and of
section 2's keyword-case rule, its state on master 0b98cd4, the
mechanism that builds it, the code it reuses, and team-lead's answers.

docs/architecture/slices/
└── 23-names-imports.md    new: the rule-by-rule design of the Slice
…old in the checker

Problem: a miscased keyword hid every other PHP# error of its file, so
section 2's keyword-case example failed, and THIS, SELF, Typeof(…),
value IS int and Public class got no suggestion. An import that names
nothing, a namespace of classes or a constant compiled. A full class name
in a type, new, catch, a header, an attribute or an extern got "Cannot
find type" or a parse error. Six classes directly in Sharp were no default
name, from a hand-written list. import X uses Y was two generic parse
errors. A plain PHP class named like a PHP# namespace compiled. Math
without its import and a bare library constant got errors that don't
name the fix.

Solution: the checker runs past a parse error that is only a miscased
keyword, and the analyzer suggests the keyword where no such name exists.
Every PHP# import is checked: a class, a namespace of functions and
constants, or a standard effect. One reporter names the import to add for
a full class name wherever a missing class is reported, and the parser
reads a dotted name in every place a type or class stands. The default
names are generated from the standard library and the prelude, and a test
fails when they disagree. The prelude's collection classes move to
Sharp\Internal. A new parse error says an import never carries uses. The
PHP# side reports a plain PHP class named like its namespace, and the warm
path re-analyzes its files. Not-imported and bare-constant errors name
the fix. An engine test pins the runtime meaning in every opcache mode
and under preloading.

docs/architecture/slices/23-names-imports.md       the note matches what was built
sharp/baseline/spec/§2/keyword-case                 deleted: the example passes
sharp/mago/
├── Justfile, scripts/regen-sharp-classes.php       new: generate the default class list
└── crates/
    ├── syntax/                                     miscased keywords, dotted names, uses on an import
    ├── hir/                                        lowers the new parse error
    ├── semantics/                                  checks run past a miscased keyword
    ├── names/                                      generated default names, NAMED_EFFECTS
    ├── prelude/, codex/                            collection classes in Sharp\Internal
    ├── analyzer/                                   imports, full names, keywords, not imported,
    │                                               plain PHP class named like a namespace
    ├── orchestrator/                               warm path for a namespace's files
    └── sharp-bridge/                               every error past a miscased keyword
Zend/tests/sharp/
├── NamesImports.sharp, harness/Names.inc           new: the fixture and its plain PHP library
├── names_imports.inc                               new: the driver
└── names_imports.phpt, names_imports_preload.phpt  new: every opcache mode, and preloading
Problem: patch-coverage found 15 added lines no test ran: a bare THIS,
an import of PHP's root constant, a name the file imports that names no
class, a class directly in Sharp the generated list lacks, and an unknown
attribute or caught class that is none of the names the spec describes.

Solution: one row each in the keyword and import tests, and one test that
a missing class the spec says nothing about keeps the error of its place.
Each row was probed to reach its line.

sharp/mago/crates/analyzer/tests/
└── sharp.rs    the rows and the new test
Problem

- A class that isn't imported but is named like a keyword, as `Event`,
  `Override` or `Match`, got "Write the keyword in lowercase", which
  can't compile.
- A full name got an error that doesn't name the import in a `new` or
  an `is` of three or more parts, a `match` type pattern, a type
  parameter's bound and an `on` clause, and so did a name relative to
  the file's namespace, as `Shop.Order` inside `App`. A pattern's
  variable of a refused class reported again where it was read.
- A bare `ListMethods` suggested `import Sharp.Internal.ListMethods;`,
  which the checker then refuses.
- `import Sharp;` listed every default name and `Sharp.Module` as
  imports to add.
- A constant import and a namespace of classes only had no issue code
  of their own.
- The CLI's structure rule tests imported classes that don't exist.

Solution

- `report_unknown_sharp_class` tries "not imported" when one class has
  the short name, and the keyword only when none has it.
- The parser reads a dotted type pattern of three or more parts and a
  dotted event in `on`, whose `On.events` now holds names. The semantic
  checks pass a dotted bound and a `new` a member chain reads through
  on to the analyzer. `report_full_name` also starts from the file's
  namespace, `report_dotted_types` gives the full-name error, and a
  PHP# `instanceof` of no class narrows to the error type.
- `report_not_imported` skips `Sharp\Internal` classes outside the
  standard library, through `is_sharp_internal`.
- `imports_of_namespace` leaves out modules and default names, and a
  namespace with none left says it holds only default names.
- The new codes `constant-import` and `class-namespace-import`, from
  the regenerated issue-code list.
- The CLI tests declare the classes they import.

sharp/mago/
├── scripts/regen-analyzer-issue-codes.php   the two new codes
├── tests/compile.rs                         the projects declare what they import
└── crates/
    ├── syntax/src/
    │   ├── cst/cst/class_like/method.rs,
    │   │   cst/node.rs, walker/mod.rs       On.events holds names
    │   └── parser/internal/
    │       ├── pattern.rs                   a long dotted type pattern
    │       ├── type_hint.rs                 peek_dotted_name
    │       └── class_like/method.rs         a dotted event
    ├── names/src/internal/walker.rs         resolves a dotted event
    ├── semantics/src/internal/checker/
    │   └── sharp.rs                         a dotted bound, a `new` a chain reads through
    └── analyzer/
        ├── Cargo.toml                       mago-semantics for the tests
        ├── src/
        │   ├── code.rs                      regenerated
        │   ├── resolver/class_name.rs       not imported before the keyword
        │   ├── expression/access/mod.rs     the file's namespace, `new` refused once
        │   ├── expression/binary/mod.rs     a test of no class narrows to the error type
        │   ├── expression/call/mod.rs,
        │   │   statement/try.rs             a refused name reports nothing more
        │   ├── statement/class_like/
        │   │   enum_case.rs, listener.rs    the full-name error
        │   ├── statement/use_statement.rs   the new codes, the default names left out
        │   └── utils/names.rs               is_sharp_internal
        └── tests/sharp.rs                   a row for each, seen failing first
docs/architecture/slices/23-names-imports.md  updated to what was built
Problem

patch-coverage found no test that runs a named constructor without
arguments outside a member chain, the case `is_member_chain_object`
returns false for.

Solution

A semantics test refuses `new Report.of`, and leaves `new
App.Shop.Order()` and `new App.Shop.total` to the analyzer.

sharp/mago/crates/semantics/tests/
└── sharp.rs   a_new_without_arguments_is_refused_unless_a_member_chain_reads_through_it
Problem

`typeof(Missing)` and `typeof(App.Shop.Order)` in a `.sharp` file were
the literal class string of the name, so reading `.name` on it or
passing it on reported a second error, such as "expected `string`, but
found `null`".

Solution

`class_value` gives a class that does not exist the error type after
reporting it, so nothing that reads it reports again. Its symbol
reference is still recorded. `typeof` is PHP# syntax only, so a plain
PHP file's `Missing::class` keeps its issues, and
`typeof_is_the_class_name_as_in_php` now asserts both.

sharp/mago/crates/analyzer/
├── src/expression/type_of.rs   a missing class's value has the error type
└── tests/sharp.rs              the typeof parity test, and a typeof(…).name row
docs/architecture/slices/23-names-imports.md   typeof in the refused-name paragraph
Problem

#145 was behind master, which gained #142, #143, #144, #146 to #150.
Both sides changed the default names: this branch generates them from
the standard library into `SHARP_CLASSES`, and #143 added `Result` to a
hand-written list and made `Error` a default name nothing shadows.

Solution

One mechanism: `default_classes()` stays `NAMED_EFFECTS` plus the
generated `SHARP_CLASSES`, regenerated from the merged library, which now
holds `Sharp\Error` and `Sharp\Result`. #143's `fixed_default_class`
stays, and `default_classes()` leaves out the class it fixes, so `Error`
is never shadowable while `Result` is.

sharp/mago/crates/names/src/
├── lib.rs                 default_classes and fixed_default_class
├── sharp_classes.rs       regenerated: Error and Result join
└── internal/walker.rs     both imports kept
docs/architecture/slices/23-names-imports.md   Error is the one fixed default name
@heyJordanParker
heyJordanParker merged commit fc90b0e into master Oct 16, 2026
20 checks passed
heyJordanParker added a commit that referenced this pull request Oct 16, 2026
#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 added a commit that referenced this pull request Oct 16, 2026
Problem
- #145, names and imports, landed, and #136 conflicted with master in
  three files. Both sides added a ParseError variant in the same place,
  and #145 moved NAMED_EFFECTS into mago_names.
- #145 generates the list of classes declared in `Sharp`, and that list
  lacked this branch's `Sharp\Scope`.

Solution
- syntax/src/error.rs keeps both variants in every match:
  FunctionParameterInUsesInSharp and #145's UsesOnImportInSharp.
- hir/src/lower/error/mod.rs keeps #145's comment and adds a function
  parameter a method names in `uses`.
- effects/check.rs imports NAMED_EFFECTS from mago_names, as #145 does,
  and keeps this branch's Binding import.
- names/src/sharp_classes.rs is regenerated with
  `php scripts/regen-sharp-classes.php ../composer/library`, the command
  of `just regen-sharp-classes`, and gains `Sharp\Scope`.

sharp/mago/crates
├── syntax/src/error.rs             both variants
├── hir/src/lower/error/mod.rs      both comments
├── analyzer/src/effects/check.rs   both imports
└── names/src/sharp_classes.rs      regenerated, gains Sharp\Scope
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/names-imports 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