[LL FIR] do not resolve declarations from script separately

It leads to exceptions because of race.
We shouldn't try to resolve a script and its content simultaneously,
because it is not thread-safe

^KT-60728
This commit is contained in:
Dmitrii Gridin
2023-07-26 21:13:15 +02:00
committed by Space Team
parent 6fa254c353
commit 8a906004e3
5 changed files with 183 additions and 6 deletions
@@ -272,11 +272,18 @@ fun FirElementWithResolveState.tryCollectDesignationWithFile(): FirDesignationWi
val firFile = getContainingFile() ?: return null
FirDesignationWithFile(path = emptyList(), this, firFile)
}
is FirDeclaration -> {
val path = collectDesignationPath(this) ?: return null
val firFile = path.lastOrNull()?.getContainingFile() ?: getContainingFile() ?: return null
FirDesignationWithFile(path, this, firFile)
val firScript = firFile.declarations.singleOrNull() as? FirScript
if (firScript != null) {
FirDesignationWithFile(path = emptyList(), firScript, firFile)
} else {
FirDesignationWithFile(path, this, firFile)
}
}
else -> unexpectedElementError<FirElementWithResolveState>(this)
}
}
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirSession
import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirSessionConfigurator
import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.llFirSession
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.base.AbstractLowLevelApiSingleFileTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.configurators.AnalysisApiFirScriptTestConfigurator
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.configurators.AnalysisApiFirSourceTestConfigurator
import org.jetbrains.kotlin.analysis.low.level.api.fir.useFirSessionConfigurator
import org.jetbrains.kotlin.fir.FirElement
@@ -40,8 +41,6 @@ import org.jetbrains.kotlin.test.services.assertions
* Check that every declaration is visited exactly one time during diagnostic collection
*/
abstract class AbstractDiagnosticTraversalCounterTest : AbstractLowLevelApiSingleFileTest() {
override val configurator = AnalysisApiFirSourceTestConfigurator(analyseInDependentSession = false)
override fun configureTest(builder: TestConfigurationBuilder) {
super.configureTest(builder)
builder.apply {
@@ -153,3 +152,11 @@ abstract class AbstractDiagnosticTraversalCounterTest : AbstractLowLevelApiSingl
}
}
}
abstract class AbstractSourceDiagnosticTraversalCounterTest : AbstractDiagnosticTraversalCounterTest() {
override val configurator = AnalysisApiFirSourceTestConfigurator(analyseInDependentSession = false)
}
abstract class AbstractScriptDiagnosticTraversalCounterTest : AbstractDiagnosticTraversalCounterTest() {
override val configurator get() = AnalysisApiFirScriptTestConfigurator
}
@@ -0,0 +1,158 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("analysis/low-level-api-fir/testdata/fileStructure")
@TestDataPath("$PROJECT_ROOT")
public class ScriptDiagnosticTraversalCounterTestGenerated extends AbstractScriptDiagnosticTraversalCounterTest {
@Test
public void testAllFilesPresentInFileStructure() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/low-level-api-fir/testdata/fileStructure"), Pattern.compile("^(.+)\\.(kts)$"), null, true);
}
@Test
@TestMetadata("classMemberPropertyScript.kts")
public void testClassMemberPropertyScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/classMemberPropertyScript.kts");
}
@Test
@TestMetadata("classScript.kts")
public void testClassScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/classScript.kts");
}
@Test
@TestMetadata("constructorsScript.kts")
public void testConstructorsScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/constructorsScript.kts");
}
@Test
@TestMetadata("danglingAnnotationClassLevelScript.kts")
public void testDanglingAnnotationClassLevelScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/danglingAnnotationClassLevelScript.kts");
}
@Test
@TestMetadata("declarationsInPropertyInitScript.kts")
public void testDeclarationsInPropertyInitScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/declarationsInPropertyInitScript.kts");
}
@Test
@TestMetadata("enumClassScript.kts")
public void testEnumClassScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/enumClassScript.kts");
}
@Test
@TestMetadata("enumClassWithBodyScript.kts")
public void testEnumClassWithBodyScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/enumClassWithBodyScript.kts");
}
@Test
@TestMetadata("initBlockScript.kts")
public void testInitBlockScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/initBlockScript.kts");
}
@Test
@TestMetadata("localClassScript.kts")
public void testLocalClassScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/localClassScript.kts");
}
@Test
@TestMetadata("localFunScript.kts")
public void testLocalFunScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/localFunScript.kts");
}
@Test
@TestMetadata("localPropertyScript.kts")
public void testLocalPropertyScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/localPropertyScript.kts");
}
@Test
@TestMetadata("memberTypeAliasScript.kts")
public void testMemberTypeAliasScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/memberTypeAliasScript.kts");
}
@Test
@TestMetadata("nestedClassesScript.kts")
public void testNestedClassesScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/nestedClassesScript.kts");
}
@Test
@TestMetadata("propertyAccessorsScript.kts")
public void testPropertyAccessorsScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/propertyAccessorsScript.kts");
}
@Test
@TestMetadata("script.kts")
public void testScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/script.kts");
}
@Test
@TestMetadata("superClassCallScript.kts")
public void testSuperClassCallScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/superClassCallScript.kts");
}
@Test
@TestMetadata("topLevelExpressionBodyFunWithTypeScript.kts")
public void testTopLevelExpressionBodyFunWithTypeScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/topLevelExpressionBodyFunWithTypeScript.kts");
}
@Test
@TestMetadata("topLevelExpressionBodyFunWithoutTypeScript.kts")
public void testTopLevelExpressionBodyFunWithoutTypeScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/topLevelExpressionBodyFunWithoutTypeScript.kts");
}
@Test
@TestMetadata("topLevelFunWithTypeScript.kts")
public void testTopLevelFunWithTypeScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/topLevelFunWithTypeScript.kts");
}
@Test
@TestMetadata("topLevelPropertyScript.kts")
public void testTopLevelPropertyScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/topLevelPropertyScript.kts");
}
@Test
@TestMetadata("topLevelUnitFunScript.kts")
public void testTopLevelUnitFunScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/topLevelUnitFunScript.kts");
}
@Test
@TestMetadata("withoutNameScript.kts")
public void testWithoutNameScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/fileStructure/withoutNameScript.kts");
}
}
@@ -18,7 +18,7 @@ import java.util.regex.Pattern;
@SuppressWarnings("all")
@TestMetadata("analysis/low-level-api-fir/testdata/fileStructure")
@TestDataPath("$PROJECT_ROOT")
public class DiagnosticTraversalCounterTestGenerated extends AbstractDiagnosticTraversalCounterTest {
public class SourceDiagnosticTraversalCounterTestGenerated extends AbstractSourceDiagnosticTraversalCounterTest {
@Test
public void testAllFilesPresentInFileStructure() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/low-level-api-fir/testdata/fileStructure"), Pattern.compile("^(.+)\\.(kt)$"), null, true);
@@ -6,11 +6,12 @@
package org.jetbrains.kotlin.generators.tests.analysis.api
import org.jetbrains.kotlin.analysis.low.level.api.fir.*
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.AbstractDiagnosticTraversalCounterTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.compiler.based.AbstractLLFirDiagnosticCompilerTestDataSpecTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.AbstractFirOutOfContentRootContextCollectionTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.AbstractFirSourceContextCollectionTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.AbstractScriptContextCollectionTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.AbstractScriptDiagnosticTraversalCounterTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.AbstractSourceDiagnosticTraversalCounterTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.compiler.based.AbstractDiagnosticCompilerTestDataTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.compiler.based.AbstractLLFirPreresolvedReversedDiagnosticCompilerTestDataSpecTest
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostic.compiler.based.AbstractLLFirPreresolvedReversedDiagnosticCompilerTestDataTest
@@ -93,10 +94,14 @@ internal fun TestGroupSuite.generateFirLowLevelApiTests() {
model("fileStructure", pattern = TestGeneratorUtil.KTS)
}
testClass<AbstractDiagnosticTraversalCounterTest> {
testClass<AbstractSourceDiagnosticTraversalCounterTest> {
model("fileStructure", pattern = TestGeneratorUtil.KT)
}
testClass<AbstractScriptDiagnosticTraversalCounterTest> {
model("fileStructure", pattern = TestGeneratorUtil.KTS)
}
testClass<AbstractSourceInnerDeclarationsResolvePhaseTest> {
model("innerDeclarationsResolve")
}