Skip to content

Commit c8d49c9

Browse files
committed
Add tests tests/(neg|run)/overload_repeated for #24072
1 parent e8cc49b commit c8d49c9

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

tests/neg/overload_repeated.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@ object Test {
22
def bar1(x: Any) = 1
33
def bar1(x: String*) = 2
44

5+
// (b) isn't as good as (a) because of ints
6+
// (a) isn't as good as (b) because T in Class[? <: T] may not subtype of Number
7+
def bar2[T](a: Class[? <: T]): Int = 1 // (a)
8+
def bar2[T <: Number](a: Class[T], ints: Int*): Int = 2 // (b)
9+
510
assert(bar1("") == 1) // error: ambiguous in Scala 2 and Scala 3
11+
assert(bar2(classOf[Integer]) == 1) // error: ambiguous in Scala 2 and Scala 3
612
}

tests/run/overload_repeated/A_1.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,34 @@ class A_1 {
1111
public static <T> int foo4(T x) { return 1; }
1212
public static <T> int foo4(T x, T... y) { return 2; }
1313

14+
// https://github.com/scala/scala3/issues/24072
15+
public static <T> int foo5(Class<? extends T> a) { return 1; }
16+
public static <T> int foo5(Class<T> a, int... ints) { return 2; }
17+
18+
public static <T> int foo6(Class<T> a, int... ints) { return 1; }
19+
public static <T> int foo6(int a) { return 2; }
20+
21+
public static <T extends Number> int foo7(Class<? extends T> a) { return 1; }
22+
public static <T extends Number> int foo7(Class<T> a, int... ints) { return 2; }
23+
24+
public static <T extends Number> int foo8(Class<? extends T> a) { return 1; } // (a)
25+
public static <T> int foo8(Class<T> a, int... ints) { return 2; } // (b)
26+
27+
public static <T> int foo9(Class<? extends T> a) { return 1; } // (a)
28+
public static <T extends Number> int foo9(Class<T> a, int... ints) { return 2; } // (b)
29+
1430
public static boolean check() {
1531
// Java prefers non-varargs to varargs:
1632
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2
1733
return
1834
foo1("") == 1 &&
1935
foo2("") == 1 &&
2036
foo3("") == 1 &&
21-
foo4("") == 1;
37+
foo4("") == 1 &&
38+
foo5(Object.class) == 1 &&
39+
foo6(Object.class) == 1 &&
40+
foo7(Integer.class) == 1 &&
41+
foo8(Integer.class) == 1 &&
42+
foo9(Integer.class) == 1;
2243
}
2344
}

tests/run/overload_repeated/B_2.scala

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ object Test {
1717
def bar5[T](a: Class[? <: T]): Int = 1
1818
def bar5[T](a: Class[T], ints: Int*): Int = 2
1919

20-
def bar6[T](a: Int): Int = 1
21-
def bar6[T](a: Class[T], ints: Int*): Int = 2
20+
def bar6[T](a: Class[T], ints: Int*): Int = 1
21+
def bar6[T](a: Int): Int = 2
22+
23+
def bar7[T <: Number](a: Class[? <: T]): Int = 1
24+
def bar7[T <: Number](a: Class[T], ints: Int*): Int = 2
25+
26+
def bar8[T <: Number](a: Class[? <: T]): Int = 1 // (a)
27+
def bar8[T](a: Class[T], ints: Int*): Int = 2 // (b)
28+
29+
def bar9[T](a: Class[? <: T]): Int = 1 // (a)
30+
def bar9[T <: Number](a: Class[T], ints: Int*): Int = 2 // (b)
2231

2332
def main(args: Array[String]): Unit = {
2433
// In Java, varargs are always less specific than non-varargs (see
@@ -29,13 +38,21 @@ object Test {
2938
assert(A_1.foo2("") == 1) // Same as in Java and Scala 2
3039
assert(A_1.foo3("") == 1) // Same as in Java and Scala 2
3140
assert(A_1.foo4("") == 1) // Same as in Java and Scala 2
41+
assert(A_1.foo5(classOf[Object]) == 1) // Same as in Java and Scala 2
42+
assert(A_1.foo6(classOf[Object]) == 1) // Same as in Java and Scala 2
43+
assert(A_1.foo7(classOf[Integer]) == 1) // Same as in Java and Scala 2
44+
assert(A_1.foo8(classOf[Integer]) == 1) // Same as in Java and Scala 2
45+
// assert(A_1.foo9(classOf[Integer]) == 1) // Works in Java, ambiguous in Scala 2 and 3
3246

3347
// Same with Scala varargs:
3448
// assert(bar1("") == 1) // Works in Java, ambiguous in Scala 2 and Dotty
3549
assert(bar2("") == 1) // same in Scala 2
3650
assert(bar3("") == 1) // same in Scala 2
3751
assert(bar4("") == 1) // same in Scala 2
3852
assert(bar5(classOf[Object]) == 1) // same in Scala2
39-
assert(bar6(classOf[Object]) == 2) // same in Scala2
53+
assert(bar6(classOf[Object]) == 1) // same in Scala2
54+
assert(bar7(classOf[Integer]) == 1) // same in Scala2
55+
assert(bar8(classOf[Integer]) == 1) // same in Scala2
56+
// assert(bar9(classOf[Integer]) == 1) Works in Java, ambiguous in Scala 2 and 3
4057
}
4158
}

0 commit comments

Comments
 (0)