KT-422 Tune literal typing rules so that varargs overloaded by primitive types work

#KT-422 fixed
This commit is contained in:
Svetlana Isakova
2013-01-16 15:18:51 +04:00
parent b2823c5966
commit da4f1aec1d
3 changed files with 37 additions and 6 deletions
@@ -158,18 +158,18 @@ public class OverloadingConflictResolver {
}
}
// Check the non-matching parameters of one function against the vararg parameter of the other funciton
// Check the non-matching parameters of one function against the vararg parameter of the other function
// Example:
// f(a : A, vararg vf : T)
// g(vararg vg : T)
// here we check that typeof(a) < typeof(vg) and elementTypeOf(vf) < elementTypeOf(vg)
// f(vararg vf : T)
// g(a : A, vararg vg : T)
// here we check that typeOf(a) < elementTypeOf(vf) and elementTypeOf(vg) < elementTypeOf(vf)
if (fSize < gSize) {
ValueParameterDescriptor fParam = fParams.get(fSize - 1);
JetType fParamType = fParam.getVarargElementType();
assert fParamType != null : "fIsVararg guarantees this";
for (int i = fSize - 1; i < gSize; i++) {
ValueParameterDescriptor gParam = gParams.get(i);
if (!typeMoreSpecific(fParamType, gParam.getType())) {
if (!typeMoreSpecific(fParamType, getVarargElementTypeOrType(gParam))) {
return false;
}
}
@@ -180,7 +180,7 @@ public class OverloadingConflictResolver {
assert gParamType != null : "gIsVararg guarantees this";
for (int i = gSize - 1; i < fSize; i++) {
ValueParameterDescriptor fParam = fParams.get(i);
if (!typeMoreSpecific(fParam.getType(), gParamType)) {
if (!typeMoreSpecific(getVarargElementTypeOrType(fParam), gParamType)) {
return false;
}
}
@@ -190,6 +190,15 @@ public class OverloadingConflictResolver {
return true;
}
@NotNull
private static JetType getVarargElementTypeOrType(@NotNull ValueParameterDescriptor parameterDescriptor) {
JetType varargElementType = parameterDescriptor.getVarargElementType();
if (varargElementType != null) {
return varargElementType;
}
return parameterDescriptor.getType();
}
private boolean isVariableArity(List<ValueParameterDescriptor> fParams) {
int fSize = fParams.size();
return fSize > 0 && fParams.get(fSize - 1).getVarargElementType() != null;
@@ -0,0 +1,17 @@
// KT-422 Tune literal typing rules so that varargs overloaded by primitive types work
fun foo<T>(vararg t : T) = t
fun foo(vararg a: Int) = a
fun foo(vararg a: Long) = a
fun test() {
foo(1, 2, 3) // Error, but should be foo of ints
}
fun <T> array(vararg elements : T) = elements
fun array(vararg elements : Int) = elements
fun test1() {
array("A", "A")
array(1, 1)
}
@@ -4257,6 +4257,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
doTest("compiler/testData/diagnostics/tests/varargs/kt1838-val.kt");
}
@TestMetadata("kt422.kt")
public void testKt422() throws Exception {
doTest("compiler/testData/diagnostics/tests/varargs/kt422.kt");
}
@TestMetadata("MoreSpecificVarargsOfEqualLength.kt")
public void testMoreSpecificVarargsOfEqualLength() throws Exception {
doTest("compiler/testData/diagnostics/tests/varargs/MoreSpecificVarargsOfEqualLength.kt");