Skip to content

Commit 75da035

Browse files
committed
Fix #657: Add scala.Dynamic support.
1 parent 07fd8a3 commit 75da035

38 files changed

+551
-7
lines changed

src/dotty/tools/dotc/core/Types.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,6 +3021,9 @@ object Types {
30213021

30223022
object ErrorType extends ErrorType
30233023

3024+
/* Type used to track Select nodes that could not resolve a member and their qualifier is a scala.Dynamic. */
3025+
object TryDynamicCallType extends ErrorType
3026+
30243027
/** Wildcard type, possibly with bounds */
30253028
abstract case class WildcardType(optBounds: Type) extends CachedGroundType with TermType {
30263029
def derivedWildcardType(optBounds: Type)(implicit ctx: Context) =

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ object Applications {
8686

8787
import Applications._
8888

89-
trait Applications extends Compatibility { self: Typer =>
89+
trait Applications extends Compatibility { self: Typer with Dynamic =>
9090

9191
import Applications._
9292
import tpd.{ cpy => _, _ }
9393
import untpd.cpy
94+
import Dynamic.isDynamicMethod
9495

9596
/** @tparam Arg the type of arguments, could be tpd.Tree, untpd.Tree, or Type
9697
* @param methRef the reference to the method of the application
@@ -553,6 +554,13 @@ trait Applications extends Compatibility { self: Typer =>
553554

554555
fun1.tpe match {
555556
case ErrorType => tree.withType(ErrorType)
557+
case TryDynamicCallType =>
558+
tree match {
559+
case tree @ Apply(Select(qual, name), args) if !isDynamicMethod(name) =>
560+
typedDynamicApply(qual, name, args, pt)(tree)
561+
case _ =>
562+
handleUnexpectedFunType(tree, fun1)
563+
}
556564
case _ => methPart(fun1).tpe match {
557565
case funRef: TermRef =>
558566
tryEither { implicit ctx =>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package dotty.tools
2+
package dotc
3+
package typer
4+
5+
import dotty.tools.dotc.ast.Trees.NamedArg
6+
import dotty.tools.dotc.ast.tpd._
7+
import dotty.tools.dotc.ast.untpd
8+
import dotty.tools.dotc.core.Constants.Constant
9+
import dotty.tools.dotc.core.Contexts.Context
10+
import dotty.tools.dotc.core.Names.Name
11+
import dotty.tools.dotc.core.StdNames._
12+
import dotty.tools.dotc.core.Types._
13+
import dotty.tools.dotc.core.Mode
14+
import dotty.tools.dotc.core.Decorators._
15+
16+
object Dynamic {
17+
def isDynamicMethod(name: Name): Boolean =
18+
name == nme.applyDynamic || name == nme.selectDynamic || name == nme.updateDynamic || name == nme.applyDynamicNamed
19+
}
20+
21+
/** Translates selection that does not typecheck according to the scala.Dynamic rules:
22+
* foo.bar(baz) = quux ~~> foo.selectDynamic(bar).update(baz, quux)
23+
* foo.bar = baz ~~> foo.updateDynamic("bar")(baz)
24+
* foo.bar(x = bazX, y = bazY, baz, ...) ~~> foo.applyDynamicNamed("bar")(("x", bazX), ("y", bazY), ("", baz), ...)
25+
* foo.bar(baz0, baz1, ...) ~~> foo.applyDynamic(bar)(baz0, baz1, ...)
26+
* foo.bar ~~> foo.selectDynamic(bar)
27+
*
28+
* The first matching rule of is applied.
29+
*/
30+
trait Dynamic { self: Typer with Applications =>
31+
32+
/** Translate selection that does not typecheck according to the normal rules into a applyDynamic/applyDynamicNamed.
33+
* foo.bar(baz0, baz1, ...) ~~> foo.applyDynamic(bar)(baz0, baz1, ...)
34+
* foo.bar(x = bazX, y = bazY, baz, ...) ~~> foo.applyDynamicNamed("bar")(("x", bazX), ("y", bazY), ("", baz), ...)
35+
*/
36+
def typedDynamicApply(qual: untpd.Tree, name: Name, args: List[untpd.Tree], pt: Type)(original: untpd.Apply)(
37+
implicit ctx: Context): Tree = {
38+
def isNamedArg(arg: untpd.Tree): Boolean = arg match { case NamedArg(_, _) => true; case _ => false }
39+
val dynName = if (args.exists(isNamedArg)) nme.applyDynamicNamed else nme.applyDynamic
40+
if (dynName == nme.applyDynamicNamed && untpd.isWildcardStarArgList(args)) {
41+
ctx.error("applyDynamicNamed does not support passing a vararg parameter", original.pos)
42+
original.withType(ErrorType)
43+
} else {
44+
def namedArgTuple(name: String, arg: untpd.Tree) = untpd.Tuple(List(Literal(Constant(name)), arg))
45+
def namedArgs = args.map {
46+
case NamedArg(argName, arg) => namedArgTuple(argName.toString, arg)
47+
case arg => namedArgTuple("", arg)
48+
}
49+
val args1 = if (dynName == nme.applyDynamic) args else namedArgs
50+
typedApply(untpd.Apply(coreDynamic(qual, dynName, name), args1), pt)
51+
}
52+
}
53+
54+
/** Translate selection that does not typecheck according to the normal rules into a selectDynamic.
55+
* foo.bar ~~> foo.selectDynamic(bar)
56+
*
57+
* Note: inner part of translation foo.bar(baz) = quux ~~> foo.selectDynamic(bar).update(baz, quux) is achieved
58+
* through an existing transformation of in typedAssign [foo.bar(baz) = quux ~~> foo.bar.update(baz, quux)].
59+
*/
60+
def typedDynamicSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree =
61+
typedApply(coreDynamic(tree.qualifier, nme.selectDynamic, tree.name), pt)
62+
63+
/** Translate selection that does not typecheck according to the normal rules into a updateDynamic.
64+
* foo.bar = baz ~~> foo.updateDynamic(bar)(baz)
65+
*/
66+
def typedDynamicAssign(qual: untpd.Tree, name: Name, rhs: untpd.Tree, pt: Type)(implicit ctx: Context): Tree =
67+
typedApply(untpd.Apply(coreDynamic(qual, nme.updateDynamic, name), rhs), pt)
68+
69+
private def coreDynamic(qual: untpd.Tree, dynName: Name, name: Name)(implicit ctx: Context): untpd.Apply =
70+
untpd.Apply(untpd.Select(qual, dynName), Literal(Constant(name.toString)))
71+
}

src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ object ProtoTypes {
430430
(if (theMap != null) theMap else new WildApproxMap).mapOver(tp)
431431
}
432432

433+
@sharable object AssignProto extends UncachedGroundType with MatchAlways
434+
433435
private[ProtoTypes] class WildApproxMap(implicit ctx: Context) extends TypeMap {
434436
def apply(tp: Type) = wildApprox(tp, this)
435437
}

src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,16 @@ trait TypeAssigner {
196196
def selectionType(site: Type, name: Name, pos: Position)(implicit ctx: Context): Type = {
197197
val mbr = site.member(name)
198198
if (reallyExists(mbr)) site.select(name, mbr)
199-
else {
199+
else if (site.derivesFrom(defn.DynamicClass) && !Dynamic.isDynamicMethod(name)) {
200+
TryDynamicCallType
201+
} else {
200202
if (!site.isErroneous) {
201203
ctx.error(
202204
if (name == nme.CONSTRUCTOR) d"$site does not have a constructor"
203-
else d"$name is not a member of $site", pos)
205+
else if (site.derivesFrom(defn.DynamicClass)) {
206+
d"$name is not a member of $site\n" +
207+
"possible cause: maybe a wrong Dynamic method signature?"
208+
} else d"$name is not a member of $site", pos)
204209
}
205210
ErrorType
206211
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ object Typer {
5858
assert(tree.pos.exists, s"position not set for $tree # ${tree.uniqueId}")
5959
}
6060

61-
class Typer extends Namer with TypeAssigner with Applications with Implicits with Checking {
61+
class Typer extends Namer with TypeAssigner with Applications with Implicits with Dynamic with Checking {
6262

6363
import Typer._
6464
import tpd.{cpy => _, _}
6565
import untpd.cpy
66+
import Dynamic.isDynamicMethod
6667

6768
/** A temporary data item valid for a single typed ident:
6869
* The set of all root import symbols that have been
@@ -316,7 +317,13 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
316317
def asSelect(implicit ctx: Context): Tree = {
317318
val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this))
318319
if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.pos)
319-
typedSelect(tree, pt, qual1)
320+
val select = typedSelect(tree, pt, qual1)
321+
pt match {
322+
case _: FunProto | AssignProto => select
323+
case _ =>
324+
if (select.tpe eq TryDynamicCallType) typedDynamicSelect(tree, pt)
325+
else select
326+
}
320327
}
321328

322329
def asJavaSelectFromTypeTree(implicit ctx: Context): Tree = {
@@ -480,7 +487,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
480487
val appliedUpdate = cpy.Apply(fn)(wrappedUpdate, (args map untpd.TypedSplice) :+ tree.rhs)
481488
typed(appliedUpdate, pt)
482489
case lhs =>
483-
val lhsCore = typedUnadapted(lhs)
490+
val lhsCore = typedUnadapted(lhs, AssignProto)
484491
def lhs1 = typed(untpd.TypedSplice(lhsCore))
485492
def canAssign(sym: Symbol) = // allow assignments from the primary constructor to class fields
486493
sym.is(Mutable, butNot = Accessor) ||
@@ -508,6 +515,12 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
508515
case _ =>
509516
reassignmentToVal
510517
}
518+
case TryDynamicCallType =>
519+
tree match {
520+
case Assign(Select(qual, name), rhs) if !isDynamicMethod(name) =>
521+
typedDynamicAssign(qual, name, rhs, pt)
522+
case _ => reassignmentToVal
523+
}
511524
case tpe =>
512525
reassignmentToVal
513526
}
@@ -1665,7 +1678,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
16651678
tree match {
16661679
case _: MemberDef | _: PackageDef | _: Import | _: WithoutTypeOrPos[_] => tree
16671680
case _ => tree.tpe.widen match {
1668-
case ErrorType =>
1681+
case _: ErrorType =>
16691682
tree
16701683
case ref: TermRef =>
16711684
pt match {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic
4+
5+
object DynamicTest {
6+
new Foo().bazApply() // error
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic
4+
5+
object DynamicTest {
6+
new Foo().bazApply("abc", 1) // error
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic
4+
5+
object DynamicTest {
6+
new Foo().bazApply _ // error // error
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def selectDynamic(name: String): String = ???
5+
def applyDynamicNamed(name: String)(args: Any*): String = ???
6+
}
7+
8+
object DynamicTest {
9+
new Foo().bazApply() // error
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamic(name: String)(args: String*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
new Foo().bazApply(1, 2, 3) // error // error // error
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamic(name: String)(args: String*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
def test: Int = new Foo().bazApply() // error
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamic(name: Int)(args: String*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
def test: String = new Foo().bazApply() // error
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamicNamed(name: String)(args: (String, Int)*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
def test: String = new Foo().bazApply("1" -> 2) // error
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamicNamed(name: String)(args: (String, Int)*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
new Foo().applyDynamic("bar")("1" -> 2) // error
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic
4+
5+
object DynamicTest {
6+
new Foo().bazApply(a = "abc", b = 1) // error
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def selectDynamic(name: String): Array[String] = ???
5+
def applyDynamic(name: String)(args: Any*): String = ???
6+
}
7+
8+
object DynamicTest {
9+
new Foo().bazApply(a = "abc", b = 1) // error
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic
4+
5+
object DynamicTest {
6+
new Foo().bazApply("abc", b = 1) // error
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic
4+
5+
object DynamicTest {
6+
new Foo().bazApply("abc", 4, b = 1, b = "bcd") // error
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamicNamed(name: String)(args: String*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
new Foo().bazApply("abc", 4, b = 1, b = "bcd") // error // error // error // error
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamicNamed(name: Int)(args: Any*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
new Foo().bazApply("abc", 4, b = 1, b = "bcd") // error
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamicNamed(name: String)(args: Any*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
def test: Int = new Foo().bazApply("abc", 4, b = 1, b = "bcd") // error
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamic(name: String)(args: Any*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
new Foo().applyDynamicNamed("abc")() // error
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def selectDynamic(name: String): String = ???
5+
}
6+
7+
object DynamicTest {
8+
implicit class Bar(foo: Foo) {
9+
def bazSelect: Int = ???
10+
}
11+
12+
def baz: String = new Foo().bazSelect // error
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applyDynamic(name: String)(args: Any*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
implicit class Bar(foo: Foo) {
9+
def bazApply: Int = ???
10+
}
11+
12+
def baz: String = new Foo().bazApply("") // error
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.language.dynamics
2+
3+
class Foo extends scala.Dynamic {
4+
def applytDynamicNamed(name: String)(args: Any*): String = ???
5+
}
6+
7+
object DynamicTest {
8+
implicit class Bar(foo: Foo) {
9+
def bazApply: Int = ???
10+
}
11+
12+
def baz: String = new Foo().bazApply(a = "") // error
13+
}

0 commit comments

Comments
 (0)