diff --git a/arrow-libs/optics/arrow-optics-ksp-plugin/src/main/kotlin/arrow/optics/plugin/OpticsProcessor.kt b/arrow-libs/optics/arrow-optics-ksp-plugin/src/main/kotlin/arrow/optics/plugin/OpticsProcessor.kt index cf72783982d..ff66299e188 100644 --- a/arrow-libs/optics/arrow-optics-ksp-plugin/src/main/kotlin/arrow/optics/plugin/OpticsProcessor.kt +++ b/arrow-libs/optics/arrow-optics-ksp-plugin/src/main/kotlin/arrow/optics/plugin/OpticsProcessor.kt @@ -14,7 +14,9 @@ import com.google.devtools.ksp.processing.KSPLogger import com.google.devtools.ksp.processing.Resolver import com.google.devtools.ksp.processing.SymbolProcessor import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSAnnotation import com.google.devtools.ksp.symbol.KSClassDeclaration +import com.google.devtools.ksp.symbol.KSFunctionDeclaration import com.google.devtools.ksp.validate class OpticsProcessor( @@ -27,7 +29,15 @@ class OpticsProcessor( val (resolved, deferred) = resolver .getSymbolsWithAnnotation("arrow.optics.optics") .filterIsInstance() - .partition { it.validate() } + .partition { + it.validate { _, element -> + when (element) { + is KSAnnotation -> false + is KSFunctionDeclaration -> false + else -> true + } + } + } resolved.forEach(::processClass) // If types used by the annotated class are by other processors, diff --git a/arrow-libs/optics/arrow-optics-ksp-plugin/src/test/kotlin/arrow/optics/plugin/LensTests.kt b/arrow-libs/optics/arrow-optics-ksp-plugin/src/test/kotlin/arrow/optics/plugin/LensTests.kt index 6050901eda3..b22e0116c32 100755 --- a/arrow-libs/optics/arrow-optics-ksp-plugin/src/test/kotlin/arrow/optics/plugin/LensTests.kt +++ b/arrow-libs/optics/arrow-optics-ksp-plugin/src/test/kotlin/arrow/optics/plugin/LensTests.kt @@ -59,6 +59,34 @@ class LensTests { """.evals("r" to true) } + @Test + fun `Lenses are generated for data class referencing its own lenses for type inference`() { + """ + |$`package` + |$imports + |@optics + |data class UsingLens(val field: String) { + | fun getLens() = UsingLens.field + | companion object + |} + | + |val i: Lens = UsingLens.field + |val r = i != null + """.evals("r" to true) + } + + @Test + fun `Lenses are not generated for unresolved types`() { + """ + |$`package` + |$imports + |@optics + |data class InvalidType(val field: Foo) { + | companion object + |} + """.compilationFails() + } + @Test fun `Lenses which mentions imported elements`() { """