Skip to content

Commit 64daa7e

Browse files
committed
[FIR2IR] Properly handle generics with nullable types in delegate body generation
There was a misconception between `isMarkedNullable` and `canBeNull`. ^KT-79816 Fixed
1 parent 9237f28 commit 64daa7e

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/backend/Fir2IrFakeOverrideStrategy.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ class Fir2IrDelegatedMembersGenerationStrategy(
386386

387387
val irCastOrCall = if (
388388
delegateTargetFunction.returnType.let { it.hasAnnotation(FlexibleNullability) || it.hasAnnotation(EnhancedNullability) } &&
389-
!delegatedFunction.returnType.isMarkedNullable()
389+
!delegatedFunction.returnType.canBeNull()
390390
) {
391391
Fir2IrImplicitCastInserter.implicitNotNullCast(irCall)
392392
} else {

compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypePredicates.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.ir.declarations.IrClass
1212
import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
1313
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
1414
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
15+
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
1516
import org.jetbrains.kotlin.ir.types.impl.IrCapturedType
1617
import org.jetbrains.kotlin.ir.util.IdSignature
1718
import org.jetbrains.kotlin.ir.util.hasEqualFqName
@@ -158,8 +159,17 @@ fun <T : Enum<T>> IrType.getPrimitiveOrUnsignedType(byIdSignature: Map<IdSignatu
158159
return byShortName[klass.name]
159160
}
160161

161-
fun IrType.isMarkedNullable() = (this as? IrSimpleType)?.nullability == SimpleTypeNullability.MARKED_NULLABLE
162-
fun IrSimpleType.isMarkedNullable() = nullability == SimpleTypeNullability.MARKED_NULLABLE
162+
fun IrType.isMarkedNullable(): Boolean = (this as? IrSimpleType)?.nullability == SimpleTypeNullability.MARKED_NULLABLE
163+
fun IrSimpleType.isMarkedNullable(): Boolean = nullability == SimpleTypeNullability.MARKED_NULLABLE
164+
165+
fun IrType.canBeNull(): Boolean = when (this) {
166+
is IrSimpleType -> isMarkedNullable() || when (this) {
167+
is IrCapturedType -> constructor.superTypes.any { it.canBeNull() }
168+
else -> (classifier as? IrTypeParameterSymbol)?.owner?.superTypes?.all { it.canBeNull() } ?: false
169+
}
170+
is IrDynamicType -> true
171+
is IrErrorType -> isMarkedNullable
172+
}
163173

164174
fun IrType.isUnit() = isNotNullClassType(IdSignatureValues.unit)
165175

compiler/testData/codegen/box/classDelegation/notNullAssertionsAndDelegationToGenericInterface.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// TARGET_BACKEND: JVM_IR
2-
// IGNORE_BACKEND_K2: JVM_IR
32
// FULL_JDK
43
// ISSUE: KT-79816
54

0 commit comments

Comments
 (0)