Skip to content

Accept merged shapes in unions and bound accepts reasons - #7210

Merged
ondrejmirtes merged 3 commits into
2.3.xfrom
accepts-merged-shapes
Nov 27, 2026
Merged

ondrejmirtes merged 3 commits into
2.3.xfrom
accepts-merged-shapes

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Since 0e9fdb5, union() merges shapes that differ at one key into one shape: $b ? ['type' => 'boolean'] : ['type' => 'keyword'] becomes array{type: 'boolean'|'keyword'}. No member of array{type: 'boolean'}|array{type: 'keyword', fields?: …} accepts that shape alone, so it is reported as return.type. In a downstream project, one such error on a recursive mapping type carried a 2.7 MB tip, and formatting that tip took the job from about 1.5 to 13 minutes.

  • Accept a merged shape when a union accepts each of the shapes it holds. UnionType::accepts() splits the shape at a key holding a union of constant scalars, using ArrayAlgebra::splitConstantArrayAtFiniteValue() with a cap of 64 pieces.
  • Keep at most AcceptsResult::REASONS_LIMIT (10) reasons. The reasons of nested unions multiplied at every level, reaching 829 on that error.
  • Escape non-style tags before formatting messages and tips. Symfony's OutputFormatter is quadratic on every <…> in a type description. With them escaped, the 2.7 MB tip formats in 0.17 s instead of about 15 minutes, and the output is the same.

The turbo mirrors are ported in AcceptsResult.cpp and UnionType.cpp. The smoke test, signature parity and side-by-side checks pass, and the suites pass with the extension loaded.

Amp thread: https://ampcode.com/threads/T-01a21c50-6b52-77f7-9d04-dff937be67fa

Every member of a union that rejects a type adds its own reasons, and
ConstantArrayType::checkOurKeys() prefixes each one with the full value
types, so the reasons of nested unions of shapes multiply with every level.
A recursive mapping type produced 829 reasons and a 2.7 MB tip on a single
return.type error. The constructor now keeps only the first ten.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c50-6b52-77f7-9d04-dff937be67fa
Symfony's OutputFormatter reads every <...> in a type description such as
array<int, string> as a possible style tag and measures the text up to it
from the start of the message, which is quadratic in the message length:
a 2.7 MB tip took about 11 minutes to format. ConsoleTagEscaper escapes
what is not a style tag before the table and JSON formatters format it, so
the escaped tags are skipped. The output stays the same.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c50-6b52-77f7-9d04-dff937be67fa
union() merges shapes that differ at one key into one shape, such as
array{type: 'boolean'|'keyword'}, which no member of
array{type: 'boolean'}|array{type: 'keyword', fields?: ...} accepts alone.
UnionType::accepts() now splits such a shape at a key holding a union of
constant scalars and accepts it when it accepts every piece.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c50-6b52-77f7-9d04-dff937be67fa
@ondrejmirtes
ondrejmirtes merged commit 47676d6 into 2.3.x Nov 27, 2026
421 of 424 checks passed
@ondrejmirtes
ondrejmirtes deleted the accepts-merged-shapes branch November 27, 2026 10:58
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