Skip to content

fix: support class constants in defined() - #967

Merged
nahime0 merged 14 commits into
mainfrom
cursor/fix-defined-class-constant-752-ce30
Sep 14, 2026
Merged

nahime0 merged 14 commits into
mainfrom
cursor/fix-defined-class-constant-752-ce30

Conversation

@nahime0

@nahime0 nahime0 commented Sep 11, 2026

Copy link
Copy Markdown
Member

Summary

  • resolve literal defined('Class::CONST') names through class, interface, trait-imported, and enum metadata instead of the global-constant table alone
  • preserve PHP class/member casing, inheritance, implemented interfaces, visibility, lexical self::/parent::, named arguments, and runtime-called-class static:: behavior
  • preserve the hidden called-class capture through class-scope closures and Closure::bind, bindTo, and call on every supported target
  • compose trait constants in the frontend, accepting declarations with equivalent folded PHP values and rejecting incompatible trait/class compositions before canonical class metadata is built
  • raise catchable PHP Errors for invalid relative scopes and mark defined() as potentially throwing so exception optimization preserves handlers
  • document the behavior and demonstrate class and late-static probes in the classes example

Verification

  • PHP 8 cross-checks for late-static, named, trait-imported, equivalent/conflicting trait, visibility, namespace/casing, and invalid-scope behavior
  • cargo test --test codegen_tests test_defined_literal (10 passed)
  • cargo test --test codegen_tests closure_bind (22 passed)
  • focused trait-constant codegen/eval/reflection and conflict-diagnostic tests
  • focused closure call, existing late-static constant, diagnostics, and EIR smoke tests
  • optimizer-on/off parity for late-static and trait-imported probes
  • emitter coverage for macOS AArch64, iOS ARM64, iOS Simulator ARM64, Linux AArch64, and Linux x86_64 closure-bind descriptors
  • cargo build
  • generated builtin docs/parity/EIR-boundary audit workflow
  • assembly-comment and diff hygiene checks

Remaining limit

  • eval-mode class-constant names and dynamic/first-class-callable defined() names remain outside this AOT lookup

Fixes #752
Fixes #1005
Fixes #1006

Open in Web Open in Cursor 

Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
@nahime0
nahime0 marked this pull request as ready for review September 11, 2026 16:29
@github-actions github-actions Bot added area:codegen Touches target-aware assembly or backend lowering. area:eir Touches EIR definitions, lowering, validation, or passes. area:types Touches type checking, inference, or compatibility. scope:multi-area Touches more compiler areas than the automatic area-label cap. size:m Medium-sized pull request. type:fix Corrects broken or incompatible behavior. labels Sep 11, 2026
@greptile-apps

greptile-apps Bot commented Sep 11, 2026

Copy link
Copy Markdown

Greptile Summary

This PR extends literal defined() handling to class-like constants and adds the metadata, late-static dispatch, closure capture preservation, trait composition, diagnostics, documentation, and regression coverage needed to support it.

  • Resolves class, interface, enum, inherited, and trait-imported constants with PHP-compatible casing and visibility.
  • Dispatches static:: probes using the runtime called class and preserves that state through closure binding.
  • Detects incompatible trait-constant compositions and compares simple folded constant values.
  • Marks invalid relative-scope probes as throwing and documents the supported AOT behavior.

Confidence Score: 4/5

The PR is not yet safe to merge because equivalent trait constants can still be rejected when equality requires resolving a named or scoped constant rather than folding literal arithmetic.

The previous equivalent-constant finding is only partially fixed: constants_compatible now normalizes expressions such as 3 + 4 and 7, but standalone folding leaves named constants and class/scoped constant accesses unresolved, so declarations whose evaluated PHP values are equal can remain structurally different and still trigger the incompatible-composition diagnostic.

Important Files Changed

Filename Overview
src/types/defined.rs Implements scope-aware class-like constant existence checks, relative-scope validation, inheritance, interfaces, enum cases, and PHP casing rules.
src/ir_lower/expr/scoped_values.rs Adds runtime called-class dispatch for literal defined('static::CONST') probes.
src/ir_lower/expr/closures.rs Captures the called-class identifier in class-scope closures so late-static behavior survives method return.
src/codegen_support/runtime/callables/closure_bind.rs Extends closure binding to validate and copy descriptors containing the hidden called-class capture across all supported targets.
src/types/traits/merge.rs Composes trait constants and compares duplicate values after standalone folding, but the previous equivalent-value finding remains only partly fixed.
tests/codegen/oop/constants.rs Adds broad regression coverage for class-like defined() behavior, visibility, late-static dispatch, traits, enums, and invalid scopes.

Reviews (9): Last reviewed commit: "fix: compare folded trait constant value..." | Re-trigger Greptile

Comment thread src/types/defined.rs
Comment thread src/types/defined.rs
Comment thread src/types/defined.rs
cursoragent and others added 2 commits September 14, 2026 11:07
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
@github-actions github-actions Bot added area:builtins Touches PHP builtin declarations or emitters. and removed area:eir Touches EIR definitions, lowering, validation, or passes. labels Sep 14, 2026
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
@cursor

cursor Bot commented Sep 14, 2026

Copy link
Copy Markdown

Greptile P1 disposition after PHP 8.5 and compiler-path verification:

  • P1a — DEFER-with-follow-up: valid late-static gap; runtime called-class work is #1005.
  • P1b — VALID / fixed: invalid relative scopes now throw catchable Error; defined() is marked MAY_THROW so exception optimization preserves the handler.
  • P1c — DEFER-with-follow-up: valid trait-composition gap; shared AOT trait-constant work is #1006.

Current HEAD: 000c2248e4bad2db47785e2561973bc92f0491c4. Focused class-constant, diagnostics, EIR smoke, PHP cross-check, build, generated builtin-docs, and hygiene checks pass.

Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
@github-actions github-actions Bot added area:eir Touches EIR definitions, lowering, validation, or passes. and removed area:builtins Touches PHP builtin declarations or emitters. labels Sep 14, 2026
cursoragent and others added 2 commits September 14, 2026 12:14
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
Comment thread src/ir_lower/program/metadata.rs Outdated
cursoragent and others added 2 commits September 14, 2026 12:28
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
Comment thread src/codegen_support/runtime/callables/closure_bind.rs
cursoragent and others added 3 commits September 14, 2026 12:50
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
Comment thread src/types/traits/merge.rs Outdated
cursoragent and others added 2 commits September 14, 2026 12:55
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
Co-authored-by: Vincenzo Petrucci <nahime0@users.noreply.github.com>
@nahime0

nahime0 commented Sep 14, 2026

Copy link
Copy Markdown
Member Author

Follow-up for the residual Greptile 4/5 (named/scoped trait const equality before compare): #1007

@nahime0
nahime0 merged commit 5ed3bf8 into main Sep 14, 2026
149 checks passed
@nahime0
nahime0 deleted the cursor/fix-defined-class-constant-752-ce30 branch September 14, 2026 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:codegen Touches target-aware assembly or backend lowering. area:eir Touches EIR definitions, lowering, validation, or passes. area:types Touches type checking, inference, or compatibility. scope:multi-area Touches more compiler areas than the automatic area-label cap. size:m Medium-sized pull request. type:fix Corrects broken or incompatible behavior.

Projects

None yet

2 participants