Skip to content

fix(enum): report a circular reference of an enum case value instead of crashing - #14350

Merged
orklah merged 3 commits into
vimeo:masterfrom
jirkace:fix/14299-enum-case-const-circular
Nov 22, 2026
Merged

orklah merged 3 commits into
vimeo:masterfrom
jirkace:fix/14299-enum-case-const-circular

Conversation

@jirkace

@jirkace jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Psalm crashes with an uncaught CircularReferenceException when an enum case value and a class constant refer to each other and the case value is read.

Before

enum Level: int {
    case Low = Limits::MIN + 1;
}

final class Limits {
    public const MIN = Level::Low->value;
}

function read(): int {
    return Level::Low->value; // Uncaught CircularReferenceException: Found a circular reference
}

After

    case Low = Limits::MIN + 1; // CircularReference: Enum case Level::Low contains a circular reference

Fixes #14299

…of crashing vimeo#14299

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 751 71.3 $\color{gray}{\textsf{+0.3\%}}$ 25 908 53 462 682
✅ symfony 3 600 193.1 $\color{gray}{\textsf{-1.2\%}}$ 50 611 98 405 1 528
✅ doctrine-orm 381 15.9 $\color{gray}{\textsf{-0.2\%}}$ 3 481 6 143 0
✅ psl 617 19.9 $\color{gray}{\textsf{-0.7\%}}$ 1 375 5 615 1
✅ phpunit 455 13.6 $\color{gray}{\textsf{+2.1\%}}$ 1 731 3 410 18
✅ php-parser 226 9.0 $\color{gray}{\textsf{+1.7\%}}$ 17 132 21 025 0
✅ tempest 895 25.3 $\color{gray}{\textsf{-0.3\%}}$ 5 047 10 528 252
✅ amphp-amp 68 2.0 $\color{gray}{\textsf{-0.5\%}}$ 29 49 0
✅ webmozarts-assert 273 4.9 $\color{gray}{\textsf{-0.2\%}}$ 577 2 720 0
✅ guzzle-promises 44 1.6 155 434 2
✅ monolog 135 6.1 $\color{gray}{\textsf{+0.3\%}}$ 913 2 479 10
✅ drupal 4 841 213.5 $\color{gray}{\textsf{-0.1\%}}$ 97 591 169 293 583
Total 13 286 576.1 $\color{gray}{\textsf{-0.4\%}}$ 204 550 373 563 3 076
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +20 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Thresholds apply per project, not to the total.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.

head d8306ff · base 3a3fdf6 (merge-base) · workflow run

@jirkace

jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • Reading an enum case value that refers back to the case through a class constant no longer crashes; the enum case is reported once as CircularReference and its value is unknown (mixed) for the readers, so a function returning it can get MixedReturnStatement (the test lists it in error_levels).
  • EnumCaseStorage::getValue() now returns null for a circular value instead of throwing; resolveValue() is the throwing variant, used by ClassAnalyzer::checkEnum().
  • Not run: the full suite (CI); benchmark corpora.

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The CircularReference report works for the case itself, the class-constant side, a self-cycle
between two cases, and readers such as match, tryFrom(), from() and ->value.

But getValue() now returns null for a circular case, and two of its callers assert that a backed case
always has a value, so this still crashes:

enum Level: int {
    case Low = Limits::MIN + 1;
}

final class Limits {
    public const MIN = Level::Low->value;
}

/** @param value-of<Level> $v */
function takesValue(int $v): void {}
// Uncaught AssertionError: assert($case_value !== null) in src/Psalm/Type/Atomic/TValueOf.php:52

The same asserts are in TValueOf::getValueTypeForNamedObject() (both branches) and in
SimpleAssertionReconciler (around lines 3104 and 3120). They already crash on master when a case value is an
undefined constant (case A = NOPE + 1; with value-of<E>), so handling null there (falling back to
mixed, as AtomicPropertyFetchAnalyzer::handleEnumValue() does) fixes both. Could you do that and add a
test case with value-of<Level>?

jirkace and others added 2 commits November 21, 2026 17:31
…ed for that case

getValue() is null for a case in a circular reference or of an undefined constant: TValueOf and the
value-of reconciliation take mixed for it instead of asserting a value, which crashed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Pushed d8306ff (master merged in). TValueOf::getValueTypeForNamedObject() (both branches) and the two places of SimpleAssertionReconciler take mixed for a case whose value is null instead of asserting one, as handleEnumValue() does. valueOfAnEnumWithACaseInACircularReferenceIsMixed is your Level/Limits snippet with value-of<Level> (it crashed with a TypeError/assert before and now reports the CircularReference), and valueOfAnEnumWithACaseOfAnUndefinedConstantIsMixed is the case A = NOPE + 1; one with value-of<E> and value-of<E::A>. A full self-analysis is clean. @orklah

@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 22, 2026

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jirkace!

@orklah
orklah merged commit 9927646 into vimeo:master Nov 22, 2026
71 of 72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on a circular reference between an enum case value and a class constant

2 participants