New J2K: Handle forced nullability correctly in nullability analysis

This commit is contained in:
Ilya Kirillov
2019-04-08 18:41:11 +03:00
parent 7d5793184f
commit aa53489e0b
6 changed files with 35 additions and 4 deletions
@@ -130,7 +130,7 @@ internal inline val BoundType.bound
}
internal inline fun <reified T : BoundType> T.withForcedNullability(nullability: Nullability?): T =
if (forcedNullabilityTo == nullability) this
if (forcedNullabilityTo == nullability || nullability == null) this
else when (this) {
is GenericBoundType ->
GenericBoundType(
@@ -113,6 +113,10 @@ internal data class AnalysisContext(
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(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
import org.jetbrains.kotlin.psi.KtElement
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtTypeElement
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)
for (typeElement in collectDescendantsOfType<KtTypeElement>()) {
val typeVariableName = this@Printer.analysisContext.typeElementToTypeVariable[typeElement]?.name ?: continue
+11
View File
@@ -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");
}
@TestMetadata("forcedNullability.kt")
public void testForcedNullability() throws Exception {
runTest("nj2k/testData/nullabilityAnalysis/forcedNullability.kt");
}
@TestMetadata("functionTypeParameterNullability.kt")
public void testFunctionTypeParameterNullability() throws Exception {
runTest("nj2k/testData/nullabilityAnalysis/functionTypeParameterNullability.kt");