Skip to content

feat: two Sets are == when they hold the same elements, and an event or a case takes a Set - #165

Merged
heyJordanParker merged 9 commits into
masterfrom
feat/set-equality
Oct 18, 2026
Merged

heyJordanParker merged 9 commits into
masterfrom
feat/set-equality

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

== on two Set<int> values said "== on a collection is not supported yet", although spec section 19 says "A struct or a collection gets == with no code when every field or element has ==." List and Map already compared. The gap spread: a List<Set<int>> could not compare or search, a struct with a Set field lost ==, and an event's Set parameter and a case's Set field were refused. v1's definition of done leaves no "not supported yet".

What a PHP# developer can now write

  • Two sets are == when they hold the same elements, in any order, as in Kotlin, Java, Swift and Python. !=, a nullable set, a List<Set<int>>, a Map<string, Set<int>>, a Set<Set<int>>, a Set<Set<int|string>> and a struct with a Set field all compare.
  • A List of sets searches by ==: contains, indexOf and remove find an equal set in any order.
  • Equal sets hash alike whatever their order, so a Set keys a Map and joins a Set, and toSet() of a list of equal sets holds one.
  • A list literal is a Set wherever a Set is expected, at any depth: Set<Set<int>> groups = [[1, 2], [2, 1]], Report.rows([[1, 2]], …), new Rows(1, [[1, 2]]), rows.contains([2, 1]), and a side of ==, so tags == [2, 1] is true and tags == [] compares with the empty Set.
  • At ==, [] is an empty List and [:] an empty Map, as section 12 says, so map == [], list == [:] and tags == [:] are refused as at a place: "[] is an empty List. An empty Map is written [:]."
  • A union that holds a collection beside another type compares by the value it holds, as Set<int>|int == Set<int>, int|[int, string] == 1, a search of a List<Set<int>|int> and a struct with a Set<int>|int field. Where no two of its types meet, the sides are of different types.
  • An event takes a Set parameter and a case takes a Set field, nested in a List too, and so does a struct whose only collection is nested. Plain PHP's arrays become Sets, and plain PHP's elements are checked for a case as for an event. A Map parameter or field takes plain PHP's array the same way.

How

  • analyzer/.../comparison.rs: collection_parts takes a Set. collection_refusal refuses two collection kinds, a collection beside a value of another type, and an Iterable, and hands a union to union_refusal, which compares each pair of its types. type_collection_literal gives a literal side the other side's collection type, through check_sharp_literal_kind, get_empty_literal_type and get_set_literal_type. The remaining refusal names the Iterable and says that == compares a List, a Map or a Set.
  • analyzer/.../array.rs: get_set_literal_type applies a Set's declared element type inside the literal. analyzer/.../invocation/analyzer.rs types a literal argument against its parameter, so verify_argument_type no longer widens a PHP# literal's Set parameter.
  • analyzer/src/assertion.rs: two collections compared by value narrow nothing.
  • semantics/.../sharp.rs: check_property_parameter no longer refuses a Set, and is_key_hint takes a union of key types.
  • sharp-bridge/src/lower.rs: an event's, a case's and a struct's constructor declares each property beside its parameter when a parameter holds a Map or a Set at any depth, and a case's constructor reads its checks from the case's class. == on a union that holds a collection runs \Sharp\Internal\equals.
  • docs/spec.md: checked examples set-equality and union-equality (section 19), event-set-parameter (section 15) and case-set-field (section 20).

Tests

set_values.phpt and set_values_preload.phpt run the same rows without and with preloading, in every engine mode: equal sets in any order, nullable sets, sets in a List, a Map, a Set and a struct, searches, hashing, an event and a case with a Set built in PHP# and in plain PHP, a nested List<Set<int>> in an event, a case and a struct built in plain PHP, plain PHP's wrong elements refused for an event and a case, list literals at any depth, a Set<Set<int|string>>, and unions that hold a collection. Mago's tests: sets_compare_by_their_elements_in_any_order, a_list_literal_is_a_set_wherever_a_set_is_expected_at_any_depth, an_empty_literal_at_a_comparison_is_the_collection_the_spec_names, equality_of_two_collections_narrows_nothing, a_union_that_holds_a_collection_compares_as_each_of_its_types_does, equality_of_an_iterable_is_not_supported_yet, a_set_element_takes_what_a_map_key_takes, and the bridge's an_event_a_case_and_a_struct_make_each_nested_set_a_set_before_they_set_the_property and equality_of_a_union_that_holds_a_collection_compares_by_what_it_holds.

Not in this PR, after v1

  • sharp/bin/spec's class map leaves out the case classes an enum's file declares, so a run that builds a data case throws Class "App\Badge\Marked" not found. case-set-field claims compiles for that reason, and shows no result.
  • A plain PHP caller can't pass an array to a PHP# parameter typed as a union that holds a Set or a Map: Report::unions([1, 2], …) for Set<int>|int either throws "must be of type Sharp\Set|int, array given".
  • const e = [] and const e = [:] both get PHP's empty-array type, so == against such a local can't tell an empty List from an empty Map.
  • == on an Iterable, or on a type parameter bound by a collection, is still not supported yet.

…or a case takes a Set

Problem: `==` on two `Set<int>` values said "`==` on a collection is not
supported yet", although spec section 19 gives every collection `==`
when its elements have it. The gap spread: a `List<Set<int>>` could not
compare or search with `contains`, `indexOf` and `remove`, a struct
with a `Set` field had no `==`, and an event's `Set` parameter and a
case's `Set` field were refused, because their constructor promoted the
PHP array a list literal or plain PHP passes straight into the property.

