From 7fbb8cf9e5fc1b54d266e1c3229ca7588f4e8697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 10 Apr 2026 09:36:48 +0200 Subject: [PATCH] fix(spaces): Ensure space children ordering is transitive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch fixes the SpaceRoom::compare_rooms method to be transitive (If A ≤ B and B ≤ C then A ≤ C). The transitive property of a comparison function is required by the sorting functions we are using. If the property doesn't hold, sorting will panic. The previous logic violated strict weak ordering by falling back towards room ID comparison as soon as one of the values doesn't have a SpaceRoomChildState. This introduced inconsistent ordering paths where: - A and B would be compared using the SpaceRoomChildState - B and C would be compared using only the room ID - A and C would be compared using only the room ID Leading to the case where: - A < B due to the state - B < C due to the room ID - and finally A > C due to the room ID. As a result, transitivity could be broken (A < B, B < C, but C < A), leading to a panic during the sorting. --- crates/matrix-sdk-ui/src/spaces/room.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk-ui/src/spaces/room.rs b/crates/matrix-sdk-ui/src/spaces/room.rs index 14c828fa8..70826ac99 100644 --- a/crates/matrix-sdk-ui/src/spaces/room.rs +++ b/crates/matrix-sdk-ui/src/spaces/room.rs @@ -168,7 +168,9 @@ impl SpaceRoom { .cmp(&b_state.origin_server_ts) .then(a_room_id.cmp(b_room_id)), }, - _ => a_room_id.cmp(b_room_id), + (None, Some(_)) => Ordering::Greater, + (Some(_), None) => Ordering::Less, + (None, None) => a_room_id.cmp(b_room_id), } } }