diff --git a/analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/contextReceiver.descriptors.rendered b/analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/contextReceiver.descriptors.rendered
deleted file mode 100644
index 46a7825af31..00000000000
--- a/analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/contextReceiver.descriptors.rendered
+++ /dev/null
@@ -1,37 +0,0 @@
-interface Context1
-
-interface Context2 {
- fun getContextElement(): A
-}
-
-class Context2Impl(value: A) : Context2 {
- override fun getContextElement(): A
-
- val value: A
-}
-
-context(Int, String)
-class A {
- constructor(int: Int)
-}
-
-context(Context1, c2@Context2)
-class B {
- override fun toString(): String
-}
-
-context(Context2)
-class C {
- val a: A
- get()
-}
-
-context(Context1)
-@Deprecated(message = "Use `B` instead.")
-class D
-
-context(b@B, `fun`@A)
-fun foo()
-
-context(Context2>)
-fun bar(): ERROR
diff --git a/analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/contextReceiver.kt b/analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/contextReceiver.kt
index ef71325d75c..b8cbfe18a08 100644
--- a/analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/contextReceiver.kt
+++ b/analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/contextReceiver.kt
@@ -1,3 +1,5 @@
+// !LANGUAGE: +ContextReceivers
+
interface Context1
interface Context2 {
diff --git a/compiler/fir/analysis-tests/testData/resolve/expresssions/propertyAccessWithExplicitTypeArguments.kt b/compiler/fir/analysis-tests/testData/resolve/expresssions/propertyAccessWithExplicitTypeArguments.kt
index 7a5f19714e2..13379db3428 100644
--- a/compiler/fir/analysis-tests/testData/resolve/expresssions/propertyAccessWithExplicitTypeArguments.kt
+++ b/compiler/fir/analysis-tests/testData/resolve/expresssions/propertyAccessWithExplicitTypeArguments.kt
@@ -1,4 +1,5 @@
// ISSUE: KT-54978
+// !LANGUAGE: +ContextReceivers
// Case 1: Parameters and local variables
fun f1(x: Int) {
diff --git a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt
index 7715d6a83b5..538369002c0 100644
--- a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt
+++ b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt
@@ -627,7 +627,7 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
parameter("contextReceiverRepresentation")
}
val AMBIGUOUS_CALL_WITH_IMPLICIT_CONTEXT_RECEIVER by error(PositioningStrategy.REFERENCE_BY_QUALIFIED)
- val UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL by error()
+ val UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL by error(PositioningStrategy.NAME_IDENTIFIER)
}
val TYPES_AND_TYPE_PARAMETERS by object : DiagnosticGroup("Types & type parameters") {
diff --git a/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt b/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt
index 5ed2d66378b..4b5f84397c9 100644
--- a/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt
+++ b/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt
@@ -397,7 +397,7 @@ object FirErrors {
val NO_CONTEXT_RECEIVER by error1(SourceElementPositioningStrategies.REFERENCE_BY_QUALIFIED)
val MULTIPLE_ARGUMENTS_APPLICABLE_FOR_CONTEXT_RECEIVER by error1(SourceElementPositioningStrategies.REFERENCE_BY_QUALIFIED)
val AMBIGUOUS_CALL_WITH_IMPLICIT_CONTEXT_RECEIVER by error0(SourceElementPositioningStrategies.REFERENCE_BY_QUALIFIED)
- val UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL by error0()
+ val UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL by error0(SourceElementPositioningStrategies.NAME_IDENTIFIER)
// Types & type parameters
val RECURSION_IN_IMPLICIT_TYPES by error0()
diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt
index c944f71c937..bb077fef84a 100644
--- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt
+++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt
@@ -268,6 +268,8 @@ private fun mapInapplicableCandidateError(
rootCause.expectedContextReceiverType.removeTypeVariableTypes(typeContext)
)
+ is UnsupportedContextualDeclarationCall -> FirErrors.UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL.createOn(source)
+
is AmbiguousValuesForContextReceiverParameter ->
FirErrors.MULTIPLE_ARGUMENTS_APPLICABLE_FOR_CONTEXT_RECEIVER.createOn(
qualifiedAccessSource ?: source,
diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt
index 695165a7eb4..376e0bc8dc4 100644
--- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt
+++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt
@@ -202,6 +202,11 @@ object CheckContextReceivers : ResolutionStage() {
candidate.substitutor.substituteOrSelf(it.typeRef.coneType)
}?.takeUnless { it.isEmpty() } ?: return
+ if (!context.session.languageVersionSettings.supportsFeature(LanguageFeature.ContextReceivers)) {
+ sink.reportDiagnostic(UnsupportedContextualDeclarationCall)
+ return
+ }
+
val receiverGroups: List> =
context.bodyResolveContext.towerDataContext.towerDataElements.asReversed().mapNotNull { towerDataElement ->
towerDataElement.implicitReceiver?.receiverExpression?.let(::listOf)
diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionDiagnostic.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionDiagnostic.kt
index ae489fe290c..3fb919d1747 100644
--- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionDiagnostic.kt
+++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionDiagnostic.kt
@@ -137,6 +137,8 @@ class NoApplicableValueForContextReceiver(
val expectedContextReceiverType: ConeKotlinType
) : ResolutionDiagnostic(INAPPLICABLE)
+object UnsupportedContextualDeclarationCall : ResolutionDiagnostic(INAPPLICABLE)
+
class AmbiguousValuesForContextReceiverParameter(
val expectedContextReceiverType: ConeKotlinType,
) : ResolutionDiagnostic(INAPPLICABLE)
diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/unsupported.fir.kt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/unsupported.fir.kt
index d8e2bd1bdaa..6dcf2eaa247 100644
--- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/unsupported.fir.kt
+++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/unsupported.fir.kt
@@ -29,11 +29,11 @@ fun useWithContextReceivers() {
with(42) {
with("") {
f({}, 42)
- sameAsFWithoutNonContextualCounterpart({}, 42)
- p
- val a = A()
- a.p
- a.m()
+ sameAsFWithoutNonContextualCounterpart({}, 42)
+ p
+ val a = A()
+ a.p
+ a.m()
}
}
}