Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow apply adaptation on Selectable with Fields #20489

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer

// Otherwise, if the qualifier derives from class Selectable,
// and the selector name matches one of the element of the `Fields` type member,
// and the selector is neither applied nor assigned to,
// and the selector is not assigned to,
// expand to a typed dynamic dispatch using selectDynamic wrapped in a cast
if qual.tpe.derivesFrom(defn.SelectableClass) && !isDynamicExpansion(tree)
&& !pt.isInstanceOf[FunOrPolyProto] && pt != LhsProto
&& pt != LhsProto
then
val pre = if !TypeOps.isLegalPrefix(qual.tpe) then SkolemType(qual.tpe) else qual.tpe
val fieldsType = pre.select(tpnme.Fields).dealias.simplified
Expand Down
29 changes: 29 additions & 0 deletions tests/neg/named-tuple-selectable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import scala.language.experimental.namedTuples

class FromFields extends Selectable:
type Fields = (i: Int)
def selectDynamic(key: String) =
List(1, 2, 3)

trait FromRefs extends Selectable:
def selectDynamic(key: String) =
List(1, 2, 3)

def test(
fromFlds: FromFields,
fromRefs: FromRefs { val i: Int }
): Unit =
fromFlds.i(0) // error
fromRefs.i(0) // error

fromFlds.i.apply(0) // error
fromRefs.i.apply(0) // error

fromFlds.i[Int](List(1)) // error
fromRefs.i[Int](List(1)) // error

fromFlds.i(List(1)) // error
fromRefs.i(List(1)) // error

fromFlds.i.apply(List(1)) // error
fromRefs.i.apply(List(1)) // error
29 changes: 29 additions & 0 deletions tests/pos/named-tuple-selectable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import scala.language.experimental.namedTuples

class FromFields extends Selectable:
type Fields = (xs: List[Int], poly: [T] => (x: List[T]) => Option[T])
def selectDynamic(key: String) =
List(1, 2, 3)

trait FromRefs extends Selectable:
def selectDynamic(key: String) =
List(1, 2, 3)

def test(
fromFlds: FromFields,
fromRefs: FromRefs { val xs: List[Int]; val poly: [T] => (x: List[T]) => Option[T] }
): Unit =
fromFlds.xs(0)
fromRefs.xs(0)

fromFlds.xs.apply(0)
fromRefs.xs.apply(0)

fromFlds.poly[Int](List(1)): Option[Int]
fromRefs.poly[Int](List(1)): Option[Int]

fromFlds.poly(List(1))
fromRefs.poly(List(1))

fromFlds.poly.apply(List(1))
fromRefs.poly.apply(List(1))