[FIR] Report UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL

^KT-59969 Fixed
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 46a7825..0000000
--- a/analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/contextReceiver.descriptors.rendered
+++ /dev/null
@@ -1,37 +0,0 @@
-interface Context1
-
-interface Context2<A> {
-  fun getContextElement(): A
-}
-
-class Context2Impl<A>(value: A) : Context2<A> {
-  override fun getContextElement(): A
-
-  val value: A
-}
-
-context(Int, String)
-class A {
-  constructor(int: Int)
-}
-
-context(Context1, c2@Context2<String>)
-class B {
-  override fun toString(): String
-}
-
-context(Context2<A>)
-class C<A> {
-  val a: A
-    get()
-}
-
-context(Context1)
-@Deprecated(message = "Use `B` instead.")
-class D
-
-context(b@B, `fun`@A)
-fun foo()
-
-context(Context2<C<String>>)
-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 ef71325..b8cbfe1 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<A> {
diff --git a/compiler/fir/analysis-tests/testData/resolve/expresssions/propertyAccessWithExplicitTypeArguments.kt b/compiler/fir/analysis-tests/testData/resolve/expresssions/propertyAccessWithExplicitTypeArguments.kt
index 7a5f197..13379db 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 867b188..ba5ff50 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
@@ -623,7 +623,7 @@
             parameter<ConeKotlinType>("contextReceiverRepresentation")
         }
         val AMBIGUOUS_CALL_WITH_IMPLICIT_CONTEXT_RECEIVER by error<KtElement>(PositioningStrategy.REFERENCE_BY_QUALIFIED)
-        val UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL by error<KtElement>()
+        val UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL by error<KtElement>(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 bc01997..c56445f 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
@@ -395,7 +395,7 @@
     val NO_CONTEXT_RECEIVER by error1<KtElement, ConeKotlinType>(SourceElementPositioningStrategies.REFERENCE_BY_QUALIFIED)
     val MULTIPLE_ARGUMENTS_APPLICABLE_FOR_CONTEXT_RECEIVER by error1<KtElement, ConeKotlinType>(SourceElementPositioningStrategies.REFERENCE_BY_QUALIFIED)
     val AMBIGUOUS_CALL_WITH_IMPLICIT_CONTEXT_RECEIVER by error0<KtElement>(SourceElementPositioningStrategies.REFERENCE_BY_QUALIFIED)
-    val UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL by error0<KtElement>()
+    val UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL by error0<KtElement>(SourceElementPositioningStrategies.NAME_IDENTIFIER)
 
     // Types & type parameters
     val RECURSION_IN_IMPLICIT_TYPES by error0<PsiElement>()
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 c944f71..bb077fe 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 @@
                     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 695165a..376e0bc 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 @@
             candidate.substitutor.substituteOrSelf(it.typeRef.coneType)
         }?.takeUnless { it.isEmpty() } ?: return
 
+        if (!context.session.languageVersionSettings.supportsFeature(LanguageFeature.ContextReceivers)) {
+            sink.reportDiagnostic(UnsupportedContextualDeclarationCall)
+            return
+        }
+
         val receiverGroups: List<List<FirExpression>> =
             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 ae489fe..3fb919d 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 @@
     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 d8e2bd1..6dcf2ea 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 @@
     with(42) {
         with("") {
             f({}, 42)
-            sameAsFWithoutNonContextualCounterpart({}, 42)
-            p
-            val a = A()
-            a.p
-            a.m()
+            <!UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL!>sameAsFWithoutNonContextualCounterpart<!>({}, 42)
+            <!UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL!>p<!>
+            val a = <!UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL!>A<!>()
+            a.<!UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL!>p<!>
+            a.<!UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL!>m<!>()
         }
     }
 }