kopykat-kt / kopykat

Little utilities for more pleasant immutable data in Kotlin
Other
283 stars 16 forks source link

Issue with Nullables types when nesting kopycat types #73

Closed slawluc closed 2 years ago

slawluc commented 2 years ago

Given these gradle settings

buildscript {
    dependencies {
        .....
        classpath "com.google.devtools.ksp:symbol-processing-gradle-plugin:1.7.10-1.0.6"
    }
}

dependencies {
    .......
    ksp("at.kopyk:kopykat-ksp:1.0-rc8")
    compileOnly("at.kopyk:kopykat-annotations:1.0-rc8")
}

ksp {
    arg("generate","annotated")
    arg("mutableCopy", "true")
    arg("copyMap", "true")
    arg("hierarchyCopy", "true")
    arg("superCopy", "false")
}

When I attempt to annotate these class with @CopyExtensions and have one depend on the other via nullable type

@CopyExtensions data class Person(val name: String, val passport: Passport?)
@CopyExtensions data class Passport(val id: String, val countryCode: String)

Then I get the following compilation error

e: /.../build/generated/ksp/main/kotlin/.../com....Person$Mutable.kt: (24, 13): 
Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Passport?

The compilation error in generated code is on the fun Person.toMutable. 'passport' is a nullable type.

@`Person$DslMarker`
public class `Person$Mutable`(
  public var name: String,
  public var passport: `Passport$Mutable`,
  public val old: Person,
)

public fun `Person$Mutable`.freeze(): Person = com.a.b.c.Person(name =
    name, passport = passport.freeze())

public fun Person.toMutable(): `Person$Mutable` =
    com.a.b.c.`Person$Mutable`(
           old = this, 
           name = name, 
           passport = passport.toMutable()
)

public inline fun Person.copy(block: `Person$Mutable`.() -> Unit): Person =
    toMutable().apply(block).freeze()

Note The following compiles correctly

@CopyExtensions data class Person(val name: String, val passport: Passport?)
data class Passport(val id: String, val countryCode: String)
serras commented 2 years ago

Thanks for the great bug report! A fix is on its way in #74.