Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support definitely non-nullable types #1268

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -34,6 +34,7 @@ public class TypeVariableName private constructor(
/** Either [KModifier.IN], [KModifier.OUT], or null. */
public val variance: KModifier? = null,
public val isReified: Boolean = false,
public val isDefinitelyNonNullable: Boolean = false,
nullable: Boolean = false,
annotations: List<AnnotationSpec> = emptyList(),
tags: Map<KClass<*>, Any> = emptyMap()
Expand All @@ -44,7 +45,7 @@ public class TypeVariableName private constructor(
annotations: List<AnnotationSpec>,
tags: Map<KClass<*>, Any>
): TypeVariableName {
return copy(nullable, annotations, this.bounds, this.isReified, tags)
return copy(nullable, annotations, bounds, isReified, tags)
}

public fun copy(
Expand All @@ -53,18 +54,72 @@ public class TypeVariableName private constructor(
bounds: List<TypeName> = this.bounds.toList(),
reified: Boolean = this.isReified,
tags: Map<KClass<*>, Any> = this.tagMap.tags
): TypeVariableName = copy(
nullable = nullable,
annotations = annotations,
bounds = bounds,
reified = reified,
tags = tags,
isDefinitelyNonNullable = isDefinitelyNonNullable
)

private fun copy(
nullable: Boolean = this.isNullable,
annotations: List<AnnotationSpec> = this.annotations.toList(),
bounds: List<TypeName> = this.bounds.toList(),
reified: Boolean = this.isReified,
tags: Map<KClass<*>, Any> = this.tagMap.tags,
isDefinitelyNonNullable: Boolean = this.isDefinitelyNonNullable,
): TypeVariableName {
if (isDefinitelyNonNullable) {
require(!nullable) {
"Cannot make a TypeVariableName nullable if it is already definitely non-nullable"
}
}
return TypeVariableName(
name, bounds.withoutImplicitBound(), variance, reified, nullable,
annotations, tags
name = name,
bounds = bounds.withoutImplicitBound(),
variance = variance,
isReified = reified,
isDefinitelyNonNullable = isDefinitelyNonNullable,
nullable = nullable,
annotations = annotations,
tags = tags
)
}

/**
* Returns a new [TypeVariableName] copy of this that will emit as a "definitely non-nullable"
* type with the `T & Any` syntax.
*
* See https://kotlinlang.org/docs/whatsnew17.html#stable-definitely-non-nullable-types.
*/
public fun copy(isDefinitelyNonNullable: Boolean = true): TypeVariableName {
if (isDefinitelyNonNullable) {
require(!isNullable) {
"Cannot make a TypeVariableName non-nullable if it is already definitely non-nullable"
}
require(bounds.none { it == ANY }) {
"Cannot make a TypeVariableName non-nullable if it already has a non-nullable Any bound"
}
}
return copy(
isDefinitelyNonNullable = isDefinitelyNonNullable
)
}

private fun List<TypeName>.withoutImplicitBound(): List<TypeName> {
return if (size == 1) this else filterNot { it == NULLABLE_ANY }
}

override fun emit(out: CodeWriter) = out.emit(name)
override fun emit(out: CodeWriter): CodeWriter {
if (isDefinitelyNonNullable) {
out.emit("$name·&·Any")
} else {
out.emit(name)
}
return out
}

public companion object {
internal fun of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.google.common.truth.Truth.assertThat
import com.squareup.kotlinpoet.TypeVariableName.Companion.NULLABLE_ANY_LIST
import java.io.Serializable
import kotlin.test.Test
import kotlin.test.assertFailsWith

class TypeVariableNameTest {
@Test fun nullableAnyIsImplicitBound() {
Expand Down Expand Up @@ -251,4 +252,34 @@ class TypeVariableNameTest {
}

class GenericClass<T>

@Test
fun definitelyNonNullableType_simple() {
val typeName = TypeVariableName("T").copy()
assertThat(typeName.toString()).isEqualTo("T & Any")
Comment on lines +258 to +259
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very surprising behavior

}

@Test
fun definitelyNonNullableType_errorWhenNonNullableMadeNullable() {
assertFailsWith<IllegalArgumentException> {
TypeVariableName("T").copy()
.copy(nullable = true)
}
}

@Test
fun definitelyNonNullableType_errorWhenNullableMadeNonNullable() {
assertFailsWith<IllegalArgumentException> {
(TypeVariableName("T").copy(nullable = true) as TypeVariableName)
.copy()
}
}

@Test
fun definitelyNonNullableType_errorWhenNonNullAny() {
assertFailsWith<IllegalArgumentException> {
TypeVariableName("T", listOf(ANY))
.copy()
}
}
}