[FIR] Improve readability of rendered types in diagnostics

#KT-61824 Fixed
#KT-61688 Fixed
This commit is contained in:
Kirill Rakhman
2023-09-12 14:27:29 +02:00
committed by Space Team
parent 99b852adf8
commit 4e1dfcd2a8
25 changed files with 65 additions and 37 deletions
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.fir.renderer.ConeTypeRenderer
import org.jetbrains.kotlin.fir.renderer.FirRenderer
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
import org.jetbrains.kotlin.fir.types.ConeAttributes
import org.jetbrains.kotlin.fir.types.ConeAttribute
import org.jetbrains.kotlin.fir.types.FirTypeRef
/**
@@ -114,5 +114,5 @@ private fun FirTypeRef.renderType(builder: StringBuilder = StringBuilder()): Str
).renderElementAsString(this)
private object EmptyConeTypeAttributeRenderer : ConeAttributeRenderer() {
override fun render(attributes: ConeAttributes): String = ""
override fun render(attributes: Iterable<ConeAttribute<*>>): String = ""
}
@@ -3,7 +3,7 @@ class C
class B
class A {
val B.foo: C.() -> Unit get() = <!NULL_FOR_NONNULL_TYPE("@ExtensionFunctionType kotlin/Function1<C, kotlin/Unit>")!>null<!>
val B.foo: C.() -> Unit get() = <!NULL_FOR_NONNULL_TYPE("kotlin/Function1<C, kotlin/Unit>")!>null<!>
}
fun <T, R> with(arg: T, f: T.() -> R): R = arg.f()
@@ -5,12 +5,18 @@
package org.jetbrains.kotlin.fir.renderer
import org.jetbrains.kotlin.fir.types.ConeAttributes
import org.jetbrains.kotlin.fir.types.ConeAttribute
abstract class ConeAttributeRenderer {
abstract fun render(attributes: ConeAttributes): String
abstract fun render(attributes: Iterable<ConeAttribute<*>>): String
object ToString : ConeAttributeRenderer() {
override fun render(attributes: ConeAttributes): String = attributes.joinToString(separator = " ", postfix = " ") { it.toString() }
override fun render(attributes: Iterable<ConeAttribute<*>>): String =
attributes.joinToString(separator = " ", postfix = " ") { it.toString() }
}
object ForReadability : ConeAttributeRenderer() {
override fun render(attributes: Iterable<ConeAttribute<*>>): String =
attributes.joinToString(separator = " ", postfix = " ") { it.renderForReadability() }
}
}
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.renderer
import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
open class ConeTypeRenderer(
private val attributeRenderer: ConeAttributeRenderer = ConeAttributeRenderer.ToString
@@ -180,16 +181,16 @@ open class ConeTypeRenderer(
builder.append(">")
}
private fun ConeKotlinType.renderAttributes() {
protected open fun ConeKotlinType.renderAttributes() {
if (!attributes.any()) return
builder.append(attributeRenderer.render(attributes))
}
private fun ConeKotlinType.renderNonCompilerAttributes() {
protected fun ConeKotlinType.renderNonCompilerAttributes() {
val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttributeKey
if (attributes.any { it.key !in compilerAttributes }) {
builder.append(attributeRenderer.render(attributes))
}
attributes
.filter { it.key !in compilerAttributes }
.ifNotEmpty { builder.append(attributeRenderer.render(this)) }
}
private fun ConeTypeProjection.render() {
@@ -8,13 +8,13 @@ package org.jetbrains.kotlin.fir.renderer
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.fir.types.ConeFlexibleType
import org.jetbrains.kotlin.fir.types.ConeIntegerLiteralType
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.renderer.replacePrefixesInTypeRepresentations
import org.jetbrains.kotlin.renderer.typeStringsDifferOnlyInNullability
class ConeTypeRendererForReadability(
private val idRendererCreator: () -> ConeIdRenderer,
) : ConeTypeRenderer() {
) : ConeTypeRenderer(ConeAttributeRenderer.ForReadability) {
constructor(builder: StringBuilder, idRendererCreator: () -> ConeIdRenderer) : this(idRendererCreator) {
this.builder = builder
this.idRenderer = idRendererCreator()
@@ -76,4 +76,8 @@ class ConeTypeRendererForReadability(
override fun render(type: ConeIntegerLiteralType) {
render(type.getApproximatedType())
}
override fun ConeKotlinType.renderAttributes() {
renderNonCompilerAttributes()
}
}
@@ -30,6 +30,7 @@ abstract class ConeAttribute<out T : ConeAttribute<T>> : AnnotationMarker {
abstract fun isSubtypeOf(other: @UnsafeVariance T?): Boolean
abstract override fun toString(): String
open fun renderForReadability(): String = toString()
abstract val key: KClass<out T>
}
@@ -66,6 +66,18 @@ class FirRenderer(
fun withDeclarationAttributes(): FirRenderer =
FirRenderer(declarationRenderer = FirDeclarationRendererWithAttributes())
fun forReadability(): FirRenderer = FirRenderer(
typeRenderer = ConeTypeRenderer(),
idRenderer = ConeIdShortRenderer(),
classMemberRenderer = FirNoClassMemberRenderer(),
bodyRenderer = null,
propertyAccessorRenderer = null,
callArgumentsRenderer = FirCallNoArgumentsRenderer(),
modifierRenderer = FirPartialModifierRenderer(),
valueParameterRenderer = FirValueParameterRendererNoDefaultValue(),
declarationRenderer = FirDeclarationRenderer("local ")
)
}
init {
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
import org.jetbrains.kotlin.fir.expressions.builder.buildAnnotationCallCopy
import org.jetbrains.kotlin.fir.expressions.builder.buildAnnotationCopy
import org.jetbrains.kotlin.fir.render
import org.jetbrains.kotlin.fir.renderer.FirRenderer
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import kotlin.reflect.KClass
@@ -55,6 +56,9 @@ class CustomAnnotationTypeAttribute(
override fun toString(): String = annotations.joinToString(separator = " ") { it.render() }
override fun renderForReadability(): String =
annotations.joinToString(separator = " ") { FirRenderer.forReadability().renderElementAsString(it, trim = true) }
override val key: KClass<out CustomAnnotationTypeAttribute>
get() = CustomAnnotationTypeAttribute::class
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/compatqualUsage.kt:2:11: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/compatqualUsage.kt:2:11: error: null cannot be a value of a non-null type 'kotlin/String'.
b.foo(null)
^
COMPILATION_ERROR
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/compatqualUsage.kt:2:11: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/compatqualUsage.kt:2:11: error: null cannot be a value of a non-null type 'kotlin/String'.
b.foo(null)
^
COMPILATION_ERROR
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/jspecifyUsage.kt:2:11: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/jspecifyUsage.kt:2:11: error: null cannot be a value of a non-null type 'kotlin/String'.
a.foo(null)
^
COMPILATION_ERROR
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/jspecifyUsage.kt:2:11: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/jspecifyUsage.kt:2:11: error: null cannot be a value of a non-null type 'kotlin/String'.
a.foo(null)
^
COMPILATION_ERROR
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/jsr305DefaultMigration.kt:2:19: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/jsr305DefaultMigration.kt:2:19: error: null cannot be a value of a non-null type 'kotlin/String'.
annotated.foo(null)
^
COMPILATION_ERROR
+1 -1
View File
@@ -1,6 +1,6 @@
warning: argument -Xjsr305-annotations is deprecated. Please use -Xjsr305 instead
warning: option 'enable' for -Xjsr305 flag is deprecated. Please use 'strict' instead
compiler/testData/cli/jvm/jsr305Usage.kt:2:11: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/jsr305Usage.kt:2:11: error: null cannot be a value of a non-null type 'kotlin/String'.
a.foo(null)
^
COMPILATION_ERROR
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/jsr305Migration.kt:3:19: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/jsr305Migration.kt:3:19: error: null cannot be a value of a non-null type 'kotlin/String'.
annotated.bar(null)
^
COMPILATION_ERROR
+2 -2
View File
@@ -1,7 +1,7 @@
compiler/testData/cli/jvm/jsr305Migration.kt:2:19: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/jsr305Migration.kt:2:19: error: null cannot be a value of a non-null type 'kotlin/String'.
annotated.foo(null)
^
compiler/testData/cli/jvm/jsr305Migration.kt:3:19: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/jsr305Migration.kt:3:19: error: null cannot be a value of a non-null type 'kotlin/String'.
annotated.bar(null)
^
COMPILATION_ERROR
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/jsr305Migration.kt:3:19: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/jsr305Migration.kt:3:19: error: null cannot be a value of a non-null type 'kotlin/String'.
annotated.bar(null)
^
COMPILATION_ERROR
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/jsr305Usage.kt:2:11: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/jsr305Usage.kt:2:11: error: null cannot be a value of a non-null type 'kotlin/String'.
a.foo(null)
^
COMPILATION_ERROR
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/severalAnnotations.kt:2:11: error: null cannot be a value of a non-null type '@EnhancedNullability kotlin/String'.
compiler/testData/cli/jvm/severalAnnotations.kt:2:11: error: null cannot be a value of a non-null type 'kotlin/String'.
a.foo(null)
^
COMPILATION_ERROR
@@ -12,14 +12,14 @@ annotation class Ann
fun <@Ann R : @Ann Any> f3(a: Array<@Ann R>): Array<@Ann R?> = null!!
fun test2(a: @Ann Array<in @Ann Int>) {
val r: Array<in Int?> = f3(<!ARGUMENT_TYPE_MISMATCH("kotlin/Array<@R|Ann|() R>; @R|Ann|() kotlin/Array<CapturedType(in @R|Ann|() kotlin/Int)>")!>a<!>)
val r: Array<in Int?> = f3(<!ARGUMENT_TYPE_MISMATCH("kotlin/Array<@Ann() R>; @Ann() kotlin/Array<CapturedType(in @Ann() kotlin/Int)>")!>a<!>)
}
var test3: Int = 0
set(s: <!WRONG_SETTER_PARAMETER_TYPE("kotlin/Int; @R|Ann|() kotlin/String")!>@Ann String<!>) {}
set(s: <!WRONG_SETTER_PARAMETER_TYPE("kotlin/Int; @Ann() kotlin/String")!>@Ann String<!>) {}
fun f4(fn: (@Ann Int, @Ann Int) -> Unit) {}
val test4 = f4 <!ARGUMENT_TYPE_MISMATCH("kotlin/Function2<@R|Ann|() kotlin/Int, @R|Ann|() kotlin/Int, kotlin/Unit>; kotlin/Function1<@R|Ann|() kotlin/Int, kotlin/Unit>")!>{ single -> }<!>
val test4 = f4 <!ARGUMENT_TYPE_MISMATCH("kotlin/Function2<@Ann() kotlin/Int, @Ann() kotlin/Int, kotlin/Unit>; kotlin/Function1<@Ann() kotlin/Int, kotlin/Unit>")!>{ single -> }<!>
@@ -11,5 +11,5 @@ class C<T> {
}
fun test(a: C<out CharSequence>) {
a[1] = <!ARGUMENT_TYPE_MISMATCH("@R|A|() CapturedType(out kotlin/CharSequence); kotlin/Int")!>25<!>
a[1] = <!ARGUMENT_TYPE_MISMATCH("@A() CapturedType(out kotlin/CharSequence); kotlin/Int")!>25<!>
}
@@ -13,5 +13,5 @@ class C<T> {
class Out<out F>
fun test(a: C<out CharSequence>, y: Out<CharSequence>) {
a + <!ARGUMENT_TYPE_MISMATCH("Out<@R|A|() CapturedType(out kotlin/CharSequence)>; Out<kotlin/CharSequence>")!>y<!>
a + <!ARGUMENT_TYPE_MISMATCH("Out<@A() CapturedType(out kotlin/CharSequence)>; Out<kotlin/CharSequence>")!>y<!>
}
@@ -17,7 +17,7 @@ interface A {
interface B : A {
override val p1: <!PROPERTY_TYPE_MISMATCH_ON_OVERRIDE("p1; @An() val p1: @R|An|() String")!>Int<!>
@An
override <!VAR_OVERRIDDEN_BY_VAL("public abstract override val /B.p2: @R|An|() kotlin/String public get(): @R|An|() kotlin/String; public abstract var /A.p2: @R|An|() kotlin/String public get(): @R|An|() kotlin/String public set(value: @R|An|() kotlin/String): kotlin/Unit")!>val<!> p2: @An String
override <!VAR_OVERRIDDEN_BY_VAL("public abstract override val /B.p2: @An() kotlin/String public get(): @An() kotlin/String; public abstract var /A.p2: @An() kotlin/String public get(): @An() kotlin/String public set(value: @An() kotlin/String): kotlin/Unit")!>val<!> p2: @An String
override fun test(arg: String): <!RETURN_TYPE_MISMATCH_ON_OVERRIDE("test; @An() fun test(@An() arg: @R|An|() String): @R|An|() String")!>Int<!>
}
@@ -30,7 +30,7 @@ fun testYield() {
val buildee = build {
yield { val x: UserKlass = this<!UNRESOLVED_LABEL!>@yield<!> }
}
checkExactType<Buildee<UserKlass.() -> Unit>>(<!ARGUMENT_TYPE_MISMATCH("Buildee<@ExtensionFunctionType kotlin/Function1<UserKlass, kotlin/Unit>>; Buildee<kotlin/Function0<kotlin/Unit>>")!>buildee<!>)
checkExactType<Buildee<UserKlass.() -> Unit>>(<!ARGUMENT_TYPE_MISMATCH("Buildee<kotlin/Function1<UserKlass, kotlin/Unit>>; Buildee<kotlin/Function0<kotlin/Unit>>")!>buildee<!>)
}
// test 2: PTV is in producing position (materialize-case)
@@ -42,5 +42,5 @@ fun testMaterialize() {
materialize()
)
}
checkExactType<Buildee<UserKlass.() -> Unit>>(<!ARGUMENT_TYPE_MISMATCH("Buildee<@ExtensionFunctionType kotlin/Function1<UserKlass, kotlin/Unit>>; Buildee<kotlin/Function0<kotlin/Unit>>")!>buildee<!>)
checkExactType<Buildee<UserKlass.() -> Unit>>(<!ARGUMENT_TYPE_MISMATCH("Buildee<kotlin/Function1<UserKlass, kotlin/Unit>>; Buildee<kotlin/Function0<kotlin/Unit>>")!>buildee<!>)
}
@@ -1,7 +1,7 @@
/main.kt:(98,103): error: Argument type mismatch: actual type is 'V', but '@EnhancedNullability V & Any' was expected.
/main.kt:(98,103): error: Argument type mismatch: actual type is 'V', but 'V & Any' was expected.
/main.kt:(119,120): error: Argument type mismatch: actual type is 'kotlin/collections/List<V>', but 'ft<@EnhancedNullability kotlin/collections/MutableList<@EnhancedNullability @R|org/jetbrains/annotations/NotNull|() V & Any>, @EnhancedNullability kotlin/collections/List<@EnhancedNullability @R|org/jetbrains/annotations/NotNull|() V & Any>>' was expected.
/main.kt:(119,120): error: Argument type mismatch: actual type is 'kotlin/collections/List<V>', but 'kotlin/collections/(Mutable)List<@NotNull() V & Any>' was expected.
/main.kt:(133,138): error: Argument type mismatch: actual type is 'V', but '@EnhancedNullability E & Any' was expected.
/main.kt:(133,138): error: Argument type mismatch: actual type is 'V', but 'E & Any' was expected.
/main.kt:(154,155): error: Argument type mismatch: actual type is 'kotlin/collections/List<V>', but 'ft<@EnhancedNullability kotlin/collections/MutableList<@EnhancedNullability @R|org/jetbrains/annotations/NotNull|() E & Any>, @EnhancedNullability kotlin/collections/List<@EnhancedNullability @R|org/jetbrains/annotations/NotNull|() E & Any>>' was expected.
/main.kt:(154,155): error: Argument type mismatch: actual type is 'kotlin/collections/List<V>', but 'kotlin/collections/(Mutable)List<@NotNull() E & Any>' was expected.