Account for context class receivers when creating the ExpressionReceiver

^KT-54084 Fixed
This commit is contained in:
Nikita Nazarov
2022-09-19 03:46:42 +03:00
committed by Nikita Nazarov
parent f578381726
commit bd742bb95c
12 changed files with 227 additions and 4 deletions
@@ -793,6 +793,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/declarations/contextReceivers/compoundAssignmentOperators.kt");
}
@Test
@TestMetadata("contextReceiverMethod.kt")
public void testContextReceiverMethod() throws Exception {
runTest("compiler/testData/ir/irText/declarations/contextReceivers/contextReceiverMethod.kt");
}
@Test
@TestMetadata("contextualFunctionConversion.kt")
public void testContextualFunctionConversion() throws Exception {
@@ -793,6 +793,12 @@ public class LightTreeFir2IrTextTestGenerated extends AbstractLightTreeFir2IrTex
runTest("compiler/testData/ir/irText/declarations/contextReceivers/compoundAssignmentOperators.kt");
}
@Test
@TestMetadata("contextReceiverMethod.kt")
public void testContextReceiverMethod() throws Exception {
runTest("compiler/testData/ir/irText/declarations/contextReceivers/contextReceiverMethod.kt");
}
@Test
@TestMetadata("contextualFunctionConversion.kt")
public void testContextualFunctionConversion() throws Exception {
@@ -65,7 +65,7 @@ interface ExpressionReceiver : ReceiverValue {
if (referenceExpression != null) {
val descriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, referenceExpression)
if (descriptor is ClassDescriptor) {
if (descriptor is ClassDescriptor && !referenceExpression.isContextClassReceiverReference(bindingContext)) {
return ThisExpressionClassReceiver(descriptor.original, expression, type, original = null)
}
} else if (expression is KtSuperExpression) {
@@ -78,5 +78,8 @@ interface ExpressionReceiver : ReceiverValue {
return ExpressionReceiverImpl(expression, type, original = null)
}
private fun KtReferenceExpression.isContextClassReceiverReference(bindingContext: BindingContext): Boolean =
bindingContext[BindingContext.THIS_REFERENCE_TARGET, this]?.value is ContextClassReceiver
}
}
@@ -0,0 +1,61 @@
FILE fqName:<root> fileName:/contextReceiverMethod.kt
CLASS CLASS name:Context modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Context
CONSTRUCTOR visibility:public <> () returnType:<root>.Context [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Context modality:FINAL visibility:public superTypes:[kotlin.Any]'
FUN name:foo visibility:public modality:FINAL <> ($this:<root>.Context) returnType:kotlin.Int
$this: VALUE_PARAMETER name:<this> type:<root>.Context
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun foo (): kotlin.Int declared in <root>.Context'
CONST Int type=kotlin.Int value=1
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
CLASS CLASS name:Test modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Test
FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:<root>.Context visibility:private [final]
CONSTRUCTOR visibility:public <> (_context_receiver_0:<root>.Context) returnType:<root>.Test [primary]
VALUE_PARAMETER name:_context_receiver_0 index:0 type:<root>.Context
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
SET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:<root>.Context visibility:private [final]' type=kotlin.Unit origin=null
receiver: GET_VAR '<this>: <root>.Test declared in <root>.Test' type=<root>.Test origin=null
value: GET_VAR '_context_receiver_0: <root>.Context declared in <root>.Test.<init>' type=<root>.Context origin=null
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test modality:FINAL visibility:public superTypes:[kotlin.Any]'
FUN name:foo visibility:public modality:FINAL <> ($this:<root>.Test) returnType:kotlin.Int
$this: VALUE_PARAMETER name:<this> type:<root>.Test
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun foo (): kotlin.Int declared in <root>.Test'
CONST Int type=kotlin.Int value=2
FUN name:bar visibility:public modality:FINAL <> ($this:<root>.Test) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.Test
BLOCK_BODY
VAR name:x type:kotlin.Int [val]
CALL 'public final fun foo (): kotlin.Int declared in <root>.Context' type=kotlin.Int origin=null
$this: GET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:<root>.Context visibility:private [final]' type=<root>.Context origin=null
receiver: GET_VAR '<this>: <root>.Test declared in <root>.Test.bar' type=<root>.Test origin=null
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
@@ -0,0 +1,31 @@
class Context {
constructor() /* primary */ {
super/*Any*/()
/* <init>() */
}
fun foo(): Int {
return 1
}
}
class Test {
private /* final field */ val contextReceiverField0: Context
constructor(_context_receiver_0: Context) /* primary */ {
super/*Any*/()
<this>.#contextReceiverField0 = _context_receiver_0
/* <init>() */
}
fun foo(): Int {
return 2
}
fun bar() {
val x: Int = <this>.#contextReceiverField0.foo()
}
}
@@ -0,0 +1,61 @@
FILE fqName:<root> fileName:/contextReceiverMethod.kt
CLASS CLASS name:Context modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Context
CONSTRUCTOR visibility:public <> () returnType:<root>.Context [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Context modality:FINAL visibility:public superTypes:[kotlin.Any]'
FUN name:foo visibility:public modality:FINAL <> ($this:<root>.Context) returnType:kotlin.Int
$this: VALUE_PARAMETER name:<this> type:<root>.Context
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun foo (): kotlin.Int declared in <root>.Context'
CONST Int type=kotlin.Int value=1
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
CLASS CLASS name:Test modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Test
FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:<root>.Context visibility:private [final]
CONSTRUCTOR visibility:public <> (<this>:<root>.Context) returnType:<root>.Test [primary]
VALUE_PARAMETER name:<this> index:0 type:<root>.Context
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
SET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:<root>.Context visibility:private [final]' type=kotlin.Unit origin=null
receiver: GET_VAR '<this>: <root>.Test declared in <root>.Test' type=<root>.Test origin=null
value: GET_VAR '<this>: <root>.Context declared in <root>.Test.<init>' type=<root>.Context origin=null
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test modality:FINAL visibility:public superTypes:[kotlin.Any]'
FUN name:foo visibility:public modality:FINAL <> ($this:<root>.Test) returnType:kotlin.Int
$this: VALUE_PARAMETER name:<this> type:<root>.Test
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun foo (): kotlin.Int declared in <root>.Test'
CONST Int type=kotlin.Int value=2
FUN name:bar visibility:public modality:FINAL <> ($this:<root>.Test) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.Test
BLOCK_BODY
VAR name:x type:kotlin.Int [val]
CALL 'public final fun foo (): kotlin.Int declared in <root>.Context' type=kotlin.Int origin=null
$this: GET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:<root>.Context visibility:private [final]' type=<root>.Context origin=null
receiver: GET_VAR '<this>: <root>.Test declared in <root>.Test.bar' type=<root>.Test origin=null
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
@@ -0,0 +1,13 @@
// !LANGUAGE: +ContextReceivers
class Context {
fun foo() = 1
}
context(Context)
class Test {
fun foo() = 2
fun bar() {
val x = this@Context.foo()
}
}
@@ -0,0 +1,31 @@
class Context {
constructor() /* primary */ {
super/*Any*/()
/* <init>() */
}
fun foo(): Int {
return 1
}
}
class Test {
private /* final field */ val contextReceiverField0: Context
constructor(<this>: Context) /* primary */ {
super/*Any*/()
<this>.#contextReceiverField0 = <this>
/* <init>() */
}
fun foo(): Int {
return 2
}
fun bar() {
val x: Int = <this>.#contextReceiverField0.foo()
}
}
@@ -45,5 +45,4 @@ FILE fqName:<root> fileName:/property.kt
$this: CALL 'public abstract fun a (): kotlin.Int declared in <root>.A' type=kotlin.Int origin=null
$this: GET_VAR '<this>: <root>.A declared in <root>.<get-c>' type=<root>.A origin=null
other: CALL 'public abstract fun b (): kotlin.Int declared in <root>.B' type=kotlin.Int origin=null
$this: TYPE_OP type=<root>.B origin=IMPLICIT_CAST typeOperand=<root>.B
GET_VAR '<this>: <root>.B declared in <root>.<get-c>' type=<root>.A origin=null
$this: GET_VAR '<this>: <root>.B declared in <root>.<get-c>' type=<root>.B origin=null
@@ -10,5 +10,6 @@ interface B {
val c: Int
get(<this>: A, <this>: B): Int {
return <this>.a().plus(other = <this> /*as B */.b())
return <this>.a().plus(other = <this>.b())
}
@@ -793,6 +793,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest {
runTest("compiler/testData/ir/irText/declarations/contextReceivers/compoundAssignmentOperators.kt");
}
@Test
@TestMetadata("contextReceiverMethod.kt")
public void testContextReceiverMethod() throws Exception {
runTest("compiler/testData/ir/irText/declarations/contextReceivers/contextReceiverMethod.kt");
}
@Test
@TestMetadata("contextualFunctionConversion.kt")
public void testContextualFunctionConversion() throws Exception {
@@ -613,6 +613,11 @@ public class KlibTextTestCaseGenerated extends AbstractKlibTextTestCase {
runTest("compiler/testData/ir/irText/declarations/contextReceivers/class.kt");
}
@TestMetadata("contextReceiverMethod.kt")
public void testContextReceiverMethod() throws Exception {
runTest("compiler/testData/ir/irText/declarations/contextReceivers/contextReceiverMethod.kt");
}
@TestMetadata("contextualPrimaryConstructorWithParams.kt")
public void testContextualPrimaryConstructorWithParams() throws Exception {
runTest("compiler/testData/ir/irText/declarations/contextReceivers/contextualPrimaryConstructorWithParams.kt");