FIR2IR: fix smart casts in case field is accessed

This commit is contained in:
Mikhail Glukhikh
2021-01-25 13:49:40 +03:00
parent 1c9064836e
commit 1a4b327210
8 changed files with 189 additions and 6 deletions
@@ -2140,6 +2140,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/firProblems/InnerClassInAnonymous.kt");
}
@Test
@TestMetadata("JCTree.kt")
public void testJCTree() throws Exception {
runTest("compiler/testData/ir/irText/firProblems/JCTree.kt");
}
@Test
@TestMetadata("kt43342.kt")
public void testKt43342() throws Exception {
@@ -22,10 +22,7 @@ import org.jetbrains.kotlin.fir.resolve.scope
import org.jetbrains.kotlin.fir.scopes.FakeOverrideTypeCalculator
import org.jetbrains.kotlin.fir.scopes.FirTypeScope
import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirAnonymousFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.visitors.FirDefaultVisitor
import org.jetbrains.kotlin.ir.IrElement
@@ -298,7 +295,7 @@ class Fir2IrImplicitCastInserter(
return implicitCastOrExpression(value, castTypeRef)
}
val referencedSymbol = calleeReference.resolvedSymbol
if (referencedSymbol !is FirPropertySymbol && referencedSymbol !is FirFunctionSymbol) {
if (referencedSymbol !is FirPropertySymbol && referencedSymbol !is FirFunctionSymbol && referencedSymbol !is FirFieldSymbol) {
return implicitCastOrExpression(value, castTypeRef)
}
@@ -339,7 +336,7 @@ class Fir2IrImplicitCastInserter(
}
}
when (referencedSymbol) {
is FirPropertySymbol -> scope.processPropertiesByName(name, processor)
is FirPropertySymbol, is FirFieldSymbol -> scope.processPropertiesByName(name, processor)
is FirFunctionSymbol -> scope.processFunctionsByName(name, processor)
}
return result
@@ -0,0 +1,21 @@
class Owner<out T : JCTree> {
constructor(tree: T) /* primary */ {
super/*Any*/()
/* <init>() */
}
val tree: T
field = tree
get
val foo: String
get(): String {
var tree: JCTree = <this>.<get-tree>()
when {
tree /*as T */ is JCTypeApply -> return tree /*as JCTypeApply */.#clazz /*!! String */
}
return ""
}
}
+53
View File
@@ -0,0 +1,53 @@
FILE fqName:<root> fileName:/JCTreeUser.kt
CLASS CLASS name:Owner modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Owner<T of <root>.Owner>
TYPE_PARAMETER name:T index:0 variance:out superTypes:[<root>.JCTree]
CONSTRUCTOR visibility:public <> (tree:T of <root>.Owner) returnType:<root>.Owner<T of <root>.Owner> [primary]
VALUE_PARAMETER name:tree index:0 type:T of <root>.Owner
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Owner modality:FINAL visibility:public superTypes:[kotlin.Any]'
PROPERTY name:tree visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:tree type:T of <root>.Owner visibility:private [final]
EXPRESSION_BODY
GET_VAR 'tree: T of <root>.Owner declared in <root>.Owner.<init>' type=T of <root>.Owner origin=INITIALIZE_PROPERTY_FROM_PARAMETER
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-tree> visibility:public modality:FINAL <> ($this:<root>.Owner<T of <root>.Owner>) returnType:T of <root>.Owner
correspondingProperty: PROPERTY name:tree visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.Owner<T of <root>.Owner>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-tree> (): T of <root>.Owner declared in <root>.Owner'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:tree type:T of <root>.Owner visibility:private [final]' type=T of <root>.Owner origin=null
receiver: GET_VAR '<this>: <root>.Owner<T of <root>.Owner> declared in <root>.Owner.<get-tree>' type=<root>.Owner<T of <root>.Owner> origin=null
PROPERTY name:foo visibility:public modality:FINAL [val]
FUN name:<get-foo> visibility:public modality:FINAL <> ($this:<root>.Owner<T of <root>.Owner>) returnType:kotlin.String
correspondingProperty: PROPERTY name:foo visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.Owner<T of <root>.Owner>
BLOCK_BODY
VAR name:tree type:<root>.JCTree [var]
CALL 'public final fun <get-tree> (): T of <root>.Owner declared in <root>.Owner' type=T of <root>.Owner origin=GET_PROPERTY
$this: GET_VAR '<this>: <root>.Owner<T of <root>.Owner> declared in <root>.Owner.<get-foo>' type=<root>.Owner<T of <root>.Owner> origin=null
WHEN type=kotlin.Unit origin=IF
BRANCH
if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=<root>.JCTree.JCTypeApply
TYPE_OP type=T of <root>.Owner origin=IMPLICIT_CAST typeOperand=T of <root>.Owner
GET_VAR 'var tree: <root>.JCTree [var] declared in <root>.Owner.<get-foo>' type=<root>.JCTree origin=null
then: RETURN type=kotlin.Nothing from='public final fun <get-foo> (): kotlin.String declared in <root>.Owner'
TYPE_OP type=kotlin.String origin=IMPLICIT_NOTNULL typeOperand=kotlin.String
GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:clazz type:kotlin.String? visibility:public' type=kotlin.String? origin=GET_PROPERTY
receiver: TYPE_OP type=<root>.JCTree.JCTypeApply origin=IMPLICIT_CAST typeOperand=<root>.JCTree.JCTypeApply
GET_VAR 'var tree: <root>.JCTree [var] declared in <root>.Owner.<get-foo>' type=<root>.JCTree origin=null
RETURN type=kotlin.Nothing from='public final fun <get-foo> (): kotlin.String declared in <root>.Owner'
CONST String type=kotlin.String value=""
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
+24
View File
@@ -0,0 +1,24 @@
// FILE: JCTree.java
public class JCTree {
public abstract static class JCExpression extends JCTree {
}
public static class JCTypeApply extends JCExpression {
public String clazz = "OK";
}
}
// FILE: JCTreeUser.kt
class Owner<out T : JCTree>(val tree: T) {
val foo: String
get() {
var tree: JCTree = tree
if (tree is JCTree.JCTypeApply) {
return tree.clazz
}
return "";
}
}
+23
View File
@@ -0,0 +1,23 @@
class Owner<out T : JCTree> {
constructor(tree: T) /* primary */ {
super/*Any*/()
/* <init>() */
}
val tree: T
field = tree
get
val foo: String
get(): String {
var tree: JCTree = <this>.<get-tree>()
when {
tree is JCTypeApply -> { // BLOCK
return tree /*as JCTypeApply */super.#clazz /*!! String */
}
}
return ""
}
}
+53
View File
@@ -0,0 +1,53 @@
FILE fqName:<root> fileName:/JCTreeUser.kt
CLASS CLASS name:Owner modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Owner<T of <root>.Owner>
TYPE_PARAMETER name:T index:0 variance:out superTypes:[<root>.JCTree]
CONSTRUCTOR visibility:public <> (tree:T of <root>.Owner) returnType:<root>.Owner<T of <root>.Owner> [primary]
VALUE_PARAMETER name:tree index:0 type:T of <root>.Owner
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Owner modality:FINAL visibility:public superTypes:[kotlin.Any]'
PROPERTY name:tree visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:tree type:T of <root>.Owner visibility:private [final]
EXPRESSION_BODY
GET_VAR 'tree: T of <root>.Owner declared in <root>.Owner.<init>' type=T of <root>.Owner origin=INITIALIZE_PROPERTY_FROM_PARAMETER
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-tree> visibility:public modality:FINAL <> ($this:<root>.Owner<T of <root>.Owner>) returnType:T of <root>.Owner
correspondingProperty: PROPERTY name:tree visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.Owner<T of <root>.Owner>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-tree> (): T of <root>.Owner declared in <root>.Owner'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:tree type:T of <root>.Owner visibility:private [final]' type=T of <root>.Owner origin=null
receiver: GET_VAR '<this>: <root>.Owner<T of <root>.Owner> declared in <root>.Owner.<get-tree>' type=<root>.Owner<T of <root>.Owner> origin=null
PROPERTY name:foo visibility:public modality:FINAL [val]
FUN name:<get-foo> visibility:public modality:FINAL <> ($this:<root>.Owner<T of <root>.Owner>) returnType:kotlin.String
correspondingProperty: PROPERTY name:foo visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.Owner<T of <root>.Owner>
BLOCK_BODY
VAR name:tree type:<root>.JCTree [var]
CALL 'public final fun <get-tree> (): T of <root>.Owner declared in <root>.Owner' type=T of <root>.Owner origin=GET_PROPERTY
$this: GET_VAR '<this>: <root>.Owner<T of <root>.Owner> declared in <root>.Owner.<get-foo>' type=<root>.Owner<T of <root>.Owner> origin=null
WHEN type=kotlin.Unit origin=IF
BRANCH
if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=<root>.JCTree.JCTypeApply
GET_VAR 'var tree: <root>.JCTree [var] declared in <root>.Owner.<get-foo>' type=<root>.JCTree origin=null
then: BLOCK type=kotlin.Unit origin=null
RETURN type=kotlin.Nothing from='public final fun <get-foo> (): kotlin.String declared in <root>.Owner'
TYPE_OP type=kotlin.String origin=IMPLICIT_NOTNULL typeOperand=kotlin.String
GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:clazz type:@[FlexibleNullability] kotlin.String? visibility:public' type=@[FlexibleNullability] kotlin.String? origin=GET_PROPERTY
receiver: TYPE_OP type=<root>.JCTree.JCTypeApply origin=IMPLICIT_CAST typeOperand=<root>.JCTree.JCTypeApply
GET_VAR 'var tree: <root>.JCTree [var] declared in <root>.Owner.<get-foo>' type=<root>.JCTree origin=null
RETURN type=kotlin.Nothing from='public final fun <get-foo> (): kotlin.String declared in <root>.Owner'
CONST String type=kotlin.String value=""
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
@@ -2140,6 +2140,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest {
runTest("compiler/testData/ir/irText/firProblems/InnerClassInAnonymous.kt");
}
@Test
@TestMetadata("JCTree.kt")
public void testJCTree() throws Exception {
runTest("compiler/testData/ir/irText/firProblems/JCTree.kt");
}
@Test
@TestMetadata("kt43342.kt")
public void testKt43342() throws Exception {