Skip to content

Commit 1d09d41

Browse files
committed
Fix advanced candidate setup
Use NoPrefix since that way the info of the TermRef is guaranteed to be the info of the Symbol. More tests for overloading behavior of extension methods
1 parent f611dbf commit 1d09d41

File tree

2 files changed

+97
-11
lines changed

2 files changed

+97
-11
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
16521652
val strippedInfo = strippedType(cand.widen)
16531653
if (strippedInfo.exists) {
16541654
val sym = cand.symbol.asTerm.copy(info = strippedInfo)
1655-
(TermRef(cand.prefix, sym), cand) :: Nil
1655+
(TermRef(NoPrefix, sym), cand) :: Nil
16561656
}
16571657
else Nil
16581658
}

tests/run/extmethod-overload.scala

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,104 @@ object Test extends App {
1919
assert(h(1)(1)(2) == 2)
2020
assert(h(1)(1)("two") == 3)
2121

22-
implied Foo {
23-
def (x: Int) |+| (y: Int) = x + y
24-
def (x: Int) |+| (y: String) = x + y.length
22+
// Test with extension methods in implied object
23+
object test1 {
2524

26-
def (xs: List[T]) +++ [T] (ys: List[T]): List[T] = xs ++ ys ++ ys
27-
def (xs: List[T]) +++ [T] (ys: Iterator[T]): List[T] = xs ++ ys ++ ys
25+
implied Foo {
26+
def (x: Int) |+| (y: Int) = x + y
27+
def (x: Int) |+| (y: String) = x + y.length
28+
29+
def (xs: List[T]) +++ [T] (ys: List[T]): List[T] = xs ++ ys ++ ys
30+
def (xs: List[T]) +++ [T] (ys: Iterator[T]): List[T] = xs ++ ys ++ ys
31+
}
32+
33+
assert((1 |+| 2) == 3)
34+
assert((1 |+| "2") == 2)
35+
36+
val xs = List(1, 2)
37+
assert((xs +++ xs).length == 6)
38+
assert((xs +++ xs.iterator).length == 4, xs +++ xs.iterator)
39+
}
40+
test1
41+
42+
// Test with imported extension methods
43+
object test2 {
44+
import test1.Foo._
45+
46+
assert((1 |+| 2) == 3)
47+
assert((1 |+| "2") == 2)
48+
49+
val xs = List(1, 2)
50+
assert((xs +++ xs).length == 6)
51+
assert((xs +++ xs.iterator).length == 4, xs +++ xs.iterator)
2852
}
53+
test2
54+
55+
// Test with implied extension methods coming from base class
56+
object test3 {
57+
class Foo {
58+
def (x: Int) |+| (y: Int) = x + y
59+
def (x: Int) |+| (y: String) = x + y.length
60+
61+
def (xs: List[T]) +++ [T] (ys: List[T]): List[T] = xs ++ ys ++ ys
62+
def (xs: List[T]) +++ [T] (ys: Iterator[T]): List[T] = xs ++ ys ++ ys
63+
}
64+
implied Bar for Foo
65+
66+
assert((1 |+| 2) == 3)
67+
assert((1 |+| "2") == 2)
68+
69+
val xs = List(1, 2)
70+
assert((xs +++ xs).length == 6)
71+
assert((xs +++ xs.iterator).length == 4, xs +++ xs.iterator)
72+
}
73+
test3
74+
75+
// Test with implied extension methods coming from implied alias
76+
object test4 {
77+
implied for test3.Foo = test3.Bar
78+
79+
assert((1 |+| 2) == 3)
80+
assert((1 |+| "2") == 2)
2981

30-
assert((1 |+| 2) == 3)
31-
assert((1 |+| "2") == 2)
82+
val xs = List(1, 2)
83+
assert((xs +++ xs).length == 6)
84+
assert((xs +++ xs.iterator).length == 4, xs +++ xs.iterator)
85+
}
86+
test4
87+
88+
class C {
89+
def xx (x: Any) = 2
90+
}
91+
def (c: C) xx (x: Int) = 1
92+
93+
val c = new C
94+
assert(c.xx(1) == 2) // member method takes precedence
95+
96+
object D {
97+
def (x: Int) yy (y: Int) = x + y
98+
}
99+
100+
implied {
101+
def (x: Int) yy (y: Int) = x - y
102+
}
103+
104+
import D._
105+
assert((1 yy 2) == 3) // imported extension method takes precedence
106+
107+
trait Rectangle {
108+
def a: Long
109+
def b: Long
110+
}
111+
112+
case class GenericRectangle(a: Long, b: Long) extends Rectangle
113+
case class Square(a: Long) extends Rectangle {
114+
def b: Long = a
115+
}
32116

33-
val xs = List(1, 2)
34-
assert((xs +++ xs).length == 6)
35-
assert((xs +++ xs.iterator).length == 4, xs +++ xs.iterator)
117+
def (rectangle: Rectangle) area: Long = 0
118+
def (square: Square) area: Long = square.a * square.a
119+
val rectangles = List(GenericRectangle(2, 3), Square(5))
120+
val areas = rectangles.map(_.area)
121+
assert(areas.sum == 0)
36122
}

0 commit comments

Comments
 (0)