[FE 1.0] Fix mistakes in error messages for resolution ambiguity with stub types

^KT-51022 Fixed
This commit is contained in:
Victor Petukhov
2022-01-28 13:14:27 +03:00
parent a54c9edaad
commit 78f4b9f1cd
6 changed files with 49 additions and 12 deletions
@@ -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);
@@ -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())
)
}
}
+3
View File
@@ -0,0 +1,3 @@
$TESTDATA_DIR$/builderInferenceErrors.kt
-d
$TEMP_DIR$
+8
View File
@@ -0,0 +1,8 @@
fun main() {
val list = buildList {
add("one")
add("two")
val secondParameter = get(1)
println(secondParameter)
}
}
+20
View File
@@ -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
@@ -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");