diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index 03b555bdae5..02dfdb06a9d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -926,11 +926,12 @@ public class DefaultErrorMessages { ELEMENT_TEXT, STRING, ELEMENT_TEXT); MAP.put(OVERLOAD_RESOLUTION_AMBIGUITY, "Overload resolution ambiguity: {0}", AMBIGUOUS_CALLS); - MAP.put(OVERLOAD_RESOLUTION_AMBIGUITY_BECAUSE_OF_STUB_TYPES, "The builder `{0}` you are using has a type argument for type parameter(s) `{1}` that was not explicitly specified. " + - "Without knowing the type of `{1}` compiler cannot choose which overloaded function `{2}` to call here. " + - "Please, either specify the type `{1}` explicitly in `{0}` builder or use explicit cast to a specific type for parameter or receiver (see specific errors on them).", STRING, STRING, STRING); - MAP.put(STUB_TYPE_IN_ARGUMENT_CAUSES_AMBIGUITY, "Type of an argument hasn't inferred yet. To disambiguate this call, please use explicit cast for the parameter to {1} if you rely a type argument for type parameter(s) {2} to be inferred to {3}", RENDER_TYPE, STRING, STRING); - MAP.put(STUB_TYPE_IN_RECEIVER_CAUSES_AMBIGUITY, "Type of a receiver hasn't inferred yet. To disambiguate this call, please use explicit cast for the receiver to {1} if you rely a type argument for type parameter(s) {2} to be inferred to {3}", RENDER_TYPE, STRING, STRING, null); + MAP.put(OVERLOAD_RESOLUTION_AMBIGUITY_BECAUSE_OF_STUB_TYPES, "No type argument for type parameter(s) `{1}` of the `{0}` builder specified. " + + "Cannot choose which overloaded function `{2}` to call. " + + "To disambiguate this call, either use an explicit type cast for a parameter or a receiver (see specific errors on them) or specify the type `{1}` explicitly.", + STRING, STRING, STRING); + MAP.put(STUB_TYPE_IN_ARGUMENT_CAUSES_AMBIGUITY, "The type of an argument hasn''t been inferred yet. To disambiguate this call, explicitly cast it to `{0}` if you want the builder''s type parameter(s) `{1}` to be inferred to `{2}`.", RENDER_TYPE, STRING, STRING); + MAP.put(STUB_TYPE_IN_RECEIVER_CAUSES_AMBIGUITY, "The type of a receiver hasn''t been inferred yet. To disambiguate this call, explicitly cast it to `{0}` if you want the builder''s type parameter(s) `{1}` to be inferred to `{2}`.", RENDER_TYPE, STRING, STRING, null); MAP.put(NONE_APPLICABLE, "None of the following functions can be called with the arguments supplied: {0}", AMBIGUOUS_CALLS); MAP.put(CANNOT_COMPLETE_RESOLVE, "Cannot choose among the following candidates without completing type inference: {0}", AMBIGUOUS_CALLS); MAP.put(UNRESOLVED_REFERENCE_WRONG_RECEIVER, "Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: {0}", AMBIGUOUS_CALLS); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ResolutionWithStubTypesChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ResolutionWithStubTypesChecker.kt index 2b990ea4707..1fcce64f26f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ResolutionWithStubTypesChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ResolutionWithStubTypesChecker.kt @@ -112,9 +112,9 @@ class ResolutionWithStubTypesChecker(private val kotlinCallResolver: KotlinCallR context.trace.report( OVERLOAD_RESOLUTION_AMBIGUITY_BECAUSE_OF_STUB_TYPES.on( calleeExpression, - builderCalleeExpression.toString(), - typeVariablesCausedAmbiguity.toString(), - calleeExpression.toString() + builderCalleeExpression.text, + typeVariablesCausedAmbiguity.joinToString { it.originalTypeParameter.toString() }, + calleeExpression.text ) ) } @@ -140,14 +140,14 @@ class ResolutionWithStubTypesChecker(private val kotlinCallResolver: KotlinCallR if (receiverType != newReceiverType) { val typeVariables = substitutionMap.map { it.key as NewTypeVariableConstructor } val typeParameters = typeVariables.joinToString { (it.originalTypeParameter?.name ?: it).toString() } - val inferredTypes = substitutionMap.values.joinToString() + val inferredTypes = substitutionMap.values addAll(typeVariables) context.trace.report( STUB_TYPE_IN_RECEIVER_CAUSES_AMBIGUITY.on( kotlinCall.explicitReceiver?.psiExpression ?: kotlinCall.psiCall.callElement, - newReceiverType, typeParameters, inferredTypes, + newReceiverType, typeParameters, inferredTypes.joinToString(), if (relatedLambdaToLabel != null) BuilderLambdaLabelingInfo(relatedLambdaToLabel) else BuilderLambdaLabelingInfo.EMPTY ) ) @@ -171,12 +171,12 @@ class ResolutionWithStubTypesChecker(private val kotlinCallResolver: KotlinCallR val psiExpression = valueArgument.psiExpression ?: continue val typeVariables = substitutionMap.map { it.key as NewTypeVariableConstructor } val typeParameters = typeVariables.joinToString { (it.originalTypeParameter?.name ?: it).toString() } - val inferredTypes = substitutionMap.values.joinToString() + val inferredTypes = substitutionMap.values addAll(typeVariables) context.trace.report( - STUB_TYPE_IN_ARGUMENT_CAUSES_AMBIGUITY.on(psiExpression, substitutedType, typeParameters, inferredTypes) + STUB_TYPE_IN_ARGUMENT_CAUSES_AMBIGUITY.on(psiExpression, substitutedType, typeParameters, inferredTypes.joinToString()) ) } } diff --git a/compiler/testData/cli/jvm/builderInferenceErrors.args b/compiler/testData/cli/jvm/builderInferenceErrors.args new file mode 100644 index 00000000000..7584bb018bf --- /dev/null +++ b/compiler/testData/cli/jvm/builderInferenceErrors.args @@ -0,0 +1,3 @@ +$TESTDATA_DIR$/builderInferenceErrors.kt +-d +$TEMP_DIR$ diff --git a/compiler/testData/cli/jvm/builderInferenceErrors.kt b/compiler/testData/cli/jvm/builderInferenceErrors.kt new file mode 100644 index 00000000000..a386b4b4363 --- /dev/null +++ b/compiler/testData/cli/jvm/builderInferenceErrors.kt @@ -0,0 +1,8 @@ +fun main() { + val list = buildList { + add("one") + add("two") + val secondParameter = get(1) + println(secondParameter) + } +} diff --git a/compiler/testData/cli/jvm/builderInferenceErrors.out b/compiler/testData/cli/jvm/builderInferenceErrors.out new file mode 100644 index 00000000000..7bae50a2842 --- /dev/null +++ b/compiler/testData/cli/jvm/builderInferenceErrors.out @@ -0,0 +1,20 @@ +compiler/testData/cli/jvm/builderInferenceErrors.kt:6:9: error: overload resolution ambiguity: +public inline fun println(message: Any?): Unit defined in kotlin.io +public inline fun println(message: Boolean): Unit defined in kotlin.io +public inline fun println(message: Byte): Unit defined in kotlin.io +public inline fun println(message: Char): Unit defined in kotlin.io +public inline fun println(message: CharArray): Unit defined in kotlin.io +public inline fun println(message: Double): Unit defined in kotlin.io +public inline fun println(message: Float): Unit defined in kotlin.io +public inline fun println(message: Int): Unit defined in kotlin.io +public inline fun println(message: Long): Unit defined in kotlin.io +public inline fun println(message: Short): Unit defined in kotlin.io + println(secondParameter) + ^ +compiler/testData/cli/jvm/builderInferenceErrors.kt:6:9: error: no type argument for type parameter(s) `E` of the `buildList` builder specified. Cannot choose which overloaded function `println` to call. To disambiguate this call, either use an explicit type cast for a parameter or a receiver (see specific errors on them) or specify the type `E` explicitly. + println(secondParameter) + ^ +compiler/testData/cli/jvm/builderInferenceErrors.kt:6:17: error: the type of an argument hasn't been inferred yet. To disambiguate this call, explicitly cast it to `String` if you want the builder's type parameter(s) `E` to be inferred to `String`. + println(secondParameter) + ^ +COMPILATION_ERROR diff --git a/compiler/tests-gen/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/cli/CliTestGenerated.java index 3603fe88673..cc45d190461 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -111,6 +111,11 @@ public class CliTestGenerated extends AbstractCliTest { runTest("compiler/testData/cli/jvm/builderInferenceByDefault.args"); } + @TestMetadata("builderInferenceErrors.args") + public void testBuilderInferenceErrors() throws Exception { + runTest("compiler/testData/cli/jvm/builderInferenceErrors.args"); + } + @TestMetadata("classAndFileClassClash.args") public void testClassAndFileClassClash() throws Exception { runTest("compiler/testData/cli/jvm/classAndFileClassClash.args");