[FIR] KT-56723: Ensure safe access over non-expressions is always Unit

^KT-56723 Fixed
This commit is contained in:
Nikolay Lunyak
2023-02-17 18:04:51 +02:00
committed by Space Team
parent a8370ea994
commit be5850112a
22 changed files with 414 additions and 18 deletions
@@ -501,6 +501,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/kt56665.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/diagnostics/tests/kt56723.kt");
}
@Test
@TestMetadata("LValueAssignment.kt")
public void testLValueAssignment() throws Exception {
@@ -501,6 +501,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/kt56665.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/diagnostics/tests/kt56723.kt");
}
@Test
@TestMetadata("LValueAssignment.kt")
public void testLValueAssignment() throws Exception {
@@ -501,6 +501,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/kt56665.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/diagnostics/tests/kt56723.kt");
}
@Test
@TestMetadata("LValueAssignment.kt")
public void testLValueAssignment() throws Exception {
@@ -50813,6 +50813,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -50813,6 +50813,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -49,8 +49,8 @@ import org.jetbrains.kotlin.resolve.calls.tower.CandidateApplicability
import org.jetbrains.kotlin.types.ConstantValueKind
import org.jetbrains.kotlin.types.SmartcastStability
import org.jetbrains.kotlin.types.model.safeSubstitute
import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.runIf
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
@@ -498,31 +498,38 @@ fun FirSafeCallExpression.propagateTypeFromQualifiedAccessAfterNullCheck(
file: FirFile,
) {
val receiverType = nullableReceiverExpression.typeRef.coneTypeSafe<ConeKotlinType>()
val typeAfterNullCheck = selector.expressionTypeOrUnitForAssignment() ?: return
val isReceiverActuallyNullable = if (session.languageVersionSettings.supportsFeature(LanguageFeature.SafeCallsAreAlwaysNullable)) {
true
} else {
receiverType != null && session.typeContext.run { receiverType.isNullableType() }
val selector = selector
val resultingType = when {
selector is FirExpression && !selector.isCallToStatementLikeFunction -> {
val type = selector.typeRef.coneTypeSafe<ConeKotlinType>() ?: return
val isReceiverActuallyNullable = session.languageVersionSettings.supportsFeature(LanguageFeature.SafeCallsAreAlwaysNullable)
|| receiverType != null && session.typeContext.run { receiverType.isNullableType() }
if (isReceiverActuallyNullable) {
type.withNullability(ConeNullability.NULLABLE, session.typeContext)
} else {
type
}
}
// Branch for things that shouldn't be used as expressions.
// They are forced to return not-null `Unit`, regardless of the receiver.
else -> {
StandardClassIds.Unit.constructClassLikeType(emptyArray(), isNullable = false)
}
}
val resultingType =
if (isReceiverActuallyNullable)
typeAfterNullCheck.withNullability(ConeNullability.NULLABLE, session.typeContext)
else
typeAfterNullCheck
val resolvedTypeRef = typeRef.resolvedTypeFromPrototype(resultingType)
replaceTypeRef(resolvedTypeRef)
session.lookupTracker?.recordTypeResolveAsLookup(resolvedTypeRef, source, file.source)
}
private fun FirStatement.expressionTypeOrUnitForAssignment(): ConeKotlinType? {
if (this is FirExpression) return typeRef.coneTypeSafe()
require(this is FirVariableAssignment) {
"The only non-expression FirQualifiedAccess is FirVariableAssignment, but ${this::class} was found"
private val FirExpression.isCallToStatementLikeFunction: Boolean
get() {
val symbol = (this as? FirFunctionCall)?.calleeReference?.toResolvedFunctionSymbol() ?: return false
return origin == FirFunctionCallOrigin.Operator && symbol.name in OperatorNameConventions.STATEMENT_LIKE_OPERATORS
}
return StandardClassIds.Unit.constructClassLikeType(emptyArray(), isNullable = false)
}
fun FirAnnotation.getCorrespondingClassSymbolOrNull(session: FirSession): FirRegularClassSymbol? {
return annotationTypeRef.coneType.fullyExpandedType(session).classId?.let {
+27
View File
@@ -0,0 +1,27 @@
fun foo(f: () -> Unit, returnIfOk: String): String {
val string = f().toString()
return if (string == "kotlin.Unit") {
returnIfOk
} else {
"FAIL: $string;"
}
}
class Wrapper(var s: String)
fun box(): String {
val w: Wrapper? = Wrapper("Test")
val lambda = {
w?.s = "X"
}
val w2: Wrapper? = null
val lambda2 = {
w2?.s = "X"
}
return foo(lambda, "O") + foo(lambda2, "K")
}
+91
View File
@@ -0,0 +1,91 @@
import kotlin.reflect.KProperty
fun foo(f: () -> Unit) {
f()
}
class Wrapper(var s: String)
fun bar(w: Wrapper?) {
// K1: type is () -> Unit, K2: type is () -> Unit?
val lambda = {
w?.s = "X"
}
// K1: Ok, K2: ARGUMENT_TYPE_MISMATCH
foo(lambda)
}
class Wrapper2(val w: Wrapper?)
fun baz(w2: Wrapper2?) {
val lambda = {
w2?.w?.s = "X"
}
foo(lambda)
}
object Indexible {
operator fun get(index: Int) = "$index"
operator fun set(index: Int, value: String) {}
}
class IndexibleRef(val ind: Indexible)
class IndexibleRefRef(val ref: IndexibleRef?)
fun ban(refRef: IndexibleRefRef?, ref: IndexibleRef?) {
val lambda = {
ref?.ind[1] = "X"
}
foo(lambda)
val lambda2 = {
refRef?.ref?.ind[1] = "X"
}
foo(lambda2)
val lambda3 = {
ref?.ind?.set(1, "X")
}
foo(<!ARGUMENT_TYPE_MISMATCH!>lambda3<!>)
val lambda4 = {
refRef?.ref?.ind?.set(1, "X")
}
foo(<!ARGUMENT_TYPE_MISMATCH!>lambda4<!>)
}
object PlusAssignable {
operator fun plusAssign(index: Int) {}
}
object Indexible2 {
operator fun get(index: Int) = PlusAssignable
operator fun set(index: Int, value: String) {}
}
class Indexible2Ref(val ind: Indexible2)
fun bam(ref: Indexible2Ref?) {
val lambda = {
ref?.ind[1] += 1
}
foo(lambda)
val lambd2 = {
ref?.ind?.get(1)?.plusAssign(1)
}
foo(lambda)
}
class DelegatedHolder {
var delegated by object {
operator fun getValue(thisRef: Any?, desc: KProperty<*>) = "test"
operator fun setValue(thisRef: Any?, desc: KProperty<*>, value: String) {}
}
}
fun bap(holder: DelegatedHolder?) {
val lambda = {
holder?.delegated = "Y"
}
foo(lambda)
}
+91
View File
@@ -0,0 +1,91 @@
import kotlin.reflect.KProperty
fun foo(f: () -> Unit) {
f()
}
class Wrapper(var s: String)
fun bar(w: Wrapper?) {
// K1: type is () -> Unit, K2: type is () -> Unit?
val lambda = {
w?.s = "X"
}
// K1: Ok, K2: ARGUMENT_TYPE_MISMATCH
foo(lambda)
}
class Wrapper2(val w: Wrapper?)
fun baz(w2: Wrapper2?) {
val lambda = {
w2?.w?.s = "X"
}
foo(lambda)
}
object Indexible {
operator fun get(index: Int) = "$index"
operator fun set(index: Int, value: String) {}
}
class IndexibleRef(val ind: Indexible)
class IndexibleRefRef(val ref: IndexibleRef?)
fun ban(refRef: IndexibleRefRef?, ref: IndexibleRef?) {
val lambda = {
<!UNSAFE_CALL!>ref?.ind[1]<!> = "X"
}
foo(lambda)
val lambda2 = {
<!UNSAFE_CALL!>refRef?.ref?.ind[1]<!> = "X"
}
foo(lambda2)
val lambda3 = {
ref?.ind?.set(1, "X")
}
foo(<!UNSUPPORTED_FEATURE!>lambda3<!>)
val lambda4 = {
refRef?.ref?.ind?.set(1, "X")
}
foo(<!UNSUPPORTED_FEATURE!>lambda4<!>)
}
object PlusAssignable {
operator fun plusAssign(index: Int) {}
}
object Indexible2 {
operator fun get(index: Int) = PlusAssignable
operator fun set(index: Int, value: String) {}
}
class Indexible2Ref(val ind: Indexible2)
fun bam(ref: Indexible2Ref?) {
val lambda = {
<!UNSAFE_CALL!>ref?.ind[1]<!> += 1
}
foo(lambda)
val lambd2 = {
ref?.ind?.get(1)?.plusAssign(1)
}
foo(lambda)
}
class DelegatedHolder {
var delegated by object {
operator fun getValue(thisRef: Any?, desc: KProperty<*>) = "test"
operator fun setValue(thisRef: Any?, desc: KProperty<*>, value: String) {}
}
}
fun bap(holder: DelegatedHolder?) {
val lambda = {
holder?.delegated = "Y"
}
foo(lambda)
}
+83
View File
@@ -0,0 +1,83 @@
package
public fun bam(/*0*/ ref: Indexible2Ref?): kotlin.Unit
public fun ban(/*0*/ refRef: IndexibleRefRef?, /*1*/ ref: IndexibleRef?): kotlin.Unit
public fun bap(/*0*/ holder: DelegatedHolder?): kotlin.Unit
public fun bar(/*0*/ w: Wrapper?): kotlin.Unit
public fun baz(/*0*/ w2: Wrapper2?): kotlin.Unit
public fun foo(/*0*/ f: () -> kotlin.Unit): kotlin.Unit
public final class DelegatedHolder {
public constructor DelegatedHolder()
public final var delegated: kotlin.String
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public object Indexible {
private constructor Indexible()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun get(/*0*/ index: kotlin.Int): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.String): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public object Indexible2 {
private constructor Indexible2()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun get(/*0*/ index: kotlin.Int): PlusAssignable
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.String): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Indexible2Ref {
public constructor Indexible2Ref(/*0*/ ind: Indexible2)
public final val ind: Indexible2
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class IndexibleRef {
public constructor IndexibleRef(/*0*/ ind: Indexible)
public final val ind: Indexible
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class IndexibleRefRef {
public constructor IndexibleRefRef(/*0*/ ref: IndexibleRef?)
public final val ref: IndexibleRef?
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public object PlusAssignable {
private constructor PlusAssignable()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun plusAssign(/*0*/ index: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Wrapper {
public constructor Wrapper(/*0*/ s: kotlin.String)
public final var s: kotlin.String
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Wrapper2 {
public constructor Wrapper2(/*0*/ w: Wrapper?)
public final val w: Wrapper?
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -501,6 +501,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/kt56665.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/diagnostics/tests/kt56723.kt");
}
@Test
@TestMetadata("LValueAssignment.kt")
public void testLValueAssignment() throws Exception {
@@ -48581,6 +48581,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -50813,6 +50813,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -39457,6 +39457,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
runTest("compiler/testData/codegen/box/unit/nullableUnit.kt");
@@ -95,4 +95,7 @@ object OperatorNameConventions {
@JvmField
val MOD_OPERATORS_REPLACEMENT = mapOf(MOD to REM, MOD_ASSIGN to REM_ASSIGN)
@JvmField
val STATEMENT_LIKE_OPERATORS = setOf(SET) + ASSIGNMENT_OPERATIONS
}
@@ -36013,6 +36013,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -36199,6 +36199,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -36199,6 +36199,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -36199,6 +36199,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -39842,6 +39842,12 @@ public class K2NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTes
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -39344,6 +39344,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -32422,6 +32422,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@TestMetadata("kt56723.kt")
public void testKt56723() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt56723.kt");
}
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
runTest("compiler/testData/codegen/box/unit/nullableUnit.kt");