Report error on non-reified type parameter with recursive bound in typeOf

Instead of throwing an exception.

 #KT-40173
This commit is contained in:
Alexander Udalov
2021-07-06 16:21:13 +02:00
parent 438ce57183
commit 02774fae0c
10 changed files with 44 additions and 3 deletions
@@ -12,9 +12,11 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.jvm.AsmTypes.*
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.TYPEOF_NON_REIFIED_TYPE_PARAMETER_WITH_RECURSIVE_BOUND
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.TYPEOF_SUSPEND_TYPE
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.model.TypeParameterMarker
@@ -73,4 +75,8 @@ class PsiInlineIntrinsicsSupport(
override fun reportSuspendTypeUnsupported() {
state.diagnostics.report(TYPEOF_SUSPEND_TYPE.on(reportErrorsOn))
}
override fun reportNonReifiedTypeParameterWithRecursiveBoundUnsupported(typeParameterName: Name) {
state.diagnostics.report(TYPEOF_NON_REIFIED_TYPE_PARAMETER_WITH_RECURSIVE_BOUND.on(reportErrorsOn, typeParameterName.asString()))
}
}
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.config.isReleaseCoroutines
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
@@ -75,6 +76,7 @@ class ReifiedTypeInliner<KT : KotlinTypeMarker>(
fun toKotlinType(type: KT): KotlinType
fun reportSuspendTypeUnsupported()
fun reportNonReifiedTypeParameterWithRecursiveBoundUnsupported(typeParameterName: Name)
}
companion object {
@@ -44,9 +44,9 @@ fun <KT : KotlinTypeMarker> TypeSystemCommonBackendContext.generateTypeOf(
val typeParameter = type.typeConstructor().getTypeParameterClassifier()
if (typeParameter != null) {
if (!doesTypeContainTypeParametersWithRecursiveBounds(type)) {
throw UnsupportedOperationException(
"Non-reified type parameters with recursive bounds are not supported yet: ${typeParameter.getName()}"
)
intrinsicsSupport.reportNonReifiedTypeParameterWithRecursiveBoundUnsupported(typeParameter.getName())
v.aconst(null)
return
}
generateNonReifiedTypeParameter(v, typeParameter, intrinsicsSupport)
@@ -210,6 +210,7 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(JVM_INLINE_WITHOUT_VALUE_CLASS, "@JvmInline annotation is only applicable to value classes");
MAP.put(TYPEOF_SUSPEND_TYPE, "Suspend functional types are not supported in typeOf");
MAP.put(TYPEOF_NON_REIFIED_TYPE_PARAMETER_WITH_RECURSIVE_BOUND, "Non-reified type parameters with recursive bounds are not supported yet: {0}", STRING);
}
@NotNull
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.diagnostics.*;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.types.KotlinType;
@@ -176,6 +177,7 @@ public interface ErrorsJvm {
DiagnosticFactory0<PsiElement> JVM_INLINE_WITHOUT_VALUE_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> TYPEOF_SUSPEND_TYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, String> TYPEOF_NON_REIFIED_TYPE_PARAMETER_WITH_RECURSIVE_BOUND = DiagnosticFactory1.create(ERROR);
@SuppressWarnings("UnusedDeclaration")
Object _initializer = new Object() {
@@ -22,7 +22,9 @@ import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.jvm.AsmTypes.*
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.TYPEOF_NON_REIFIED_TYPE_PARAMETER_WITH_RECURSIVE_BOUND
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.TYPEOF_SUSPEND_TYPE
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.model.TypeParameterMarker
@@ -115,4 +117,9 @@ class IrInlineIntrinsicsSupport(
override fun reportSuspendTypeUnsupported() {
context.psiErrorBuilder.at(reportErrorsOn, containingFile).report(TYPEOF_SUSPEND_TYPE)
}
override fun reportNonReifiedTypeParameterWithRecursiveBoundUnsupported(typeParameterName: Name) {
context.psiErrorBuilder.at(reportErrorsOn, containingFile)
.report(TYPEOF_NON_REIFIED_TYPE_PARAMETER_WITH_RECURSIVE_BOUND, typeParameterName.asString())
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
import kotlin.reflect.typeOf
fun <T : Comparable<T>> foo() {
<!TYPEOF_NON_REIFIED_TYPE_PARAMETER_WITH_RECURSIVE_BOUND!>typeOf<List<T>>()<!>
}
@@ -0,0 +1,3 @@
package
public fun </*0*/ T : kotlin.Comparable<T>> foo(): kotlin.Unit
@@ -672,6 +672,12 @@ public class DiagnosticsTestWithJvmIrBackendGenerated extends AbstractDiagnostic
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithJvmBackend/typeOf"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("nonReifiedTypeParameterWithRecursiveBound.kt")
public void testNonReifiedTypeParameterWithRecursiveBound() throws Exception {
runTest("compiler/testData/diagnostics/testsWithJvmBackend/typeOf/nonReifiedTypeParameterWithRecursiveBound.kt");
}
@Test
@TestMetadata("suspendType.kt")
public void testSuspendType() throws Exception {
@@ -660,6 +660,12 @@ public class DiagnosticsTestWithOldJvmBackendGenerated extends AbstractDiagnosti
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithJvmBackend/typeOf"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_OLD, true);
}
@Test
@TestMetadata("nonReifiedTypeParameterWithRecursiveBound.kt")
public void testNonReifiedTypeParameterWithRecursiveBound() throws Exception {
runTest("compiler/testData/diagnostics/testsWithJvmBackend/typeOf/nonReifiedTypeParameterWithRecursiveBound.kt");
}
@Test
@TestMetadata("suspendType.kt")
public void testSuspendType() throws Exception {