FirSyntheticPropertiesScope: support case with derived getter

#KT-42496 Fixed
This commit is contained in:
Mikhail Glukhikh
2020-10-13 18:29:22 +03:00
parent 2f9b7495fc
commit babc7845af
4 changed files with 36 additions and 4 deletions
@@ -18,9 +18,11 @@ import org.jetbrains.kotlin.fir.symbols.CallableId
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
import org.jetbrains.kotlin.fir.symbols.SyntheticSymbol
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.typeContext
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.ConeNullability.NOT_NULL
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.AbstractTypeChecker
class SyntheticPropertySymbol(
callableId: CallableId,
@@ -72,8 +74,28 @@ class FirSyntheticPropertiesScope(
val parameter = setter.valueParameters.singleOrNull() ?: return
if (setter.typeParameters.isNotEmpty() || setter.isStatic) return
val parameterType = (parameter.returnTypeRef as? FirResolvedTypeRef)?.type ?: return
if (getterReturnType.withNullability(NOT_NULL) != parameterType.withNullability(NOT_NULL)) {
return
if (getter.symbol.callableId.classId == setter.symbol.callableId.classId) {
if (getterReturnType.withNullability(NOT_NULL) != parameterType.withNullability(NOT_NULL)) {
return
}
} else {
// TODO: at this moment it works for cases like
// class Base {
// void setSomething(Object value) {}
// }
// class Derived extends Base {
// String getSomething() { return ""; }
// }
// In FE 1.0, we should have also Object getSomething() in class Base for this to work
// I think details here are worth designing
if (!AbstractTypeChecker.isSubtypeOf(
session.typeContext,
getterReturnType.withNullability(NOT_NULL),
parameterType.withNullability(NOT_NULL)
)
) {
return
}
}
matchingSetter = setter
})
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// FILE: JavaClass2.java
class JavaClass1 {
@@ -5,7 +5,7 @@ fun foo(o: JavaClass2) {
o.something = ""
o.setSomething(1)
o.something = 1 // we generate extension property for JavaClass2 with more specific type
o.<!VARIABLE_EXPECTED!>something<!> += "1"
o.something += "1"
}
fun useString(i: String) {}
+11
View File
@@ -0,0 +1,11 @@
FILE fqName:<root> fileName:/kt42496.kt
FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String
BLOCK_BODY
VAR name:javaClass type:<root>.JavaClass2 [val]
CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.JavaClass2' type=<root>.JavaClass2 origin=null
CALL 'public open fun setSomething (value: kotlin.Any?): kotlin.Unit declared in <root>.JavaClass1' type=kotlin.Unit origin=EQ
$this: GET_VAR 'val javaClass: <root>.JavaClass2 [val] declared in <root>.box' type=<root>.JavaClass2 origin=null
value: CONST String type=kotlin.String value="OK"
RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in <root>'
CALL 'public open fun getSomething (): kotlin.String? declared in <root>.JavaClass2' type=kotlin.String? origin=GET_PROPERTY
$this: GET_VAR 'val javaClass: <root>.JavaClass2 [val] declared in <root>.box' type=<root>.JavaClass2 origin=null