ComposeCompiler: Handle KtLabeledExpression when finding lambdas

Fixes: b/222979253
Change-Id: I8215d0e7c00f3f91ccf678503c9549d41cf35997
diff --git a/compose/compiler/compiler-hosted/integration-tests/src/test/java/androidx/compose/compiler/plugins/kotlin/SanityCheckCodegenTests.kt b/compose/compiler/compiler-hosted/integration-tests/src/test/java/androidx/compose/compiler/plugins/kotlin/SanityCheckCodegenTests.kt
index 237d15f..38c974d 100644
--- a/compose/compiler/compiler-hosted/integration-tests/src/test/java/androidx/compose/compiler/plugins/kotlin/SanityCheckCodegenTests.kt
+++ b/compose/compiler/compiler-hosted/integration-tests/src/test/java/androidx/compose/compiler/plugins/kotlin/SanityCheckCodegenTests.kt
@@ -57,4 +57,21 @@
         """
         )
     }
+
+    // Regression test for b/222979253
+    fun testLabeledLambda() = ensureSetup {
+        testCompile(
+            """
+                import androidx.compose.runtime.Composable
+
+                @Composable
+                fun test(): Unit {
+                    Box box@{}
+                }
+
+                @Composable
+                fun Box(content: @Composable () -> Unit) {}
+        """
+        )
+    }
 }
diff --git a/compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposableTargetChecker.kt b/compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposableTargetChecker.kt
index 34b23fb..3973744 100644
--- a/compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposableTargetChecker.kt
+++ b/compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposableTargetChecker.kt
@@ -50,6 +50,7 @@
 import org.jetbrains.kotlin.psi.KtFile
 import org.jetbrains.kotlin.psi.KtFunction
 import org.jetbrains.kotlin.psi.KtFunctionLiteral
+import org.jetbrains.kotlin.psi.KtLabeledExpression
 import org.jetbrains.kotlin.psi.KtLambdaArgument
 import org.jetbrains.kotlin.psi.KtLambdaExpression
 import org.jetbrains.kotlin.psi.KtProperty
@@ -349,9 +350,18 @@
         return PsiElementNode(element, bindingContext)
     }
 
-    private fun lambdaOrNull(element: PsiElement): KtFunctionLiteral? =
-        (element as? KtLambdaArgument)?.children?.firstOrNull()?.children
-            ?.firstOrNull() as KtFunctionLiteral?
+    private fun lambdaOrNull(element: PsiElement): KtFunctionLiteral? {
+        var container = (element as? KtLambdaArgument)?.children?.singleOrNull()
+        while (true) {
+            container = when (container) {
+                null -> return null
+                is KtLabeledExpression -> container.lastChild
+                is KtFunctionLiteral -> return container
+                is KtLambdaExpression -> container.children.single()
+                else -> throw Error("Unknown type: ${container.javaClass}")
+            }
+        }
+    }
 
     private fun descriptorToInferenceNode(
         descriptor: CallableDescriptor,