FIR: Avoid propagation of @Exact annotation through elvis

This commit is contained in:
Denis.Zharkov
2021-11-24 15:13:01 +03:00
committed by teamcityserver
parent 0d9ad62d4a
commit 7eb758fab1
6 changed files with 56 additions and 1 deletions
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 <E> foo() = materializeNullable<E>() ?: materialize<E>() // `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(
@@ -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 <T : Annotation> foo(annotationClass: Class<T>) = w1.getAnnotation(annotationClass) ?: w2.getAnnotation(annotationClass)
fun main() {
val x: Any = foo(RunsInActiveStoreMode::class.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 {