Skip to content

Commit 0d72352

Browse files
committed
K2: Fix controversial inference results with !! operator
The problem was that inference starts working differently depending on the presence of non-trivial bound (Any instead of Any?), see KT-69266 and KT-69326. The fix for the issues above would lead to re-work of how we choose the order in which we fix the variables. But the cases from the issue were working in K1 correctly because synthetic call for `!!` has a different signature (not having an upper bound for the type parameter). Thus, the solution is just repeating K1 behavior here, too. ^KT-69159 Fixed
1 parent 29daa48 commit 0d72352

File tree

8 files changed

+17
-35
lines changed

8 files changed

+17
-35
lines changed

compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,14 +471,21 @@ class FirSyntheticCallGenerator(
471471

472472
private fun generateSyntheticCheckNotNullFunction(): FirSimpleFunction {
473473
// Synthetic function signature:
474-
// fun <K : Any> checkNotNull(arg: K?): K
474+
// fun <K> checkNotNull(arg: K?): K & Any
475475
val functionSymbol = FirSyntheticFunctionSymbol(SyntheticCallableId.CHECK_NOT_NULL)
476-
val (typeParameter, typeParameterTypeRef) = generateSyntheticSelectTypeParameter(functionSymbol, isNullableBound = false)
476+
val (typeParameter, typeParameterTypeRef) = generateSyntheticSelectTypeParameter(functionSymbol, isNullableBound = true)
477477

478478
return generateMemberFunction(
479479
functionSymbol,
480480
SyntheticCallableId.CHECK_NOT_NULL.callableName,
481-
typeParameterTypeRef,
481+
returnType = typeParameterTypeRef.withReplacedConeType(
482+
typeParameterTypeRef.type.makeConeTypeDefinitelyNotNullOrNotNull(
483+
session.typeContext,
484+
// No checks are necessary because we're sure that the type parameter has default (nullable) upper bound.
485+
// At the same time, not having `avoidComprehensiveCheck = true` might lead to plugin initialization issues.
486+
avoidComprehensiveCheck = true,
487+
)
488+
),
482489
).apply {
483490
typeParameters += typeParameter
484491

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fun main() {
2-
<!UNSUPPORTED!>[]<!><!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!><!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
2+
<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!><!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!><!UNSUPPORTED!>[]<!><!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!><!>!!<!>
33
}

compiler/testData/diagnostics/tests/inference/kt69159Simplified.fir.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

compiler/testData/diagnostics/tests/inference/kt69159Simplified.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// FIR_IDENTICAL
12
// ISSUE: KT-69159
23

34
interface MyExpression<F>

compiler/testData/diagnostics/tests/inference/kt69159WithComparable.fir.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

compiler/testData/diagnostics/tests/inference/kt69159WithComparable.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// FIR_IDENTICAL
12
// ISSUE: KT-69159
23
interface MyExpression<F>
34

compiler/testData/diagnostics/tests/inference/kt69326.fir.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ fun <T : Comparable<T>, S : T> MyExpression<in S>.min2(): MyExpression<T?> = TOD
1010
fun <K : Any> checkNotNull(k: K?): K = TODO()
1111

1212
fun foo(x: MyExpression<String>) {
13-
getElement(x.min1())!!.<!UNRESOLVED_REFERENCE!>length<!>
13+
getElement(x.min1())!!.length
1414
checkNotNull(getElement(x.min1())).<!UNRESOLVED_REFERENCE!>length<!>
1515

16-
getElement(x.min2())!!.<!UNRESOLVED_REFERENCE!>length<!>
16+
getElement(x.min2())!!.length
1717
checkNotNull(getElement(x.min2())).<!UNRESOLVED_REFERENCE!>length<!>
1818
}

compiler/testData/diagnostics/tests/inference/regressions/kt36342.fir.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import java.lang.Exception
44
fun <K> id(arg: K): K = arg
55

66
fun test() {
7-
id(<!UNRESOLVED_REFERENCE!>unresolved<!>)!!
8-
<!UNRESOLVED_REFERENCE!>unresolved<!>!!<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
7+
<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!><!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>id<!>(<!UNRESOLVED_REFERENCE!>unresolved<!>)!!<!>
8+
<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!><!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!><!UNRESOLVED_REFERENCE!>unresolved<!>!!<!>!!<!>
99
<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>try {
1010
<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>id<!>(<!UNRESOLVED_REFERENCE!>unresolved<!>)
1111
} catch (e: Exception) {

0 commit comments

Comments
 (0)