Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import arrow.optics.plugin.companionObject
import com.google.devtools.ksp.getDeclaredProperties
import com.google.devtools.ksp.getVisibility
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeParameter
import com.google.devtools.ksp.symbol.Variance
import com.google.devtools.ksp.symbol.Visibility
import java.util.*

data class ADT(val declaration: KSClassDeclaration, val targets: List<Target>) {
val sourceClassName = declaration.qualifiedNameOrSimpleName
val sourceName = declaration.simpleName.asString().replaceFirstChar { it.lowercase(Locale.getDefault()) }.sanitize()
val simpleName = declaration.nameWithParentClass
val packageName = declaration.packageName.asSanitizedString()
val visibilityModifierName =
(declaration.companionObject?.getVisibility() ?: declaration.getVisibility()).name.lowercase()
val visibilityModifierName = declaration.effectiveCompanionVisibility.name.lowercase()
val typeParameters: List<String> = declaration.typeParameters.map { tyParam ->
if (tyParam.variance == Variance.STAR) return@map "*"
// val prefix = when (it.variance) {
Expand Down Expand Up @@ -52,6 +53,36 @@ val KSClassDeclaration.nameWithParentClass: String
else -> simpleName.asString()
}

val KSClassDeclaration.effectiveCompanionVisibility: Visibility
get() {
val visibilities =
listOfNotNull(companionObject?.getVisibility(), getVisibility()) +
allParentDeclarations().filterIsInstance<KSClassDeclaration>().map { it.getVisibility() }
return visibilities.foldRight(Visibility.PUBLIC, Visibility::plus)
}

fun KSDeclaration.allParentDeclarations(): List<KSDeclaration> = when (val parent = parentDeclaration) {
null -> emptyList()
else -> listOfNotNull(parent) + parent.allParentDeclarations()
}

operator fun Visibility.plus(other: Visibility): Visibility = when {
this == other -> this

this == Visibility.PUBLIC -> other

other == Visibility.PUBLIC -> this

this == Visibility.LOCAL || other == Visibility.LOCAL -> Visibility.LOCAL

this == Visibility.PRIVATE || other == Visibility.PRIVATE -> Visibility.PRIVATE

(this == Visibility.INTERNAL || this == Visibility.PROTECTED) &&
(other == Visibility.INTERNAL || other == Visibility.PROTECTED) -> Visibility.PRIVATE

else -> Visibility.PRIVATE
}

enum class OpticsTarget {
ISO,
LENS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun buildCompilation(
this.sources = sources.toList()
this.verbose = false
this.allWarningsAsErrors = allWarningsAsErrors
this.languageVersion = "2.0"
this.languageVersion = "2.1"
if (contextParameters) {
this.kotlincArguments = listOf("-Xcontext-parameters")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DSLTests {
|$dslModel
|$dslValues
|val modify = Employees.employees.every.company.notNull.address
| .street.name.modify(employees, String::toUpperCase)
| .street.name.modify(employees, String::uppercase)
|val r = modify.employees.map { it.company?.address?.street?.name }.toString()
""".evals("r" to "[LAMBDA STREET, LAMBDA STREET]")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,23 @@ class LensTests {
|val r = lens != null
""".evals("r" to true)
}

@Test
fun `Visibilities are correctly computed (#3869)`() {
"""
|$`package`
|$imports
|@optics
|internal sealed interface Interface {
| @optics
| data class DataClass(val value: Int) : Interface {
| companion object
| }
| companion object
|}
|
|internal val lens: Lens<Interface.DataClass, Int> = Interface.DataClass.value
|internal val r = lens != null
""".evals("r" to true)
}
}
8 changes: 4 additions & 4 deletions generator-scripts/tuple.generator.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ for (i in 2..maxTuple) {

val letters = availableLetters.take(i + 1)

val diamond1 = "<${letters.joinToString { it.toUpperCase() }}>"
val diamond2 = "<${letters.dropLast(1).joinToString { it.toUpperCase() }}>"
val diamond1 = "<${letters.joinToString { it.uppercase() }}>"
val diamond2 = "<${letters.dropLast(1).joinToString { it.uppercase() }}>"

val newLetter = availableLetters[i]

val constructor = (letters.dropLast(1).map { "this.$it" } + newLetter).joinToString()

fileContent.append(
"operator fun $diamond1 Tuple$i$diamond2.plus($newLetter: ${newLetter.toUpperCase()}): Tuple${i+1}$diamond1 = Tuple${i + 1}($constructor)"
"operator fun $diamond1 Tuple$i$diamond2.plus($newLetter: ${newLetter.uppercase()}): Tuple${i+1}$diamond1 = Tuple${i + 1}($constructor)"
)
fileContent.append("\n")

}

val fileLocation = "../modules/core/arrow-syntax/src/main/kotlin/arrow/syntax/collections/tuple.kt"

File("tuple.kt").writeText(fileContent.toString())
File("tuple.kt").writeText(fileContent.toString())
Loading