Skip to content

Union string accessories and arrays independently of member order - #7176

Closed
ondrejmirtes wants to merge 2 commits into
2.3.xfrom
empty-string-union-structural
Closed

ondrejmirtes wants to merge 2 commits into
2.3.xfrom
empty-string-union-structural

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

TypeCombinator::union() decided two cases by comparing describe(VerbosityLevel::value()) strings. Both now look at the types themselves.

'' absorbed by a non-empty string. The empty string merged into the other member only when that member described as exactly non-empty-string or non-falsy-string, and the result was then rebuilt from its case accessories alone. Now the non-empty-string/non-falsy-string accessory is removed from the intersection, and the rest is used whenever it accepts '':

Union Before After
'' | non-empty-literal-string ''|(literal-string&non-empty-string) literal-string
'' | (literal-string&lowercase-string&non-falsy-string) ''|(literal-string&lowercase-string&non-falsy-string) literal-string&lowercase-string
'' | (non-empty-string&numeric-string) ''|numeric-string unchanged

Benevolent membership. A union stayed benevolent when every member had the same description as a member of a benevolent union. uppercase-string describes as string, like lowercase-string, so (int|lowercase-string) | uppercase-string stayed benevolent while (int|lowercase-string) | non-empty-string did not. Membership is now identity, or absorbing a benevolent member (unchanged).

The C++ mirror in turbo-ext/src/TypeCombinator.cpp gets the same change. Test: nsrt/union-string-accessories-structural.php, which fails without the change.


Second commit: union no longer depends on member order or grouping. On a pool of 46 types, 32 of 97,336 triples gave semantically different results depending on bracketing; now 12 do ((a ∪ b) ∪ c vs a ∪ (b ∪ c)).

  • non-empty-X | Y is X when Y ⊆ X holds the strings non-empty-X lacks ('', and '0' for non-falsy). For example, non-empty-string | lowercase-string is string. Before, only '' | non-empty-X did this.
  • non-falsy-X | Y is non-empty-X when '0' ∈ Y ⊆ non-empty-X. For example, non-falsy-string | numeric-string is non-empty-string.
  • A member that is new to the union is compared again with the earlier members: callable-string|non-empty-string|lowercase-string is now string, where it was callable-string|string.
  • Arrays held by another member are dropped before processArrayTypes() merges the arrays. array{a: string}|array<int>|iterable<int> is now array{a: string}|iterable<int>, where it was iterable<(int|string), int|string>|iterable<int>.

The 12 triples that still differ merge two arrays before a third member holds one of them, which processArrayTypes() does on purpose. Expectations in TypeCombinatorTest and bug-12312 got more precise: after if ($s != '0'), $s is back to its declared lowercase-string&non-empty-string. The new test nsrt/union-string-accessories-complete.php fails without the change, including the rescan case. A parity dump of 25,230 unions is identical with turbo active.

TypeCombinator::union() decided two cases from describe(VerbosityLevel::value())
strings:

- '' absorbed a non-empty string only when the other member described as
  exactly non-empty-string or non-falsy-string, and then rebuilt the result
  from its case accessories alone. '' | non-empty-literal-string stayed
  ''|(literal-string&non-empty-string). Now the non-empty/non-falsy accessory
  is removed from the intersection and the rest is kept whenever it accepts
  '' (literal-string, lowercase-string, ...); ''|numeric-string stays as is.
- A union stayed benevolent when every member described like a member of a
  benevolent union, so (int|lowercase-string) | uppercase-string stayed
  benevolent because uppercase-string also describes as "string". Membership
  is now identity, or absorbing a benevolent member as before.

turbo-ext: the same in TypeCombinator.cpp.

Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
…d by another member first

TypeCombinator::union() gave different results for the same members in a
different order or grouping (32 of 97,336 triples of 46 types differed
semantically, now 12):

- non-empty-X | Y is X when Y is within X and holds the strings
  non-empty-X lacks: '' and, for non-falsy-X, '0'. non-empty-string |
  lowercase-string is string, non-empty-literal-string |
  (literal-string&lowercase-string) is literal-string. Before, only
  '' | non-empty-X did this.
- non-falsy-X | Y is non-empty-X when '0' ∈ Y ⊆ non-empty-X:
  non-falsy-string | numeric-string is non-empty-string. Before, only
  '0' | non-falsy-X did this.
- A member new to the union, like string from non-empty-string |
  lowercase-string, is compared again with the members it was not
  compared with: callable-string|non-empty-string|lowercase-string is
  string, no longer callable-string|string.
- Arrays another member holds, like array<int> next to iterable<int>, are
  dropped before processArrayTypes() merges the arrays into a type that
  member no longer holds: array{a: string}|array<int>|iterable<int> is
  array{a: string}|iterable<int>, no longer
  iterable<(int|string), int|string>|iterable<int>.

The remaining 12 triples merge two arrays before a third member holds
one of them, which processArrayTypes() does on purpose.

- TypeCombinatorTest, bug-12312: the unions above are now string,
  lowercase-string&non-empty-string, ...
- union-string-accessories-structural: (int|lowercase-string) |
  non-empty-string is (int|string), as string absorbs the benevolent
  lowercase-string; numeric-string is the strict control now.
- turbo-ext: the same in TypeCombinator.cpp.

Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
@ondrejmirtes ondrejmirtes changed the title Union '' and benevolent members by type structure, not by description Union string accessories and arrays independently of member order Nov 25, 2026
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