[KMP] Implement ACTUAL_TYPE_ALIAS_TO_NOTHING/TO_NULLABLE_TYPE errors

^KT-60650 Done
This commit is contained in:
marat.akhin
2023-07-25 12:22:28 +02:00
committed by Space Team
parent 7cef7ac639
commit 34b271b13d
23 changed files with 249 additions and 1 deletions
@@ -3399,6 +3399,18 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert
token,
)
}
add(FirErrors.ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE) { firDiagnostic ->
ActualTypeAliasToNullableTypeImpl(
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.ACTUAL_TYPE_ALIAS_TO_NOTHING) { firDiagnostic ->
ActualTypeAliasToNothingImpl(
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS) { firDiagnostic ->
ActualFunctionWithDefaultArgumentsImpl(
firDiagnostic as KtPsiDiagnostic,
@@ -2391,6 +2391,14 @@ sealed interface KtFirDiagnostic<PSI : PsiElement> : KtDiagnosticWithPsi<PSI> {
override val diagnosticClass get() = ActualTypeAliasWithComplexSubstitution::class
}
interface ActualTypeAliasToNullableType : KtFirDiagnostic<KtTypeAlias> {
override val diagnosticClass get() = ActualTypeAliasToNullableType::class
}
interface ActualTypeAliasToNothing : KtFirDiagnostic<KtTypeAlias> {
override val diagnosticClass get() = ActualTypeAliasToNothing::class
}
interface ActualFunctionWithDefaultArguments : KtFirDiagnostic<PsiElement> {
override val diagnosticClass get() = ActualFunctionWithDefaultArguments::class
}
@@ -2884,6 +2884,16 @@ internal class ActualTypeAliasWithComplexSubstitutionImpl(
token: KtLifetimeToken,
) : KtAbstractFirDiagnostic<KtTypeAlias>(firDiagnostic, token), KtFirDiagnostic.ActualTypeAliasWithComplexSubstitution
internal class ActualTypeAliasToNullableTypeImpl(
firDiagnostic: KtPsiDiagnostic,
token: KtLifetimeToken,
) : KtAbstractFirDiagnostic<KtTypeAlias>(firDiagnostic, token), KtFirDiagnostic.ActualTypeAliasToNullableType
internal class ActualTypeAliasToNothingImpl(
firDiagnostic: KtPsiDiagnostic,
token: KtLifetimeToken,
) : KtAbstractFirDiagnostic<KtTypeAlias>(firDiagnostic, token), KtFirDiagnostic.ActualTypeAliasToNothing
internal class ActualFunctionWithDefaultArgumentsImpl(
firDiagnostic: KtPsiDiagnostic,
token: KtLifetimeToken,
@@ -1018,4 +1018,26 @@ public class FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated extends Abst
runTest("compiler/testData/diagnostics/tests/multiplatform/topLevelProperty/simpleHeaderVar.kt");
}
}
@Nested
@TestMetadata("compiler/testData/diagnostics/tests/multiplatform/typealias")
@TestDataPath("$PROJECT_ROOT")
public class Typealias {
@Test
@TestMetadata("actualTypealiasToNothing.kt")
public void testActualTypealiasToNothing() throws Exception {
runTest("compiler/testData/diagnostics/tests/multiplatform/typealias/actualTypealiasToNothing.kt");
}
@Test
@TestMetadata("actualTypealiasToNullableType.kt")
public void testActualTypealiasToNullableType() throws Exception {
runTest("compiler/testData/diagnostics/tests/multiplatform/typealias/actualTypealiasToNullableType.kt");
}
@Test
public void testAllFilesPresentInTypealias() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multiplatform/typealias"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), TargetBackend.JVM_IR, true);
}
}
}
@@ -1018,4 +1018,26 @@ public class FirOldFrontendMPPDiagnosticsWithPsiTestGenerated extends AbstractFi
runTest("compiler/testData/diagnostics/tests/multiplatform/topLevelProperty/simpleHeaderVar.kt");
}
}
@Nested
@TestMetadata("compiler/testData/diagnostics/tests/multiplatform/typealias")
@TestDataPath("$PROJECT_ROOT")
public class Typealias {
@Test
@TestMetadata("actualTypealiasToNothing.kt")
public void testActualTypealiasToNothing() throws Exception {
runTest("compiler/testData/diagnostics/tests/multiplatform/typealias/actualTypealiasToNothing.kt");
}
@Test
@TestMetadata("actualTypealiasToNullableType.kt")
public void testActualTypealiasToNullableType() throws Exception {
runTest("compiler/testData/diagnostics/tests/multiplatform/typealias/actualTypealiasToNullableType.kt");
}
@Test
public void testAllFilesPresentInTypealias() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multiplatform/typealias"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), TargetBackend.JVM_IR, true);
}
}
}
@@ -1143,6 +1143,8 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
val ACTUAL_TYPE_ALIAS_TO_CLASS_WITH_DECLARATION_SITE_VARIANCE by error<KtTypeAlias>(PositioningStrategy.DECLARATION_SIGNATURE)
val ACTUAL_TYPE_ALIAS_WITH_USE_SITE_VARIANCE by error<KtTypeAlias>(PositioningStrategy.DECLARATION_SIGNATURE)
val ACTUAL_TYPE_ALIAS_WITH_COMPLEX_SUBSTITUTION by error<KtTypeAlias>(PositioningStrategy.DECLARATION_SIGNATURE)
val ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE by error<KtTypeAlias>(PositioningStrategy.DECLARATION_SIGNATURE)
val ACTUAL_TYPE_ALIAS_TO_NOTHING by error<KtTypeAlias>(PositioningStrategy.DECLARATION_SIGNATURE)
val ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS by error<PsiElement>()
val DEFAULT_ARGUMENTS_IN_EXPECT_WITH_ACTUAL_TYPEALIAS by error<KtTypeAlias> {
parameter<FirClassSymbol<*>>("expectClassSymbol")
@@ -619,6 +619,8 @@ object FirErrors {
val ACTUAL_TYPE_ALIAS_TO_CLASS_WITH_DECLARATION_SITE_VARIANCE by error0<KtTypeAlias>(SourceElementPositioningStrategies.DECLARATION_SIGNATURE)
val ACTUAL_TYPE_ALIAS_WITH_USE_SITE_VARIANCE by error0<KtTypeAlias>(SourceElementPositioningStrategies.DECLARATION_SIGNATURE)
val ACTUAL_TYPE_ALIAS_WITH_COMPLEX_SUBSTITUTION by error0<KtTypeAlias>(SourceElementPositioningStrategies.DECLARATION_SIGNATURE)
val ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE by error0<KtTypeAlias>(SourceElementPositioningStrategies.DECLARATION_SIGNATURE)
val ACTUAL_TYPE_ALIAS_TO_NOTHING by error0<KtTypeAlias>(SourceElementPositioningStrategies.DECLARATION_SIGNATURE)
val ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS by error0<PsiElement>()
val DEFAULT_ARGUMENTS_IN_EXPECT_WITH_ACTUAL_TYPEALIAS by error2<KtTypeAlias, FirClassSymbol<*>, Collection<FirCallableSymbol<*>>>()
val ACTUAL_ANNOTATION_CONFLICTING_DEFAULT_ARGUMENT_VALUE by error1<PsiElement, FirVariableSymbol<*>>()
@@ -61,5 +61,15 @@ object FirActualTypeAliasChecker : FirTypeAliasChecker() {
if (reportActualTypeAliasWithComplexSubstitution) {
reporter.reportOn(declaration.source, FirErrors.ACTUAL_TYPE_ALIAS_WITH_COMPLEX_SUBSTITUTION, context)
}
@OptIn(UnexpandedTypeCheck::class)
// an earlier check ensures we have an ACTUAL_TYPE_ALIAS_NOT_TO_CLASS error on non-expanded type alias
if (expandedTypeRef.isNothing) {
reporter.reportOn(declaration.source, FirErrors.ACTUAL_TYPE_ALIAS_TO_NOTHING, context)
}
if (expandedTypeRef.isMarkedNullable == true) {
reporter.reportOn(declaration.source, FirErrors.ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE, context)
}
}
}
}
@@ -58,6 +58,8 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ACTUAL_MISSING
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ACTUAL_TYPEALIAS_TO_SPECIAL_ANNOTATION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ACTUAL_TYPE_ALIAS_NOT_TO_CLASS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ACTUAL_TYPE_ALIAS_TO_CLASS_WITH_DECLARATION_SITE_VARIANCE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ACTUAL_TYPE_ALIAS_TO_NOTHING
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ACTUAL_TYPE_ALIAS_WITH_COMPLEX_SUBSTITUTION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ACTUAL_TYPE_ALIAS_WITH_USE_SITE_VARIANCE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.ACTUAL_WITHOUT_EXPECT
@@ -1823,6 +1825,14 @@ object FirErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
ACTUAL_TYPE_ALIAS_WITH_COMPLEX_SUBSTITUTION,
"Type arguments in the right-hand side of actual type alias should be its type parameters in the same order, e.g. 'actual typealias Foo<A, B> = Bar<A, B>'"
)
map.put(
ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE,
"Right-hand side of actual type alias cannot be a nullable type"
)
map.put(
ACTUAL_TYPE_ALIAS_TO_NOTHING,
"Right-hand side of actual type alias cannot be of type kotlin.Nothing"
)
map.put(
ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS,
"Actual function cannot have default argument values, they should be declared in the expected function"
@@ -807,6 +807,8 @@ public interface Errors {
ACTUAL_TYPE_ALIAS_TO_CLASS_WITH_DECLARATION_SITE_VARIANCE = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtTypeAlias> ACTUAL_TYPE_ALIAS_WITH_USE_SITE_VARIANCE = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtTypeAlias> ACTUAL_TYPE_ALIAS_WITH_COMPLEX_SUBSTITUTION = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtTypeAlias> ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtTypeAlias> ACTUAL_TYPE_ALIAS_TO_NOTHING = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<PsiElement> ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory2<KtNamedDeclaration, ClassDescriptor, Collection<FunctionDescriptor>> DEFAULT_ARGUMENTS_IN_EXPECT_WITH_ACTUAL_TYPEALIAS = DiagnosticFactory2.create(ERROR);
DiagnosticFactory1<PsiElement, ValueParameterDescriptor> ACTUAL_ANNOTATION_CONFLICTING_DEFAULT_ARGUMENT_VALUE =
@@ -361,6 +361,8 @@ public class DefaultErrorMessages {
MAP.put(ACTUAL_TYPE_ALIAS_TO_CLASS_WITH_DECLARATION_SITE_VARIANCE, "Aliased class should not have type parameters with declaration-site variance");
MAP.put(ACTUAL_TYPE_ALIAS_WITH_USE_SITE_VARIANCE, "Right-hand side of actual type alias cannot contain use-site variance or star projections");
MAP.put(ACTUAL_TYPE_ALIAS_WITH_COMPLEX_SUBSTITUTION, "Type arguments in the right-hand side of actual type alias should be its type parameters in the same order, e.g. 'actual typealias Foo<A, B> = Bar<A, B>'");
MAP.put(ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE, "Right-hand side of actual type alias cannot be a nullable type");
MAP.put(ACTUAL_TYPE_ALIAS_TO_NOTHING, "Right-hand side of actual type alias cannot be of type kotlin.Nothing");
MAP.put(ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS, "Actual function cannot have default argument values, they should be declared in the expected function");
MAP.put(ACTUAL_ANNOTATION_CONFLICTING_DEFAULT_ARGUMENT_VALUE, "Parameter ''{0}'' has conflicting values in the expected and actual annotation", NAME);
MAP.put(DEFAULT_ARGUMENTS_IN_EXPECT_WITH_ACTUAL_TYPEALIAS,
@@ -191,6 +191,16 @@ class DeclarationsChecker(
trace.report(ACTUAL_TYPE_ALIAS_WITH_COMPLEX_SUBSTITUTION.on(declaration))
return
}
if (rhs.isNothing()) {
trace.report(ACTUAL_TYPE_ALIAS_TO_NOTHING.on(declaration))
return
}
if (rhs.isMarkedNullable) {
trace.report(ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE.on(declaration))
return
}
}
private fun getUsedTypeAliasParameters(type: KotlinType, typeAlias: TypeAliasDescriptor): Set<TypeParameterDescriptor> =
@@ -0,0 +1,13 @@
// MODULE: m1-common
// FILE: common.kt
<!INCOMPATIBLE_MATCHING{JVM}!>expect class E01<!>
<!INCOMPATIBLE_MATCHING{JVM}!>expect class E02<!>
// MODULE: m1-jvm()()(m1-common)
// FILE: jvm.kt
typealias MyNothing = Nothing
<!ACTUAL_TYPE_ALIAS_TO_NOTHING!>actual typealias E01 = Nothing<!>
<!ACTUAL_TYPE_ALIAS_NOT_TO_CLASS!>actual typealias E02 = MyNothing<!>
@@ -0,0 +1,13 @@
// MODULE: m1-common
// FILE: common.kt
expect class E01
expect class E02
// MODULE: m1-jvm()()(m1-common)
// FILE: jvm.kt
typealias MyNothing = Nothing
<!ACTUAL_TYPE_ALIAS_TO_NOTHING!>actual typealias E01 = Nothing<!>
<!ACTUAL_TYPE_ALIAS_NOT_TO_CLASS!>actual typealias E02 = MyNothing<!>
@@ -0,0 +1,14 @@
// FIR_IDENTICAL
// MODULE: m1-common
// FILE: common.kt
expect class E01
expect class E02
// MODULE: m1-jvm()()(m1-common)
// FILE: jvm.kt
typealias MyNullable = String?
<!ACTUAL_TYPE_ALIAS_TO_NULLABLE_TYPE!>actual typealias E01 = String?<!>
<!ACTUAL_TYPE_ALIAS_NOT_TO_CLASS!>actual typealias E02 = MyNullable<!>
@@ -0,0 +1,2 @@
expect class E01
expect class E02
@@ -0,0 +1,4 @@
typealias MyNothing = Nothing
actual typealias E01 = Nothing
actual typealias E02 = MyNothing
@@ -0,0 +1,13 @@
-- Common --
Exit code: OK
Output:
-- JVM --
Exit code: COMPILATION_ERROR
Output:
compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNothing/jvm.kt:3:1: error: right-hand side of actual type alias cannot be of type kotlin.Nothing
actual typealias E01 = Nothing
^
compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNothing/jvm.kt:4:1: error: right-hand side of actual type alias should be a class, not another type alias
actual typealias E02 = MyNothing
^
@@ -0,0 +1,2 @@
expect class E01
expect class E02
@@ -0,0 +1,4 @@
typealias MyNullable = String?
actual typealias E01 = String?
actual typealias E02 = MyNullable
@@ -0,0 +1,13 @@
-- Common --
Exit code: OK
Output:
-- JVM --
Exit code: COMPILATION_ERROR
Output:
compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNullableType/jvm.kt:3:1: error: right-hand side of actual type alias cannot be a nullable type
actual typealias E01 = String?
^
compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNullableType/jvm.kt:4:1: error: right-hand side of actual type alias should be a class, not another type alias
actual typealias E02 = MyNullable
^
@@ -23581,6 +23581,28 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/multiplatform/topLevelProperty/simpleHeaderVar.kt");
}
}
@Nested
@TestMetadata("compiler/testData/diagnostics/tests/multiplatform/typealias")
@TestDataPath("$PROJECT_ROOT")
public class Typealias {
@Test
@TestMetadata("actualTypealiasToNothing.kt")
public void testActualTypealiasToNothing() throws Exception {
runTest("compiler/testData/diagnostics/tests/multiplatform/typealias/actualTypealiasToNothing.kt");
}
@Test
@TestMetadata("actualTypealiasToNullableType.kt")
public void testActualTypealiasToNullableType() throws Exception {
runTest("compiler/testData/diagnostics/tests/multiplatform/typealias/actualTypealiasToNullableType.kt");
}
@Test
public void testAllFilesPresentInTypealias() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multiplatform/typealias"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true);
}
}
}
@Nested
@@ -449,6 +449,16 @@ public class MultiPlatformIntegrationTestGenerated extends AbstractMultiPlatform
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("actualTypealiasToNothing")
public void testActualTypealiasToNothing() throws Exception {
runTest("compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNothing/");
}
@TestMetadata("actualTypealiasToNullableType")
public void testActualTypealiasToNullableType() throws Exception {
runTest("compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNullableType/");
}
public void testAllFilesPresentInImplTypeAlias() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/multiplatform/implTypeAlias"), Pattern.compile("^([^\\.]+)$"), null, true);
}
@@ -468,6 +478,32 @@ public class MultiPlatformIntegrationTestGenerated extends AbstractMultiPlatform
runTest("compiler/testData/multiplatform/implTypeAlias/nestedClassesViaTypeAlias/");
}
@TestMetadata("compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNothing")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ActualTypealiasToNothing extends AbstractMultiPlatformIntegrationTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInActualTypealiasToNothing() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNothing"), Pattern.compile("^([^\\.]+)$"), null, true);
}
}
@TestMetadata("compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNullableType")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ActualTypealiasToNullableType extends AbstractMultiPlatformIntegrationTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInActualTypealiasToNullableType() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/multiplatform/implTypeAlias/actualTypealiasToNullableType"), Pattern.compile("^([^\\.]+)$"), null, true);
}
}
@TestMetadata("compiler/testData/multiplatform/implTypeAlias/discriminateHeaderClassInFavorOfTypeAlias")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)