From 7eb758fab11e69e42fab50ba01c38e39b205a342 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Wed, 24 Nov 2021 15:13:01 +0300 Subject: [PATCH] FIR: Avoid propagation of @Exact annotation through elvis --- ...isCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ .../FirOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...ndDiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ ...FirCallCompletionResultsWriterTransformer.kt | 17 ++++++++++++++++- .../annotations/dontPropagateExact.kt | 16 ++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/testsWithStdLib/annotations/dontPropagateExact.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 02854175df2..6c807372206 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -33179,6 +33179,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.kt"); } + @Test + @TestMetadata("dontPropagateExact.kt") + public void testDontPropagateExact() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/annotations/dontPropagateExact.kt"); + } + @Test @TestMetadata("explicitMetadata.kt") public void testExplicitMetadata() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 6280d3022c3..34e95e21a3f 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -33179,6 +33179,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.kt"); } + @Test + @TestMetadata("dontPropagateExact.kt") + public void testDontPropagateExact() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/annotations/dontPropagateExact.kt"); + } + @Test @TestMetadata("explicitMetadata.kt") public void testExplicitMetadata() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 0f533bd9884..e7af5bdcbec 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -33179,6 +33179,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.kt"); } + @Test + @TestMetadata("dontPropagateExact.kt") + public void testDontPropagateExact() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/annotations/dontPropagateExact.kt"); + } + @Test @TestMetadata("explicitMetadata.kt") public void testExplicitMetadata() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt index b64dbf1e6cd..100c95fc62b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt @@ -332,7 +332,22 @@ class FirCallCompletionResultsWriterTransformer( type = substitutedType ?: initialType, TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference, ) ?: substitutedType - return withReplacedConeType(finalType) + // This is probably a temporary hack, but it seems necessary because elvis has that attribute and it may leak further like + // fun foo() = materializeNullable() ?: materialize() // `foo` return type unexpectedly gets inferred to @Exact E + // + // In FE1.0, it's not necessary since the annotation for elvis have some strange form (see org.jetbrains.kotlin.resolve.descriptorUtil.AnnotationsWithOnly) + // that is not propagated further. + val withRemovedExactAttribute = finalType?.removeExactAttribute() + + return withReplacedConeType(withRemovedExactAttribute) + } + + private fun ConeKotlinType.removeExactAttribute(): ConeKotlinType { + if (attributes.contains(CompilerConeAttributes.Exact)) { + return withAttributes(attributes.remove(CompilerConeAttributes.Exact), session.typeContext) + } + + return this } override fun transformSafeCallExpression( diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/dontPropagateExact.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/dontPropagateExact.kt new file mode 100644 index 00000000000..fb4c008d9b0 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/dontPropagateExact.kt @@ -0,0 +1,16 @@ +// FIR_IDENTICAL +// FULL_JDK +// SKIP_TXT +package test +import kotlin.reflect.KClass + +annotation class RunsInActiveStoreMode + +val w1 = ""::class.java +val w2 = ""::class.java + +private fun foo(annotationClass: Class) = w1.getAnnotation(annotationClass) ?: w2.getAnnotation(annotationClass) + +fun main() { + val x: Any = foo(RunsInActiveStoreMode::class.java) +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 3b80d78a345..c8a41b51392 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -33275,6 +33275,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.kt"); } + @Test + @TestMetadata("dontPropagateExact.kt") + public void testDontPropagateExact() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/annotations/dontPropagateExact.kt"); + } + @Test @TestMetadata("explicitMetadata.kt") public void testExplicitMetadata() throws Exception {