Skip to content

Commit

Permalink
[Tests] Add tests to forbid implicit arrays of invisible types
Browse files Browse the repository at this point in the history
#KT-25513
  • Loading branch information
zhelenskiy authored and qodana-bot committed Nov 22, 2024
1 parent 1002b47 commit c9fec6b
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2891,6 +2891,40 @@ internal val KT_DIAGNOSTIC_CONVERTER = KaDiagnosticConverterBuilder.buildConvert
token,
)
}
add(FirErrors.INFERRED_INVISIBLE_REIFIED_TYPE_ARGUMENT.errorFactory) { firDiagnostic ->
InferredInvisibleReifiedTypeArgumentErrorImpl(
firSymbolBuilder.classifierBuilder.buildTypeParameterSymbol(firDiagnostic.a),
firSymbolBuilder.typeBuilder.buildKtType(firDiagnostic.b),
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.INFERRED_INVISIBLE_REIFIED_TYPE_ARGUMENT.warningFactory) { firDiagnostic ->
InferredInvisibleReifiedTypeArgumentWarningImpl(
firSymbolBuilder.classifierBuilder.buildTypeParameterSymbol(firDiagnostic.a),
firSymbolBuilder.typeBuilder.buildKtType(firDiagnostic.b),
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.INFERRED_INVISIBLE_VARARG_TYPE_ARGUMENT.errorFactory) { firDiagnostic ->
InferredInvisibleVarargTypeArgumentErrorImpl(
firSymbolBuilder.classifierBuilder.buildTypeParameterSymbol(firDiagnostic.a),
firSymbolBuilder.typeBuilder.buildKtType(firDiagnostic.b),
firSymbolBuilder.buildSymbol(firDiagnostic.c),
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.INFERRED_INVISIBLE_VARARG_TYPE_ARGUMENT.warningFactory) { firDiagnostic ->
InferredInvisibleVarargTypeArgumentWarningImpl(
firSymbolBuilder.classifierBuilder.buildTypeParameterSymbol(firDiagnostic.a),
firSymbolBuilder.typeBuilder.buildKtType(firDiagnostic.b),
firSymbolBuilder.buildSymbol(firDiagnostic.c),
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED) { firDiagnostic ->
ExtensionInClassReferenceNotAllowedImpl(
firSymbolBuilder.callableBuilder.buildCallableSymbol(firDiagnostic.a),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,32 @@ sealed interface KaFirDiagnostic<PSI : PsiElement> : KaDiagnosticWithPsi<PSI> {
override val diagnosticClass get() = NullableOnDefinitelyNotNullable::class
}

interface InferredInvisibleReifiedTypeArgumentError : KaFirDiagnostic<KtElement> {
override val diagnosticClass get() = InferredInvisibleReifiedTypeArgumentError::class
val typeParameter: KaTypeParameterSymbol
val typeArgumentType: KaType
}

interface InferredInvisibleReifiedTypeArgumentWarning : KaFirDiagnostic<KtElement> {
override val diagnosticClass get() = InferredInvisibleReifiedTypeArgumentWarning::class
val typeParameter: KaTypeParameterSymbol
val typeArgumentType: KaType
}

interface InferredInvisibleVarargTypeArgumentError : KaFirDiagnostic<KtElement> {
override val diagnosticClass get() = InferredInvisibleVarargTypeArgumentError::class
val typeParameter: KaTypeParameterSymbol
val typeArgumentType: KaType
val valueParameter: KaSymbol
}

interface InferredInvisibleVarargTypeArgumentWarning : KaFirDiagnostic<KtElement> {
override val diagnosticClass get() = InferredInvisibleVarargTypeArgumentWarning::class
val typeParameter: KaTypeParameterSymbol
val typeArgumentType: KaType
val valueParameter: KaSymbol
}

interface ExtensionInClassReferenceNotAllowed : KaFirDiagnostic<KtExpression> {
override val diagnosticClass get() = ExtensionInClassReferenceNotAllowed::class
val referencedDeclaration: KaCallableSymbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,36 @@ internal class NullableOnDefinitelyNotNullableImpl(
token: KaLifetimeToken,
) : KaAbstractFirDiagnostic<KtElement>(firDiagnostic, token), KaFirDiagnostic.NullableOnDefinitelyNotNullable

internal class InferredInvisibleReifiedTypeArgumentErrorImpl(
override val typeParameter: KaTypeParameterSymbol,
override val typeArgumentType: KaType,
firDiagnostic: KtPsiDiagnostic,
token: KaLifetimeToken,
) : KaAbstractFirDiagnostic<KtElement>(firDiagnostic, token), KaFirDiagnostic.InferredInvisibleReifiedTypeArgumentError

internal class InferredInvisibleReifiedTypeArgumentWarningImpl(
override val typeParameter: KaTypeParameterSymbol,
override val typeArgumentType: KaType,
firDiagnostic: KtPsiDiagnostic,
token: KaLifetimeToken,
) : KaAbstractFirDiagnostic<KtElement>(firDiagnostic, token), KaFirDiagnostic.InferredInvisibleReifiedTypeArgumentWarning

internal class InferredInvisibleVarargTypeArgumentErrorImpl(
override val typeParameter: KaTypeParameterSymbol,
override val typeArgumentType: KaType,
override val valueParameter: KaSymbol,
firDiagnostic: KtPsiDiagnostic,
token: KaLifetimeToken,
) : KaAbstractFirDiagnostic<KtElement>(firDiagnostic, token), KaFirDiagnostic.InferredInvisibleVarargTypeArgumentError

internal class InferredInvisibleVarargTypeArgumentWarningImpl(
override val typeParameter: KaTypeParameterSymbol,
override val typeArgumentType: KaType,
override val valueParameter: KaSymbol,
firDiagnostic: KtPsiDiagnostic,
token: KaLifetimeToken,
) : KaAbstractFirDiagnostic<KtElement>(firDiagnostic, token), KaFirDiagnostic.InferredInvisibleVarargTypeArgumentWarning

internal class ExtensionInClassReferenceNotAllowedImpl(
override val referencedDeclaration: KaCallableSymbol,
firDiagnostic: KtPsiDiagnostic,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageFeature.ForbidJvmAnnotationsOnAnnotationParameters
import org.jetbrains.kotlin.config.LanguageFeature.ForbidJvmSerializableLambdaOnInlinedFunctionLiterals
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.contracts.description.EventOccurrencesRange
import org.jetbrains.kotlin.descriptors.ClassKind
Expand All @@ -26,7 +24,6 @@ import org.jetbrains.kotlin.fir.checkers.generator.diagnostics.model.*
import org.jetbrains.kotlin.fir.declarations.FirDeprecationInfo
import org.jetbrains.kotlin.fir.declarations.FirFunction
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
import org.jetbrains.kotlin.fir.expressions.FirCall
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.*
Expand Down Expand Up @@ -976,6 +973,19 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
val INCORRECT_LEFT_COMPONENT_OF_INTERSECTION by error<KtElement>()
val INCORRECT_RIGHT_COMPONENT_OF_INTERSECTION by error<KtElement>()
val NULLABLE_ON_DEFINITELY_NOT_NULLABLE by error<KtElement>()
val INFERRED_INVISIBLE_REIFIED_TYPE_ARGUMENT by deprecationError<KtElement>(
LanguageFeature.ForbidInferOfInvisibleTypeAsReifiedOrVararg
) {
parameter<FirTypeParameterSymbol>("typeParameter")
parameter<ConeKotlinType>("typeArgumentType")
}
val INFERRED_INVISIBLE_VARARG_TYPE_ARGUMENT by deprecationError<KtElement>(
LanguageFeature.ForbidInferOfInvisibleTypeAsReifiedOrVararg
) {
parameter<FirTypeParameterSymbol>("typeParameter")
parameter<ConeKotlinType>("typeArgumentType")
parameter<FirValueParameterSymbol>("valueParameter")
}
}

val REFLECTION by object : DiagnosticGroup("Reflection") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageFeature.ErrorAboutDataClassCopyVisibilityChange
import org.jetbrains.kotlin.config.LanguageFeature.ForbidCompanionInLocalInnerClass
import org.jetbrains.kotlin.config.LanguageFeature.ForbidExposingTypesInPrimaryConstructorProperties
import org.jetbrains.kotlin.config.LanguageFeature.ForbidInferOfInvisibleTypeAsReifiedOrVararg
import org.jetbrains.kotlin.config.LanguageFeature.ForbidInferringTypeVariablesIntoEmptyIntersection
import org.jetbrains.kotlin.config.LanguageFeature.ForbidParenthesizedLhsInAssignments
import org.jetbrains.kotlin.config.LanguageFeature.ForbidProjectionsInAnnotationProperties
Expand Down Expand Up @@ -542,6 +543,8 @@ object FirErrors {
val INCORRECT_LEFT_COMPONENT_OF_INTERSECTION: KtDiagnosticFactory0 = KtDiagnosticFactory0("INCORRECT_LEFT_COMPONENT_OF_INTERSECTION", ERROR, SourceElementPositioningStrategies.DEFAULT, KtElement::class)
val INCORRECT_RIGHT_COMPONENT_OF_INTERSECTION: KtDiagnosticFactory0 = KtDiagnosticFactory0("INCORRECT_RIGHT_COMPONENT_OF_INTERSECTION", ERROR, SourceElementPositioningStrategies.DEFAULT, KtElement::class)
val NULLABLE_ON_DEFINITELY_NOT_NULLABLE: KtDiagnosticFactory0 = KtDiagnosticFactory0("NULLABLE_ON_DEFINITELY_NOT_NULLABLE", ERROR, SourceElementPositioningStrategies.DEFAULT, KtElement::class)
val INFERRED_INVISIBLE_REIFIED_TYPE_ARGUMENT: KtDiagnosticFactoryForDeprecation2<FirTypeParameterSymbol, ConeKotlinType> = KtDiagnosticFactoryForDeprecation2("INFERRED_INVISIBLE_REIFIED_TYPE_ARGUMENT", ForbidInferOfInvisibleTypeAsReifiedOrVararg, SourceElementPositioningStrategies.DEFAULT, KtElement::class)
val INFERRED_INVISIBLE_VARARG_TYPE_ARGUMENT: KtDiagnosticFactoryForDeprecation3<FirTypeParameterSymbol, ConeKotlinType, FirValueParameterSymbol> = KtDiagnosticFactoryForDeprecation3("INFERRED_INVISIBLE_VARARG_TYPE_ARGUMENT", ForbidInferOfInvisibleTypeAsReifiedOrVararg, SourceElementPositioningStrategies.DEFAULT, KtElement::class)

// Reflection
val EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED: KtDiagnosticFactory1<FirCallableSymbol<*>> = KtDiagnosticFactory1("EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED", ERROR, SourceElementPositioningStrategies.REFERENCE_BY_QUALIFIED, KtExpression::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INCORRECT_RIGHT_C
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INC_DEC_SHOULD_NOT_RETURN_UNIT
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INEFFICIENT_EQUALS_OVERRIDING_IN_VALUE_CLASS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INFERENCE_ERROR
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INFERRED_INVISIBLE_REIFIED_TYPE_ARGUMENT
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INFERRED_INVISIBLE_VARARG_TYPE_ARGUMENT
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INFERRED_TYPE_VARIABLE_INTO_POSSIBLE_EMPTY_INTERSECTION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INFIX_MODIFIER_REQUIRED
Expand Down Expand Up @@ -1417,6 +1419,19 @@ object FirErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
NULLABLE_ON_DEFINITELY_NOT_NULLABLE,
"'!!' type cannot be marked as nullable."
)
map.put(
INFERRED_INVISIBLE_REIFIED_TYPE_ARGUMENT,
"Type argument for reified type parameter ''{0}'' is inferred as invisible type ''{1}''.",
SYMBOL,
RENDER_TYPE,
)
map.put(
INFERRED_INVISIBLE_VARARG_TYPE_ARGUMENT,
"Type argument for type parameter ''{0}'' used as type of vararg parameter ''{2}'' is inferred as invisible type ''{1}''.",
SYMBOL,
RENDER_TYPE,
DECLARATION_NAME,
)
map.put(
INCORRECT_LEFT_COMPONENT_OF_INTERSECTION,
"Intersection types are supported only for definitely non-nullable types: left part must be a type parameter with nullable bounds."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// FIR_IDENTICAL
// RUN_PIPELINE_TILL: FRONTEND
// DONT_WARN_ON_ERROR_SUPPRESSION
// WITH_STDLIB

// FILE: test1/source.kt
package test1

private interface PrivateInFileInterface

data object Public1 : PrivateInFileInterface
data object Public2 : PrivateInFileInterface

// FILE: test2/source.kt
package test2

import test1.Public1
import test1.Public2

fun <T> f1(x: T, y: T) = mutableListOf(x, y)
fun <T> f2(vararg x: T) = Unit
@Suppress("MULTIPLE_VARARG_PARAMETERS")
fun <T> f2_(vararg x: T, vararg y: T) = Unit
inline fun <reified T> f3(x: T, y: T) = listOf(x, y)
inline fun <reified T> f4(x: T, y: T) = listOf(x) + listOf(y)
inline fun <reified T> f5(x: T, y: T) = arrayOfNulls<T>(0)
inline fun <reified T> f6(x: T, y: T) = emptyArray<T>()
<!NOTHING_TO_INLINE!>inline<!> fun <T> f7(x: Array<T>) = x

object `_`

fun main() {
println(f1<_>(Public1, Public2)) // ok
println(f1(Public1, Public2)) // ok
println(f2<_>(Public1, Public2)) // fails in run time
println(f2(Public1, Public2)) // fails in run time
println(f2_(Public1, Public2)) // fails in compile time
println(f3<_>(Public1, Public2)) // fails in run time
println(f3(Public1, Public2)) // fails in run time
println(f4(Public1, Public2)) // ok
println(f4<_>(Public1, Public2)) // ok
println(f5(Public1, Public2)) // fails in run time
println(f5<_>(Public1, Public2)) // fails in run time
println(f6(Public1, Public2)) // fails in run time
println(f6<_>(Public1, Public2)) // fails in run time
println(f7(arrayOf(Public1, Public2))) // fails in run time
println(f7<_>(arrayOf(Public1, Public2))) // fails in run time

println(f2<test1.<!INVISIBLE_REFERENCE!>PrivateInFileInterface<!>>(Public1, Public2))
println(f3<test1.<!INVISIBLE_REFERENCE!>PrivateInFileInterface<!>>(Public1, Public2))
println(f3<`_`>(`_`, `_`))
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
import org.jetbrains.kotlin.test.builders.classicFrontendHandlersStep
import org.jetbrains.kotlin.test.builders.firHandlersStep
import org.jetbrains.kotlin.test.builders.jvmArtifactsHandlersStep
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives
import org.jetbrains.kotlin.test.directives.JvmEnvironmentConfigurationDirectives
import org.jetbrains.kotlin.test.directives.configureFirParser
import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontend2IrConverter
Expand Down Expand Up @@ -57,6 +58,8 @@ abstract class AbstractDiagnosticsTestWithJvmBackend<R : ResultingArtifact.Front
+JvmEnvironmentConfigurationDirectives.USE_PSI_CLASS_FILES_READING
}

useDirectives(CodegenTestDirectives)

enableMetaInfoHandler()

useConfigurators(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.jetbrains.kotlin.config

import org.jetbrains.kotlin.config.LanguageFeature.Kind.*
import org.jetbrains.kotlin.config.LanguageFeature.entries
import org.jetbrains.kotlin.config.LanguageVersion.*
import org.jetbrains.kotlin.utils.DescriptionAware
import java.util.*
Expand Down Expand Up @@ -365,6 +366,7 @@ enum class LanguageFeature(
// TODO: Remove org.jetbrains.kotlin.fir.resolve.calls.stages.ConstraintSystemForks together with this LF (KT-72961)
ConsiderForkPointsWhenCheckingContradictions(KOTLIN_2_2), // KT-68768
CallableReferenceOverloadResolutionInLambda(KOTLIN_2_2), // KT-73011
ForbidInferOfInvisibleTypeAsReifiedOrVararg(KOTLIN_2_2, kind = BUG_FIX), // KT-25513

// 2.3

Expand Down

0 comments on commit c9fec6b

Please sign in to comment.