Skip to content

Commit

Permalink
Try implict searching after finding dynamic select
Browse files Browse the repository at this point in the history
  • Loading branch information
noti0na1 committed Jan 7, 2025
1 parent dc0bb59 commit 1f28390
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
else EmptyTree

def dynamicSelect(pt: Type) =
val tree2 = cpy.Select(tree0)(untpd.TypedSplice(qual), selName)
if pt.isInstanceOf[FunOrPolyProto] || pt == LhsProto then
assignType(tree2, TryDynamicCallType)
else
typedDynamicSelect(tree2, Nil, pt)
val tree2 = cpy.Select(tree0)(untpd.TypedSplice(qual), selName)
if pt.isInstanceOf[FunOrPolyProto] || pt == LhsProto then
assignType(tree2, TryDynamicCallType)
else
typedDynamicSelect(tree2, Nil, pt)

// Otherwise, if the qualifier derives from class Dynamic, expand to a
// dynamic dispatch using selectDynamic or applyDynamic
Expand Down Expand Up @@ -885,7 +885,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
// Reject corner case where selectDynamic needs annother selectDynamic to be called. E.g. as in neg/unselectable-fields.scala.
report.error(i"Cannot use selectDynamic here since it needs another selectDynamic to be invoked", tree.srcPos)
case _ =>
dynSelected.ensureConforms(fieldType)
adapt(dynSelected, defn.AnyType).ensureConforms(fieldType)
case _ => EmptyTree
else EmptyTree

Expand Down
26 changes: 26 additions & 0 deletions tests/pos/i20512.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import language.experimental.namedTuples

import NamedTuple.*

trait Selector1 extends Selectable {
type Fields = (int: Int, str: String)

def selectDynamic(name: String)(using name.type <:< Tuple.Union[NamedTuple.Names[Fields]]) = ???
}

def test20512 = {
val s: Selector1 = new Selector1 {}
val int = s.int
val str = s.str
}

trait Ctx

class Selector2 extends Selectable:
type Fields = (bar: Int, baz: Int)
def selectDynamic(fieldName: String)(using Ctx): Any = ???

def test22023(using Ctx) =
val f = Selector2()
val bar = f.bar
val baz = f.baz

0 comments on commit 1f28390

Please sign in to comment.