Skip to content

Commit

Permalink
Fix crash, add reduced repro (#1163)
Browse files Browse the repository at this point in the history
## Proposed Changes

Add reduced reported issue repro, fix logical error in `SortedSet.add`.

## Testing

Test: `SortedSetTest.checkExistingWhenAdded`, no crash happens in
reported scenario.

## Issues Fixed

Fixes: JetBrains/compose-multiplatform#4422
  • Loading branch information
elijah-semyonov committed Mar 5, 2024
1 parent 6a3c503 commit 0456a30
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compose/mpp/demo/src/uikitMain/kotlin/bugs/IosBugs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ val IosBugs = Screen.Selection(
DropdownMenuIssue,
KeyboardIMEActionPopup,
ProperContainmentDisposal,
ComposeAndNativeScroll
ComposeAndNativeScroll,
MeasureAndLayoutCrash
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bugs

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Checkbox
import androidx.compose.material.Text
import androidx.compose.mpp.demo.Screen
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay

@Composable
fun RowItem(index: Int) {
Row(modifier = Modifier.padding(end = 30.dp)) {
var state by remember(index) { mutableStateOf<Unit?>(null) }

LaunchedEffect(index) {
delay((100..200).random().toLong())
state = Unit
}
state?.let {
Text("Item $index")
}
}
}

@Composable
fun RowItem(wrapInBox: Boolean, index: Int) {
if (wrapInBox) {
Box {
RowItem(index)
}
} else {
RowItem(index)
}
}

val MeasureAndLayoutCrash = Screen.Example("MeasureAndLayoutCrash") {
val lazyColumnState = rememberLazyListState()
var enableCrash by remember { mutableStateOf(false) }

Column(
modifier = Modifier.safeContentPadding()
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(
checked = enableCrash,
onCheckedChange = { enableCrash = it },
modifier = Modifier.padding(16.dp),
)

Text("Enable crash")
}
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.TopCenter) {
LazyColumn(
verticalArrangement = Arrangement.spacedBy(8.dp),
state = lazyColumnState,
) {
items(1000) { index ->
RowItem(enableCrash, index)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal actual class SortedSet<E> actual constructor(
var index = list.binarySearch(element, comparator)
if (index < 0) {
index = -index - 1
} else {
return false
}
list.add(index, element)
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ class SortedSetTest {
assertOrderEquals(numbers.sorted(), set)
}

@Test
fun checkExistingWhenAdded() {
val set = sortedSetOf(1, 2, 3, 4, 5)
assertFalse(set.add(3))
assertFalse(set.add(4))
assertFalse(set.add(5))
assertTrue(set.add(6))
assertTrue(set.add(7))
}

@Test
fun customComparator() {
val set = sortedSetOf(compareBy { it.length }, "B", "AAA", "DD")
Expand Down

0 comments on commit 0456a30

Please sign in to comment.