Skip to content

Commit

Permalink
fix error type replace logic after error type hint change
Browse files Browse the repository at this point in the history
  • Loading branch information
neetopia committed Jun 10, 2024
1 parent 8e5471f commit 00862a1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.google.devtools.ksp.common
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeArgument

// This does not work when the type is already error AND args are either empty or matches actual size.
inline fun <E> errorTypeOnInconsistentArguments(
arguments: List<KSTypeArgument>,
placeholdersProvider: () -> List<KSTypeArgument>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class KSTypeImpl private constructor(internal val type: KtType) : KSType {
}

override fun replace(arguments: List<KSTypeArgument>): KSType {
// Do not replace for already error types.
if (isError) {
return this
}
errorTypeOnInconsistentArguments(
arguments = arguments,
placeholdersProvider = { type.typeArguments().map { KSTypeArgumentResolvedImpl.getCached(it) } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ internal fun KtType.replace(newArgs: List<KtTypeProjection>): KtType {
// No need to copy nullability for type parameters
// because it is overridden to be always nullable in compiler.
is KtTypeParameterSymbol -> analysisSession.buildTypeParameterType(symbol)
else -> throw IllegalStateException("Unexpected type $this")
else -> throw IllegalStateException("Unexpected type ${this@replace}")
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions kotlin-analysis-api/testData/replaceWithErrorTypeArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
// flexible type replace argument:JS1<Int>
// [COVARIANT P], [P]
// [COVARIANT Int?], [Int?]
// replace error type get: <ERROR TYPE: Err>
// END

// MODULE: lib
Expand All @@ -127,6 +128,8 @@ class Foo<P: Any?> {
val barNullableFoo: Bar<out Int?> = TODO()
}
class Bar<T>

class ErrorSuper : Err {}
// FILE: JS.java
class JS<T1, T2> {}
class JS1<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ open class ReplaceWithErrorTypeArgsProcessor : AbstractTestProcessor() {
)
}
}
resolver.getClassDeclarationByName("ErrorSuper")?.let {
results.add("replace error type get: ${it.superTypes.single().resolve().replace(emptyList())}")
}
return emptyList()
}

Expand Down

0 comments on commit 00862a1

Please sign in to comment.