Repository navigation
Namespaces and imports, and keyword case: sections 23 and 2 - #145
Merged
Merged
Conversation
…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
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.
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: §2, §23
Problem
Section 2's
keyword-caseexample failed: a keyword in another case is a parse error, and the checker ran no PHP# check on a file with a parse error, soTRUEon the next line went unreported.THIS,This,SUPER,SELF,Typeof(…),Nameof(…),Exit(…),value IS int,Case Paid;in an enum andPublic classgot no suggestion, andpublic Struct Pointgot a second, generic parse error.Section 23 held for its examples, but the checker let through or misreported the rest of it:
new,nameof,typeof,catch, a class header, an attribute or anexterngot "Cannot find type", "ClassAppnot found" or a bare parse error, instead of the import to add.Behavior,ClassMembers,Dispatcher,ImmediateDispatcher,InvalidFieldsandListeningare public classes directly inSharp, yet were no default name, because the name resolver kept a hand-written list.import Vendor.Pdf uses Files;was two generic parse errors..phpfile, whichmago compilenever prints.Math.max(a, b)without its import was "MethodMath.maxdoes 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.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 keywordthisin lowercase.", and "Write the keywordSelfwith a capital S and the rest in lowercase." A class namedThis,ReturnorIfkeeps its name. The parser readsIS,As,Casein an enum,Public classandpublic Structas their keywords with one error.check_use_statementssays. A class, a standard effect, or a namespace that holds a plain PHP function or constant compiles. A single constant isconstant-import, "An import names a class or a namespace, not a constant: importVendor.Pdf, and readPdf.DEFAULT_DPI." A namespace of classes only isclass-namespace-import, and lists every class to import but a module's class and the default names.import Sharp;is "Sharpholds only default names, which every file names with no import: remove the import." Anything else,import Log;included, is "Imported class, interface, trait, or enumLogdoes not exist."importlines: addimport App.Order;and writeOrder." The parser reads a dotted name in every place a type or class stands,is,matchtype patterns andonclauses included, and the semantic checks pass a dotted bound and anewthat 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, asShop.OrderinsideApp, gets the same error. A library function or constant read through a namespace of two or more parts names the namespace import, and anexterntarget 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 itstypeofhave the error type. So has a PHP# file'stypeof(Missing), while a plain PHP file'sMissing::classkeeps its issues.Eventis not imported.", and the keyword is suggested only when no class has that name. A class ofSharp.Internalis never suggested as an import outside the standard library.Sharpis a default name, from one source.sharp/mago/scripts/regen-sharp-classes.phpreads the standard library and the prelude and writescrates/names/src/sharp_classes.rs. A test runs it and fails when the file disagrees. The prelude'sListMethods,MapMethods,SetMethodsandIterableMethodsmove toSharp\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 carriesuses: declare the library's effect once, withextern."App.Tenant.Storeis 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.Mathis not imported.", with the import as help, and "Read the library's constant through its namespace: writePdf.DEFAULT_DPI."ReturnandIf. It runs in every opcache mode and under preloading.Names beyond the design note
ParseError::UsesOnImportInSharp.mago_names::NAMED_EFFECTS, moved from the analyzer, which now imports it.sharp/mago/scripts/regen-sharp-classes.php, the generatedcrates/names/src/sharp_classes.rs, and the Justfile reciperegen-sharp-classes.Zend/tests/sharp/harness/Names.inc, the engine test's plain PHP library, wheremago compilereads plain PHP.constant-importandclass-namespace-import, listed insharp/mago/scripts/regen-analyzer-issue-codes.php.On.eventsholdsIdentifiers in place ofLocalIdentifiers, so a dotted event is a name.mago-semanticsis a dev-dependency ofmago-analyzer, soa_full_name_anywhere_in_code_names_the_import_to_addruns the semantic checks and the analysis, asmago compiledoes.Test fixtures
method_intemplate importedLib.Calcwith noLib\Calcdeclared, which is now an error. It importsLib.Calconly when the code namesCalc, and writes a blank line otherwise, so every line number stays.catch, a type named byimport Log;, and a class directly inSharpthat a newer standard library declares than the generated list holds.a_sharp_class_cannot_implement_the_struct_markerandan_extern_target_that_names_nothing_is_a_non_existent_class_method_or_functionimport a missing class, and now also report the import.a_data_case_is_a_type_of_its_ownanda_case_pattern_on_no_case_reports_only_thatrefusedShop.OrderandShapes.Boxas "no case of an enum", and now get the full-name error.a_full_name_inside_code_names_the_import_to_addanda_member_read_through_a_full_name_names_the_import_to_addlose theirmixed-return-statement.typeof_is_the_class_name_as_in_phpasserts the PHP# and the PHP issues apart, sincetypeof(Missing)no longer reports where it is passed.Sharp.Json.JsonandVendor.Package.Class0toClass4071, which now must exist, so their projects declare them.names_imports_preload.phptdefines the library's constant in the request, as a Composerfilesentry would.Merge with master
The Result Slice added
Resultto a hand-written default list and madeErrora default name nothing shadows. The merge keeps one mechanism: the generatedSHARP_CLASSES, regenerated so it holdsSharp\ErrorandSharp\Result, and the Result Slice'sfixed_default_class, whoseErrordefault_classes()leaves out.Left for later
handle(object $event), keeps master's behavior until Jordan decides it.Verification
sharp/bin/specon the merge with master45644dbb3a: "Checked 202 examples, ignored 1, and 10 fail", each insharp/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/teston the merge: 300 of 300 pass without opcache, and the names and Result engine tests pass with the opcache file cache.names_imports.phptandnames_imports_preload.phptpass 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 withMallocPreScribble=1too.clippy -D warningson those crates with--all-targets, andcargo fmt --all --check. Each new rule's test was seen failing before its code.mago compileofZend/tests/sharp(175 files) and of the standard library (31 files) is accepted.mago analyzeofbuild/gen_stub.php,run-tests.php, the Composer plugin'ssrcand the PHP SDK'ssrcgives the same 2104 issues with this branch and with its base0b98cd4a46.