diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 2812c76612e..303fefe2101 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -10600,6 +10600,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/lazy.kt"); } + @Test + @TestMetadata("localDeclaration.kt") + public void testLocalDeclaration() throws Exception { + runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.kt"); + } + @Test @TestMetadata("manyReceivers.kt") public void testManyReceivers() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index e7f7b2e8448..4badf32113a 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -16169,6 +16169,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/inferGenericPropertyType.kt"); } + @Test + @TestMetadata("localDeclaration.kt") + public void testLocalDeclaration() throws Exception { + runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/localDeclaration.kt"); + } + @Test @TestMetadata("plusAssign.kt") public void testPlusAssign() throws Exception { diff --git a/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java b/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java index 0206ffe9bb5..dfa2aac42ad 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java +++ b/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java @@ -1094,6 +1094,9 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing { */ private boolean parseLocalDeclaration(boolean rollbackIfDefinitelyNotExpression, boolean isScriptTopLevel) { PsiBuilder.Marker decl = mark(); + if (atSet(CONTEXT_KEYWORD)) { + myKotlinParsing.parseContextReceiverList(); + } KotlinParsing.ModifierDetector detector = new KotlinParsing.ModifierDetector(); myKotlinParsing.parseModifierList(detector, DEFAULT, TokenSet.EMPTY); diff --git a/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinParsing.java b/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinParsing.java index 45e53dc64eb..caadc571717 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinParsing.java +++ b/compiler/psi/src/org/jetbrains/kotlin/parsing/KotlinParsing.java @@ -620,7 +620,7 @@ public class KotlinParsing extends AbstractKotlinParsing { * contextReceiverList * : "context" "(" (label? typeReference{","})+ ")" */ - private void parseContextReceiverList() { + public void parseContextReceiverList() { assert _at(CONTEXT_KEYWORD); PsiBuilder.Marker contextReceiverList = mark(); advance(); // CONTEXT_KEYWORD diff --git a/compiler/testData/codegen/box/extensionFunctions/contextReceivers/localDeclaration.kt b/compiler/testData/codegen/box/extensionFunctions/contextReceivers/localDeclaration.kt new file mode 100644 index 00000000000..e64ea2c6c4a --- /dev/null +++ b/compiler/testData/codegen/box/extensionFunctions/contextReceivers/localDeclaration.kt @@ -0,0 +1,14 @@ +// TARGET_BACKEND: JVM_IR +// IGNORE_BACKEND_FIR: JVM_IR + +interface A { + val ok get() = "OK" +} +class B : A + +fun box(): String { + context(A) fun result() = ok + return with(B()) { + result() + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.fir.kt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.fir.kt new file mode 100644 index 00000000000..24211a331bf --- /dev/null +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.fir.kt @@ -0,0 +1,28 @@ +interface A { + fun f() {} +} +class B : A + +fun testLocalFunction() { + context(A) + fun local() { + f() + } + with(B()) { + local() + } + local() +} + +fun testLocalClass() { + context(A) + class Local { + fun local() { + f() + } + } + with(B()) { + Local().local() + } + Local() +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.kt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.kt new file mode 100644 index 00000000000..f45179f58c0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.kt @@ -0,0 +1,28 @@ +interface A { + fun f() {} +} +class B : A + +fun testLocalFunction() { + context(A) + fun local() { + f() + } + with(B()) { + local() + } + local() +} + +fun testLocalClass() { + context(A) + class Local { + fun local() { + f() + } + } + with(B()) { + Local().local() + } + Local() +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.txt new file mode 100644 index 00000000000..93fc4b3fb44 --- /dev/null +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.txt @@ -0,0 +1,19 @@ +package + +public fun testLocalClass(): kotlin.Unit +public fun testLocalFunction(): kotlin.Unit + +public interface A { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open fun f(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class B : A { + public constructor B() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun f(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index c291f4c95d2..c7819d5f0c8 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -10606,6 +10606,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/lazy.kt"); } + @Test + @TestMetadata("localDeclaration.kt") + public void testLocalDeclaration() throws Exception { + runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/localDeclaration.kt"); + } + @Test @TestMetadata("manyReceivers.kt") public void testManyReceivers() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 68cf7bbdb9b..993b33fe099 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -16169,6 +16169,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/inferGenericPropertyType.kt"); } + @Test + @TestMetadata("localDeclaration.kt") + public void testLocalDeclaration() throws Exception { + runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/localDeclaration.kt"); + } + @Test @TestMetadata("plusAssign.kt") public void testPlusAssign() throws Exception {