[FE] KT-50878 Prohibit using contextual declarations without -Xcontext-receivers

This commit is contained in:
Anastasia.Shadrina
2022-01-20 18:03:51 +07:00
committed by teamcity
parent c93594331b
commit a86b4d767e
8 changed files with 70 additions and 5 deletions
@@ -1299,6 +1299,7 @@ public interface Errors {
DiagnosticFactory1<KtElement, String> NO_CONTEXT_RECEIVER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<KtElement, String> MULTIPLE_ARGUMENTS_APPLICABLE_FOR_CONTEXT_RECEIVER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<KtElement> AMBIGUOUS_CALL_WITH_IMPLICIT_CONTEXT_RECEIVER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtElement> UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL = DiagnosticFactory0.create(ERROR);
// Error sets
ImmutableSet<? extends DiagnosticFactory<?>> UNRESOLVED_REFERENCE_DIAGNOSTICS = ImmutableSet.of(
@@ -1109,6 +1109,7 @@ public class DefaultErrorMessages {
MAP.put(NO_CONTEXT_RECEIVER, "No required context receiver found: {0}", TO_STRING);
MAP.put(MULTIPLE_ARGUMENTS_APPLICABLE_FOR_CONTEXT_RECEIVER, "Multiple arguments applicable for context receiver: {0}", TO_STRING);
MAP.put(AMBIGUOUS_CALL_WITH_IMPLICIT_CONTEXT_RECEIVER, "With implicit context receiver, call is ambiguous. Specify the receiver explicitly");
MAP.put(UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL, "To use contextual declarations, specify the `-Xcontext-receivers` compiler option");
MAP.setImmutable();
@@ -132,6 +132,10 @@ class DiagnosticReporterByTrackingStrategy(
val callElement = psiKotlinCall.psiCall.callElement
trace.report(AMBIGUOUS_CALL_WITH_IMPLICIT_CONTEXT_RECEIVER.on(callElement))
}
UnsupportedContextualDeclarationCall::class.java -> {
val callElement = psiKotlinCall.psiCall.callElement
trace.report(UNSUPPORTED_CONTEXTUAL_DECLARATION_CALL.on(callElement))
}
}
}
@@ -19,10 +19,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.*
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.resolve.calls.smartcasts.getReceiverValueWithSmartCast
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind.*
import org.jetbrains.kotlin.resolve.calls.tower.ContextReceiverAmbiguity
import org.jetbrains.kotlin.resolve.calls.tower.InfixCallNoInfixModifier
import org.jetbrains.kotlin.resolve.calls.tower.InvokeConventionCallNoOperatorModifier
import org.jetbrains.kotlin.resolve.calls.tower.VisibilityError
import org.jetbrains.kotlin.resolve.calls.tower.*
import org.jetbrains.kotlin.resolve.descriptorUtil.isInsideInterface
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo
@@ -857,6 +854,10 @@ internal object ErrorDescriptorResolutionPart : ResolutionPart() {
internal object CheckContextReceiversResolutionPart : ResolutionPart() {
override fun ResolutionCandidate.process(workIndex: Int) {
if (candidateDescriptor.contextReceiverParameters.isEmpty()) return
if (!callComponents.languageVersionSettings.supportsFeature(LanguageFeature.ContextReceivers)) {
addDiagnostic(UnsupportedContextualDeclarationCall())
return
}
val parentLexicalScopes = scopeTower.lexicalScope.parentsWithSelf.filterIsInstance<LexicalScope>()
val implicitReceiversGroups = mutableListOf<List<ReceiverValueWithSmartCastInfo>>()
for (scope in parentLexicalScopes) {
@@ -120,6 +120,12 @@ class ContextReceiverAmbiguity : ResolutionDiagnostic(RESOLVED_WITH_ERROR) {
}
}
class UnsupportedContextualDeclarationCall : ResolutionDiagnostic(RESOLVED_WITH_ERROR) {
override fun report(reporter: DiagnosticReporter) {
reporter.onCall(this)
}
}
// todo error for this access from nested class
class VisibilityError(val invisibleMember: DeclarationDescriptorWithVisibility) : ResolutionDiagnostic(RUNTIME_ERROR) {
override fun report(reporter: DiagnosticReporter) {
@@ -5,6 +5,17 @@ fun f(g: context(Any) () -> Unit, value: Any): context(A) () -> Unit {
return value as (context(A) () -> Unit)
}
fun f(g: () -> Unit, value: Any) : () -> Unit {
return g
}
context(Any)
fun sameAsFWithoutNonContextualCounterpart(g: () -> Unit, value: Any) : () -> Unit {
return g
}
context(Any) val p get() = 42
context(String, Int)
class A {
context(Any)
@@ -12,4 +23,17 @@ class A {
context(String, Int)
fun m() {}
}
}
fun useWithContextReceivers() {
with(42) {
with("") {
<!OVERLOAD_RESOLUTION_AMBIGUITY!>f<!>({}, 42)
sameAsFWithoutNonContextualCounterpart({}, 42)
p
val a = A()
a.p
a.m()
}
}
}
@@ -5,6 +5,17 @@ fun f(g: <!UNSUPPORTED_FEATURE!>context(Any)<!> () -> Unit, value: Any): <!UNSUP
return value as (<!UNSUPPORTED_FEATURE!>context(A)<!> () -> Unit)
}
fun f(g: () -> Unit, value: Any) : () -> Unit {
return g
}
<!UNSUPPORTED_FEATURE!>context(Any)<!>
fun sameAsFWithoutNonContextualCounterpart(g: () -> Unit, value: Any) : () -> Unit {
return g
}
<!UNSUPPORTED_FEATURE!>context(Any)<!> val p get() = 42
<!UNSUPPORTED_FEATURE!>context(String, Int)<!>
class A {
<!UNSUPPORTED_FEATURE!>context(Any)<!>
@@ -12,4 +23,17 @@ class A {
<!UNSUPPORTED_FEATURE!>context(String, Int)<!>
fun m() {}
}
fun useWithContextReceivers() {
with(42) {
with("") {
f({}, 42)
<!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()<!>
}
}
}
@@ -1,6 +1,10 @@
package
context(kotlin.Any) public val p: kotlin.Int
public fun f(/*0*/ g: () -> kotlin.Unit, /*1*/ value: kotlin.Any): () -> kotlin.Unit
context(kotlin.Any) public fun f(/*0*/ g: context(kotlin.Any) () -> kotlin.Unit, /*1*/ value: kotlin.Any): context(A) () -> kotlin.Unit
context(kotlin.Any) public fun sameAsFWithoutNonContextualCounterpart(/*0*/ g: () -> kotlin.Unit, /*1*/ value: kotlin.Any): () -> kotlin.Unit
public fun useWithContextReceivers(): kotlin.Unit
context(kotlin.String, kotlin.Int) public final class A {
public constructor A()