New J2K: Handle forced nullability correctly in nullability analysis
This commit is contained in:
@@ -130,7 +130,7 @@ internal inline val BoundType.bound
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal inline fun <reified T : BoundType> T.withForcedNullability(nullability: Nullability?): T =
|
internal inline fun <reified T : BoundType> T.withForcedNullability(nullability: Nullability?): T =
|
||||||
if (forcedNullabilityTo == nullability) this
|
if (forcedNullabilityTo == nullability || nullability == null) this
|
||||||
else when (this) {
|
else when (this) {
|
||||||
is GenericBoundType ->
|
is GenericBoundType ->
|
||||||
GenericBoundType(
|
GenericBoundType(
|
||||||
|
|||||||
@@ -113,6 +113,10 @@ internal data class AnalysisContext(
|
|||||||
val declarationToTypeVariable: Map<KtCallableDeclaration, TypeVariable>
|
val declarationToTypeVariable: Map<KtCallableDeclaration, TypeVariable>
|
||||||
)
|
)
|
||||||
|
|
||||||
data class AnalysisScope(val elements: List<KtElement>) : Iterable<KtElement> by elements {
|
data class AnalysisScope(val elements: List<PsiElement>) : Iterable<PsiElement> by elements {
|
||||||
constructor(vararg elements: KtElement) : this(elements.toList())
|
constructor(vararg elements: KtElement) : this(elements.toList())
|
||||||
|
constructor(file: KtFile, rangeMarker: RangeMarker?) :
|
||||||
|
this(rangeMarker?.let { marker ->
|
||||||
|
file.elementsInRange(TextRange(marker.startOffset, marker.endOffset))
|
||||||
|
} ?: listOf(file))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.nj2k.nullabilityAnalysis
|
package org.jetbrains.kotlin.nj2k.nullabilityAnalysis
|
||||||
|
|
||||||
import org.jetbrains.kotlin.psi.KtElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||||
import org.jetbrains.kotlin.psi.KtTypeElement
|
import org.jetbrains.kotlin.psi.KtTypeElement
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
|
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
|
||||||
@@ -55,7 +55,7 @@ internal class Printer(private val analysisContext: AnalysisContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal fun KtElement.addTypeVariablesNames() {
|
internal fun PsiElement.addTypeVariablesNames() {
|
||||||
val factory = KtPsiFactory(this)
|
val factory = KtPsiFactory(this)
|
||||||
for (typeElement in collectDescendantsOfType<KtTypeElement>()) {
|
for (typeElement in collectDescendantsOfType<KtTypeElement>()) {
|
||||||
val typeVariableName = this@Printer.analysisContext.typeElementToTypeVariable[typeElement]?.name ?: continue
|
val typeVariableName = this@Printer.analysisContext.typeElementToTypeVariable[typeElement]?.name ?: continue
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fun a(m: Map<Int, String>): String {
|
||||||
|
return m.get(42)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun b(m: Map<Int, String>): String {
|
||||||
|
return m.get(42)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun c() {
|
||||||
|
a(mapOf<Int, String>(q.to("nya")))
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fun a(m: Map<Int, String>): String? {
|
||||||
|
return m.get(42)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun b(m: Map<Int, String?>): String? {
|
||||||
|
return m.get(42)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun c() {
|
||||||
|
a(mapOf<Int, String>(q.to("nya")))
|
||||||
|
}
|
||||||
@@ -34,6 +34,11 @@ public class NullabilityAnalysisTestGenerated extends AbstractNullabilityAnalysi
|
|||||||
runTest("nj2k/testData/nullabilityAnalysis/compareWithNull.kt");
|
runTest("nj2k/testData/nullabilityAnalysis/compareWithNull.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("forcedNullability.kt")
|
||||||
|
public void testForcedNullability() throws Exception {
|
||||||
|
runTest("nj2k/testData/nullabilityAnalysis/forcedNullability.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("functionTypeParameterNullability.kt")
|
@TestMetadata("functionTypeParameterNullability.kt")
|
||||||
public void testFunctionTypeParameterNullability() throws Exception {
|
public void testFunctionTypeParameterNullability() throws Exception {
|
||||||
runTest("nj2k/testData/nullabilityAnalysis/functionTypeParameterNullability.kt");
|
runTest("nj2k/testData/nullabilityAnalysis/functionTypeParameterNullability.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user