Solution: the checker compares two sets by their elements, each with its
own `==`, as it compares two lists or two maps. A set never equals a
list or a map, an empty literal included, since that literal is a PHP
array. The engine's `==` and `hash()` on `Sharp\Set` already ignore
order, so the bridge's lowering runs unchanged. An event's or a case's
constructor whose parameter holds a `Map` or a `Set` now declares each
property beside a plain parameter and sets it once the object is made,
as a class's constructor does. Spec sections 15, 19 and 20 gain checked
examples.

docs/spec.md                                  <- set-equality, event-set-parameter, case-set-field
sharp/mago/crates/
├── analyzer/
│   ├── src/expression/binary/comparison.rs   <- collection_parts takes a Set, collection_refusal
│   └── tests/sharp.rs                        <- sets_compare_by_their_elements_in_any_order, equality_of_an_iterable_is_not_supported_yet
├── semantics/
│   ├── src/internal/checker/sharp.rs         <- check_property_parameter refuses no Set
│   └── tests/sharp.rs                        <- an event's and a case's Set rows
└── sharp-bridge/
    ├── src/lower.rs                          <- promoted_constructor
    └── tests/lower.rs                        <- an_event_or_a_case_makes_its_set_argument_a_set_before_it_sets_the_property
Zend/tests/sharp/
├── SetValues/                                <- Badge, Labels, Report, Tagged
├── set_values.inc                            <- the rows both tests run
├── set_values.phpt
├── set_values_preload.inc
└── set_values_preload.phpt
…ds a collection

- An event, a case and a struct make a Set nested in a List a Set before they set the property, so plain PHP builds them.
- A case's constructor reads its checks from the case's class, so plain PHP's elements are checked as an event's are.
- A list literal is a Set wherever a Set is expected, at any depth: a declaration, an argument and a side of ==.
- At ==, [] is the other side's List or Set and [:] its Map, and a literal of another kind is refused as at a place.
- Two collections compared with == narrow nothing, so `s == o || x` reports no impossible condition.
- Set<Set<int|string>> is accepted, since a union of key types keys a Map.
- == on a union that holds a collection beside another type compares by the value it holds, through \Sharp\Internal\equals.
- The refusals that remain name the Iterable, and say that == compares a List, a Map or a Set.
- A list literal beside a List stays a List, and one beside an int keeps its own type.
- A search of a List<Set<int>|int> compares each element through \Sharp\Internal\equals.
#163 gives plain PHP the array form of a PHP# Set or Map field, so the
rows that read the field's class from plain PHP threw or printed "array".
Each row now asks PHP# whether the field holds the object, and expects
the array form where plain PHP reads it.
#159 declares an event's or a case's properties beside their parameters
when an attribute names a target. This branch does so when a parameter
holds a Map or a Set at any depth. promoted_constructor keeps both
reasons, as class_members does, and declares each property through
promoted_property. The semantics test keeps both sides: a Set event
parameter and an attribute on an event are both accepted.
@heyJordanParker
heyJordanParker merged commit 8743cc5 into master Oct 18, 2026
24 checks passed
heyJordanParker added a commit that referenced this pull request Oct 18, 2026
…d beside the permanent refusals

Problem

#159, #165, #168, #170 and #171 landed on master. #159 gives attributes
to interfaces, events, constants, accessors and lambdas, #168 gives
every pattern type a test, and the collections work builds tuple and
`Iterable` constants. #166's refusals of those forms as "not supported
yet" are now refusals of code that compiles. `==` on an `Iterable` was
still "not supported yet", and a `.sharp` file the engine refused stayed
in PHP's included files, so a second `require_once` skipped it and a
later use said "Class not found".

Solution

The merge keeps master's attribute arms, its `as` arm and its
attribute-argument refusal, keeps `report_accessor` with #159's
observer-attribute arm and its `ACCESSORS` note, and drops the arms
master made unreachable: `is_undecided`'s attribute lists, pattern
types with no test and tuple constants.

`==` on an `Iterable`, or on a type parameter bound by a collection,
is a permanent error that names `toList()`: "`==` compares a `List`, a
`Map` or a `Set`, and an `Iterable` is none of them: compare
`a.toList() == b.toList()`." A search and a struct property say the
same.

A `.sharp` file the engine refuses leaves PHP's included files, so a
second `require_once` reports the same refusal.

Spec section 20's `case-set-field` states its result, `// true`, which
the spec tool now runs.

php-sharp/
├── Zend/tests/sharp/refusal_required_again.phpt     a refused file is required again
├── docs/spec.md                                      case-set-field gives true
├── ext/sharp/sharp.c                                 sharp_compile_file drops a refused file
└── sharp/mago/crates/
    ├── analyzer/
    │   ├── src/expression/binary/comparison.rs       `==` on an Iterable names toList()
    │   └── tests/sharp.rs                            its rows, and a row that compiles toList()
    ├── hir/src/lower/error/mod.rs                    both sides' error list
    └── semantics/
        ├── src/internal/checker/sharp.rs             is_undecided, report_accessor, the as arm
        └── tests/sharp.rs                            rows for what master builds
@heyJordanParker
heyJordanParker deleted the feat/set-equality branch November 3, 2026 22:08
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