From 3c68b27280f0bbf9e0258886e9488bf3a5501ed7 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Tue, 17 Oct 2023 18:23:24 +0200 Subject: [PATCH] KT-62071 [AA] Do not throw error from `getScopeContextForPosition` when `implicitScope` of receiver value is `null` `implicitScope` can be `null` in case when the implicit receiver resides in a user-defined `kotlin.*` package, but the user have not yet allowed this with compiler argument directive. In this case, we don't want the IDE to crush and show exceptions - the `kotlin` package would be highlighted by the compiler diagnostics and other resolve problems, and that would be enough ^KT-62071 Fixed --- ...eScopeContextForPositionTestGenerated.java | 6 ++ ...eScopeContextForPositionTestGenerated.java | 6 ++ ...eScopeContextForPositionTestGenerated.java | 6 ++ .../notEnabledKotlinPackage.kt | 9 +++ .../notEnabledKotlinPackage.pretty.txt | 51 +++++++++++++ .../notEnabledKotlinPackage.txt | 72 +++++++++++++++++++ .../fir/declarations/ImplicitReceiverUtils.kt | 15 ++-- 7 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.kt create mode 100644 analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.pretty.txt create mode 100644 analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.txt diff --git a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGenerated.java b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGenerated.java index 714d2fc67c0..f436bdbfe26 100644 --- a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGenerated.java +++ b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGenerated.java @@ -100,6 +100,12 @@ public class FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGener runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.kt"); } + @Test + @TestMetadata("notEnabledKotlinPackage.kt") + public void testNotEnabledKotlinPackage() throws Exception { + runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.kt"); + } + @Test @TestMetadata("simpleScopeContextForPosition.kt") public void testSimpleScopeContextForPosition() throws Exception { diff --git a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java index 5d06a3b8021..a2d2eb3a503 100644 --- a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java +++ b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/scopeProvider/FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java @@ -100,6 +100,12 @@ public class FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerate runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.kt"); } + @Test + @TestMetadata("notEnabledKotlinPackage.kt") + public void testNotEnabledKotlinPackage() throws Exception { + runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.kt"); + } + @Test @TestMetadata("simpleScopeContextForPosition.kt") public void testSimpleScopeContextForPosition() throws Exception { diff --git a/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java b/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java index 2faad208720..4560dc60384 100644 --- a/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java +++ b/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/scopeProvider/FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestGenerated.java @@ -100,6 +100,12 @@ public class FirStandaloneNormalAnalysisSourceModuleScopeContextForPositionTestG runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/localTypeScope.kt"); } + @Test + @TestMetadata("notEnabledKotlinPackage.kt") + public void testNotEnabledKotlinPackage() throws Exception { + runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.kt"); + } + @Test @TestMetadata("simpleScopeContextForPosition.kt") public void testSimpleScopeContextForPosition() throws Exception { diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.kt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.kt new file mode 100644 index 00000000000..a6074cb103a --- /dev/null +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.kt @@ -0,0 +1,9 @@ +// FILE: main.kt +package kotlin.myPackage + +class SomeClass { + fun test(param: String) { + param + } +} + diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.pretty.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.pretty.txt new file mode 100644 index 00000000000..2404c304b07 --- /dev/null +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.pretty.txt @@ -0,0 +1,51 @@ +element: param +implicit receivers: + type: kotlin.myPackage.SomeClass + owner symbol: KtFirNamedClassOrObjectSymbol + +scopes: + LocalScope, index = 0 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + LocalScope, index = 1 + packages: 0 + classifiers: 0 + callables: 1 + param: kotlin.String + constructors: 0 + + TypeScope, index = 2 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + ExplicitSimpleImportingScope, index = 3 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + PackageMemberScope, index = 4 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + DefaultSimpleImportingScope, index = 5 + + DefaultSimpleImportingScope, index = 6 + + ExplicitStarImportingScope, index = 7 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + DefaultSimpleImportingScope, index = 8 + + DefaultStarImportingScope, index = 9 + diff --git a/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.txt b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.txt new file mode 100644 index 00000000000..ebb68f3aa01 --- /dev/null +++ b/analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/notEnabledKotlinPackage.txt @@ -0,0 +1,72 @@ +element: param +implicit receivers: + type: KtClassErrorType: + annotationsList: [] + type: ERROR_TYPE + owner symbol: KtFirNamedClassOrObjectSymbol + +scopes: + LocalScope, index = 0 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + LocalScope, index = 1 + packages: 0 + classifiers: 0 + callables: 1 + KtValueParameterSymbol: + annotationsList: [] + callableIdIfNonLocal: null + contextReceivers: [] + generatedPrimaryConstructorProperty: null + hasDefaultValue: false + isCrossinline: false + isExtension: false + isImplicitLambdaParameter: false + isNoinline: false + isVararg: false + name: param + origin: SOURCE + receiverParameter: null + returnType: KtUsualClassType: + annotationsList: [] + ownTypeArguments: [] + type: kotlin/String + symbolKind: LOCAL + typeParameters: [] + constructors: 0 + + TypeScope, index = 2 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + ExplicitSimpleImportingScope, index = 3 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + PackageMemberScope, index = 4 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + DefaultSimpleImportingScope, index = 5 + + DefaultSimpleImportingScope, index = 6 + + ExplicitStarImportingScope, index = 7 + packages: 0 + classifiers: 0 + callables: 0 + constructors: 0 + + DefaultSimpleImportingScope, index = 8 + + DefaultStarImportingScope, index = 9 + diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/declarations/ImplicitReceiverUtils.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/declarations/ImplicitReceiverUtils.kt index 1929ba6f545..fdcff7f9002 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/declarations/ImplicitReceiverUtils.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/declarations/ImplicitReceiverUtils.kt @@ -282,13 +282,14 @@ class FirTowerDataElement( private fun ImplicitReceiverValue<*>.getImplicitScope( processTypeScope: FirTypeScope.(ConeKotlinType) -> FirTypeScope, ): FirScope { - return when (val type = type.fullyExpandedType(useSiteSession)) { - is ConeErrorType, - is ConeStubType -> FirTypeScope.Empty - else -> implicitScope?.processTypeScope(type) ?: errorWithAttachment("Scope for type ${type::class.simpleName} is null") { - withConeTypeEntry("type", type) - } - } + // N.B.: implicitScope == null when the type sits in a user-defined 'kotlin' package, + // but there is no '-Xallow-kotlin-package' compiler argument provided + val implicitScope = implicitScope ?: return FirTypeScope.Empty + + val type = type.fullyExpandedType(useSiteSession) + if (type is ConeErrorType || type is ConeStubType) return FirTypeScope.Empty + + return implicitScope.processTypeScope(type) } }