Skip to content

Semantic P4: compile type hints through SourceType; function-level @template, class-string<T>, conditional returns - #696

Merged
blacktrs merged 140 commits into
mainfrom
semantic-p4
Nov 15, 2026
Merged

blacktrs merged 140 commits into
mainfrom
semantic-p4

Conversation

@blacktrs

Copy link
Copy Markdown
Contributor

Summary

P4 of the semantic-core plan: the compiler reads docblocks and type hints through the shared semantic layer, and @template T + class-string<T> bind at call sites in both the analyzer/LSP and the compiler.

Supersedes #691 (GitHub would not merge a stacked PR).

Changes

  • One docblock scanner — LowerTypes reads tags via Analyze\DocType; @param T &$x now types the param. Tags inside backtick code spans / glued to words are prose, not tags.
  • SourceTypeParser takes the full hint grammar — hyphenated pseudo-types, class-constant refs, unsealed shapes, \Closure(...) signatures. tools/typehint_corpus.php checks coverage.
  • lowerTypeHint = parse cache + lowerSource(SourceType) — string ladder deleted (−613 lines), IR byte-identical, front end −1.2% instructions. Shadow check MANTICORE_TYPEHINT_CHECK found 0 mismatches.
  • Analyzer/LSP (Function-level @template bound by class-string<T>: typed returns in the analyzer and the compiler #686) — Analyze\TemplateSig, binding from Foo::class / class-string<Foo> / T-typed args, substituted @return, key-of/value-of, phpstan > psalm > plain precedence, new rule type.class-string.
  • Conditional returns — SourceType::CONDITIONAL ($x is T ? A : B) (nested, is not), evaluated at the call in analyzer and compiler; string|class-string<T> params bind T.
  • Compiler (Function-level @template bound by class-string<T>: typed returns in the analyzer and the compiler #686) — call with a class-string literal bound to @template T returns obj<T> behind instanceof + TypeError guard (never a blind cast). Works for functions, methods, interface/abstract/parent declarations, {@inheritdoc} overrides; param/property/$this receivers.

Not covered

Static calls on interfaces. T|object returns (Symfony ContainerInterface::get) stay object — faithful to the doc.

Verification

  • bin/build fixpoint each step (latest f89d50d1); analyze --json src unchanged.
  • tests/semantic (templates 33 checks, source_type), tests/analyze class_string_arg, AOT template_class_string_* (8, incl. lying-doc TypeError cases red on the previous compiler), filtered AOT runs green.
  • symfony-console: 4 sites rewritten, −51 KB.
  • Not run yet: full AOT suite, difftest, Linux gate.

… `@param T &$x` now types the param like `@param T $x`
…do-types, class-constant refs, unsealed shapes, \Closure signatures); add typehint corpus tool
…tags (prose `@var T` typed Type::$interned as junk)
…ain reference, drop the legacy-interpolation site

(cherry picked from commit 5052d09)
…d delete the string ladder

lowerTypeHint parses each distinct hint string once into an immutable
Semantic\SourceType (class context applied at lowering) and lowers it with
lowerSource; an unparseable hint is unknown. Removes lowerTypeHintText,
lowerArrayShape, nullableUnionArm, isArrayUnion, isNumericUnion,
topLevelUnionArms, splitTopLevel, topLevelColon, lowerElemHint,
lowerCallableSignature and the MANTICORE_TYPEHINT_CHECK shadow mode.

Parser: accepts non-empty-array{...} / non-empty-list{...}; an unkeyed
shape entry after an int key takes key+1 (array{1: int, string} keys the
string 2); ?T|null parses as T|null. Behaviour changes on forms seen in no
corpus: null|callable(): T now lowers like callable(): T|null (closure,
was cell); Generator<array<int,string>> lowers its args
instead of splitting on the nested comma.
…rgs, key-of/value-of, class-string literal diagnostic
…ral as obj<T>, guarded by instanceof + TypeError (#686 step 4b)
…bstract/parent methods, inherited by undocumented overrides (#686)
@blacktrs
blacktrs merged commit 8c817ed into main Nov 15, 2026
10 checks passed
@blacktrs
blacktrs deleted the semantic-p4 branch November 15, 2026 08:05
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