diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt index 139cb41126c..e1268c4d37b 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/collectors/components/ErrorNodeDiagnosticCollectorComponent.kt @@ -131,6 +131,13 @@ class ErrorNodeDiagnosticCollectorComponent( reportFirDiagnostic(diagnostic, thisReference.source, data) } + override fun visitVarargArgumentsExpression(varargArgumentsExpression: FirVarargArgumentsExpression, data: CheckerContext) { + val elementType = varargArgumentsExpression.coneElementTypeOrNull ?: return + if (elementType is ConeErrorType) { + reportFirDiagnostic(elementType.diagnostic, varargArgumentsExpression.source, data) + } + } + private fun reportFirDiagnostic( diagnostic: ConeDiagnostic, source: KtSourceElement?, diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index c26a9158cfc..f02acc2ce51 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -540,7 +540,8 @@ class Fir2IrVisitor( startOffset, endOffset, varargArgumentsExpression.resolvedType.toIrType(), - varargArgumentsExpression.varargElementType.toIrType(), + varargArgumentsExpression.coneElementTypeOrNull?.toIrType() + ?: error("Vararg expression has incorrect type"), varargArgumentsExpression.arguments.map { it.convertToIrVarargElement() } ) } diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt index 9de7fc901ca..2cedb765206 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt @@ -222,9 +222,7 @@ private fun List.mapJavaTargetArguments(session: FirSess ConeAttributes.Empty ) coneTypeOrNull = elementConeType - varargElementType = buildResolvedTypeRef { - type = elementConeType.createOutArrayType() - } + coneElementTypeOrNull = elementConeType.createOutArrayType() } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt index 0c66cc9c4d7..48d6ee375aa 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.KtFakeSourceElementKind import org.jetbrains.kotlin.KtSourceElement import org.jetbrains.kotlin.fakeElement import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.copyWithNewSource import org.jetbrains.kotlin.fir.declarations.FirValueParameter import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind @@ -38,15 +39,13 @@ internal fun remapArgumentsWithVararg( // Create a FirVarargArgumentExpression for the vararg arguments. // The order of arguments in the mapping must be preserved for FIR2IR, hence we have to find where the vararg arguments end. // FIR2IR uses the mapping order to determine if arguments need to be reordered. - val varargParameterTypeRef = varargParameter.returnTypeRef val varargElementType = varargArrayType.arrayElementType()?.approximateIntegerLiteralType() val argumentList = argumentMapping.keys.toList() var indexAfterVarargs = argumentList.size val newArgumentMapping = linkedMapOf() val varargArgument = buildVarargArgumentsExpression { - // TODO: ideally we should use here a source from the use-site and not from the declaration-site, KT-59682 - this.varargElementType = varargParameterTypeRef.withReplacedConeType(varargElementType, KtFakeSourceElementKind.VarargArgument) - this.coneTypeOrNull = varargArrayType + coneElementTypeOrNull = varargElementType + coneTypeOrNull = varargArrayType var startOffset = Int.MAX_VALUE var endOffset = 0 var firstVarargElementSource: KtSourceElement? = null diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt index dd10bdfae75..c6057a94df8 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt @@ -1600,7 +1600,7 @@ open class FirExpressionsResolveTransformer(transformer: FirAbstractBodyResolveT i += varargSize source = argument.source coneTypeOrNull = argument.resolvedType - varargElementType = argument.varargElementType + coneElementTypeOrNull = argument.coneElementTypeOrNull } } else { indicesQualifiedAccessForGet[i++] diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/FirVarargArgumentsExpression.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/FirVarargArgumentsExpression.kt index 7fd73868791..86a65a37d50 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/FirVarargArgumentsExpression.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/FirVarargArgumentsExpression.kt @@ -11,7 +11,6 @@ package org.jetbrains.kotlin.fir.expressions import org.jetbrains.kotlin.KtSourceElement import org.jetbrains.kotlin.fir.FirElement import org.jetbrains.kotlin.fir.types.ConeKotlinType -import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.visitors.FirTransformer import org.jetbrains.kotlin.fir.visitors.FirVisitor @@ -24,7 +23,7 @@ abstract class FirVarargArgumentsExpression : FirExpression() { abstract override val coneTypeOrNull: ConeKotlinType? abstract override val annotations: List abstract val arguments: List - abstract val varargElementType: FirTypeRef + abstract val coneElementTypeOrNull: ConeKotlinType? override fun accept(visitor: FirVisitor, data: D): R = visitor.visitVarargArgumentsExpression(this, data) diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/builder/FirVarargArgumentsExpressionBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/builder/FirVarargArgumentsExpressionBuilder.kt index 2c7a3029613..e57d74fead7 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/builder/FirVarargArgumentsExpressionBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/builder/FirVarargArgumentsExpressionBuilder.kt @@ -20,7 +20,6 @@ import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.FirVarargArgumentsExpression import org.jetbrains.kotlin.fir.expressions.impl.FirVarargArgumentsExpressionImpl import org.jetbrains.kotlin.fir.types.ConeKotlinType -import org.jetbrains.kotlin.fir.types.FirTypeRef @FirBuilderDsl class FirVarargArgumentsExpressionBuilder : FirAnnotationContainerBuilder, FirExpressionBuilder { @@ -28,7 +27,7 @@ class FirVarargArgumentsExpressionBuilder : FirAnnotationContainerBuilder, FirEx override var coneTypeOrNull: ConeKotlinType? = null override val annotations: MutableList = mutableListOf() val arguments: MutableList = mutableListOf() - lateinit var varargElementType: FirTypeRef + var coneElementTypeOrNull: ConeKotlinType? = null override fun build(): FirVarargArgumentsExpression { return FirVarargArgumentsExpressionImpl( @@ -36,14 +35,14 @@ class FirVarargArgumentsExpressionBuilder : FirAnnotationContainerBuilder, FirEx coneTypeOrNull, annotations.toMutableOrEmpty(), arguments, - varargElementType, + coneElementTypeOrNull, ) } } @OptIn(ExperimentalContracts::class) -inline fun buildVarargArgumentsExpression(init: FirVarargArgumentsExpressionBuilder.() -> Unit): FirVarargArgumentsExpression { +inline fun buildVarargArgumentsExpression(init: FirVarargArgumentsExpressionBuilder.() -> Unit = {}): FirVarargArgumentsExpression { contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) } diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/impl/FirVarargArgumentsExpressionImpl.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/impl/FirVarargArgumentsExpressionImpl.kt index 94c67f55998..b504bdb0b1b 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/impl/FirVarargArgumentsExpressionImpl.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/impl/FirVarargArgumentsExpressionImpl.kt @@ -18,7 +18,6 @@ import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.FirVarargArgumentsExpression import org.jetbrains.kotlin.fir.expressions.UnresolvedExpressionTypeAccess import org.jetbrains.kotlin.fir.types.ConeKotlinType -import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.visitors.FirTransformer import org.jetbrains.kotlin.fir.visitors.FirVisitor import org.jetbrains.kotlin.fir.visitors.transformInplace @@ -30,19 +29,17 @@ internal class FirVarargArgumentsExpressionImpl( override var coneTypeOrNull: ConeKotlinType?, override var annotations: MutableOrEmptyList, override val arguments: MutableList, - override var varargElementType: FirTypeRef, + override val coneElementTypeOrNull: ConeKotlinType?, ) : FirVarargArgumentsExpression() { override fun acceptChildren(visitor: FirVisitor, data: D) { annotations.forEach { it.accept(visitor, data) } arguments.forEach { it.accept(visitor, data) } - varargElementType.accept(visitor, data) } override fun transformChildren(transformer: FirTransformer, data: D): FirVarargArgumentsExpressionImpl { transformAnnotations(transformer, data) arguments.transformInplace(transformer, data) - varargElementType = varargElementType.transform(transformer, data) return this } diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt index 366e148ae5f..a5789fbc7c5 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt @@ -616,7 +616,7 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild varargArgumentsExpression.configure { +fieldList("arguments", expression) - +field("varargElementType", typeRef) + +field("coneElementTypeOrNull", coneKotlinTypeType, nullable = true) } samConversionExpression.configure { diff --git a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.fir.kt b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.fir.kt index a06ef229d47..34db412d752 100644 --- a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.fir.kt @@ -1,13 +1,14 @@ // !DIAGNOSTICS: -UNUSED_ANONYMOUS_PARAMETER -UNUSED_VARIABLE +// COMPARE_WITH_LIGHT_TREE -fun select(vararg x: T) = x[0] +fun select(vararg x: T) = x[0] fun id(x: K) = x fun main() { val x1 = select(id { x, y -> }, { x: Int, y -> }) - val x2 = select(id { x, y -> }, { x: Int, y -> }) + val x2 = select(id { x, y -> }, { x: Int, y -> }) - val x3 = select(id(fun (x, y) {}), fun (x: Int, y) {}) + val x3 = select(id(fun (x, y) {}), fun (x: Int, y) {}) val x4 = select((fun (x, y) {}), fun (x: Int, y) {}) val x5 = select(id(fun (x, y) {}), fun (x: Int, y) {}) diff --git a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.kt b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.kt index 0ba974cd9af..ee4c859b98f 100644 --- a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.kt +++ b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/notInferableParameterOfAnonymousFunction.kt @@ -1,4 +1,5 @@ // !DIAGNOSTICS: -UNUSED_ANONYMOUS_PARAMETER -UNUSED_VARIABLE +// COMPARE_WITH_LIGHT_TREE fun select(vararg x: T) = x[0] fun id(x: K) = x diff --git a/compiler/testData/diagnostics/tests/inference/nothingType/notEnoughInformationAndNothing.fir.kt b/compiler/testData/diagnostics/tests/inference/nothingType/notEnoughInformationAndNothing.fir.kt index 43b5c4161b2..9930a8c49d1 100644 --- a/compiler/testData/diagnostics/tests/inference/nothingType/notEnoughInformationAndNothing.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/nothingType/notEnoughInformationAndNothing.fir.kt @@ -1,10 +1,11 @@ // !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER // !CHECK_TYPE +// COMPARE_WITH_LIGHT_TREE class Inv fun materialize(): Inv = TODO() fun id(arg: K) = arg -fun select(vararg args: S): S = TODO() +fun select(vararg args: S): S = TODO() fun test1(b: Boolean?) { val v = when(b) { @@ -17,7 +18,7 @@ fun test1(b: Boolean?) { fun test2() { select( - materialize() + materialize() ) select(materialize(), materialize()) select(materialize(), null, Inv()) @@ -26,8 +27,8 @@ fun test2() { null ) select( - materialize(), - materialize() + materialize(), + materialize() ) select( materialize(), diff --git a/compiler/testData/diagnostics/tests/inference/nothingType/notEnoughInformationAndNothing.kt b/compiler/testData/diagnostics/tests/inference/nothingType/notEnoughInformationAndNothing.kt index 33033ff3325..1bb40754161 100644 --- a/compiler/testData/diagnostics/tests/inference/nothingType/notEnoughInformationAndNothing.kt +++ b/compiler/testData/diagnostics/tests/inference/nothingType/notEnoughInformationAndNothing.kt @@ -1,5 +1,6 @@ // !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER // !CHECK_TYPE +// COMPARE_WITH_LIGHT_TREE class Inv fun materialize(): Inv = TODO() diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt2200.fir.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt2200.fir.kt index f7389bc17e1..d65dcef2264 100644 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt2200.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt2200.fir.kt @@ -5,7 +5,7 @@ package n import checkSubtype fun main() { - val a = array(array()) + val a = array(array()) val a0 : Array> = array(array()) val a1 = array(array()) checkSubtype>>(a1) @@ -15,4 +15,4 @@ fun main() { //from library @Suppress("UNCHECKED_CAST") -fun array(vararg t : T) : Array = t as Array +fun array(vararg t : T) : Array = t as Array