Skip to content

Commit 83858e1

Browse files
committed
Allow apply adaptation on Selectable with Fields
Just like when using a regular Selectable with refinements, this change allows: fromFlds.xs(0) to be expanded to: fromFlds.xs.apply(0)
1 parent 3fdb292 commit 83858e1

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
821821

822822
// Otherwise, if the qualifier derives from class Selectable,
823823
// and the selector name matches one of the element of the `Fields` type member,
824-
// and the selector is neither applied nor assigned to,
824+
// and the selector is not assigned to,
825825
// expand to a typed dynamic dispatch using selectDynamic wrapped in a cast
826826
if qual.tpe.derivesFrom(defn.SelectableClass) && !isDynamicExpansion(tree)
827-
&& !pt.isInstanceOf[FunOrPolyProto] && pt != LhsProto
827+
&& pt != LhsProto
828828
then
829829
val pre = if !TypeOps.isLegalPrefix(qual.tpe) then SkolemType(qual.tpe) else qual.tpe
830830
val fieldsType = pre.select(tpnme.Fields).dealias.simplified
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import scala.language.experimental.namedTuples.*
2+
3+
class FromFields extends Selectable:
4+
type Fields = (i: Int)
5+
def selectDynamic(key: String) =
6+
List(1, 2, 3)
7+
8+
trait FromRefs extends Selectable:
9+
def selectDynamic(key: String) =
10+
List(1, 2, 3)
11+
12+
def test(
13+
fromFlds: FromFields,
14+
fromRefs: FromRefs { val i: Int }
15+
): Unit =
16+
fromFlds.i(0) // error
17+
fromRefs.i(0) // error
18+
19+
fromFlds.i.apply(0) // error
20+
fromRefs.i.apply(0) // error
21+
22+
fromFlds.i[Int](List(1)) // error
23+
fromRefs.i[Int](List(1)) // error
24+
25+
fromFlds.i(List(1)) // error
26+
fromRefs.i(List(1)) // error
27+
28+
fromFlds.i.apply(List(1)) // error
29+
fromRefs.i.apply(List(1)) // error
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import scala.language.experimental.namedTuples.*
2+
3+
class FromFields extends Selectable:
4+
type Fields = (xs: List[Int], poly: [T] => (x: List[T]) => Option[T])
5+
def selectDynamic(key: String) =
6+
List(1, 2, 3)
7+
8+
trait FromRefs extends Selectable:
9+
def selectDynamic(key: String) =
10+
List(1, 2, 3)
11+
12+
def test(
13+
fromFlds: FromFields,
14+
fromRefs: FromRefs { val xs: List[Int]; val poly: [T] => (x: List[T]) => Option[T] }
15+
): Unit =
16+
fromFlds.xs(0)
17+
fromRefs.xs(0)
18+
19+
fromFlds.xs.apply(0)
20+
fromRefs.xs.apply(0)
21+
22+
fromFlds.poly[Int](List(1)): Option[Int]
23+
fromRefs.poly[Int](List(1)): Option[Int]
24+
25+
fromFlds.poly(List(1))
26+
fromRefs.poly(List(1))
27+
28+
fromFlds.poly.apply(List(1))
29+
fromRefs.poly.apply(List(1))

0 commit comments

Comments
 (0)