Skip to content

Commit 80f977d

Browse files
committed
Fix #657: Add scala.Dynamic support.
1 parent b35eff9 commit 80f977d

38 files changed

+580
-10
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,8 @@ object Mode {
8484
/** Use Scala2 scheme for overloading and implicit resolution */
8585
val OldOverloadingResolution = newMode(14, "OldOverloadingResolution")
8686

87+
/* Currently inside a potential scala.Dynamic rewrite. */
88+
val IgnoreNextDynamic = newMode(15, "IgnoreNextDynamic")
89+
8790
val PatternOrType = Pattern | Type
8891
}

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: 25 additions & 2 deletions
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
@@ -541,7 +542,22 @@ trait Applications extends Compatibility { self: Typer =>
541542

542543
def realApply(implicit ctx: Context): Tree = track("realApply") {
543544
var proto = new FunProto(tree.args, IgnoredProto(pt), this)(argCtx(tree))
544-
val fun1 = typedExpr(tree.fun, proto)
545+
546+
/* In the default case we type using Mode.IgnoreNextDynamic because fun1 might be a Select(foo, bar) where foo is of
547+
* type scala.Dynamic and bar is not one of its members. In which case the following transformation will be tried:
548+
* foo.bar(baz0, baz1, ...) ~~> foo.applyDynamic(bar)(baz0, baz1, ...)
549+
* Mode.IgnoreNextDynamic will ensure that Apply(Select(foo, bar), baz) is not transformed
550+
* into foo.selectDynamic(bar).apply(args).
551+
* We remove Mode.IgnoreNextDynamic if the name of the called method if is one of the scala.Dynamic methods or
552+
* if this node was generated by an assign (see typedAssign).
553+
* See: Dynamic.scala
554+
*/
555+
def generatedByTypedAssign(name: Name) = name == nme.update && ctx.mode.is(Mode.IgnoreNextDynamic)
556+
val ctx1 = tree.fun match {
557+
case Select(_, name) if isDynamicMethod(name) || generatedByTypedAssign(name) => ctx.retractMode(Mode.IgnoreNextDynamic)
558+
case _ => ctx.addMode(Mode.IgnoreNextDynamic)
559+
}
560+
val fun1 = typedExpr(tree.fun, proto)(ctx1)
545561

546562
// Warning: The following line is dirty and fragile. We record that auto-tupling was demanded as
547563
// a side effect in adapt. If it was, we assume the tupled proto-type in the rest of the application.
@@ -553,6 +569,13 @@ trait Applications extends Compatibility { self: Typer =>
553569

554570
fun1.tpe match {
555571
case ErrorType => tree.withType(ErrorType)
572+
case TryDynamicCallType =>
573+
tree match {
574+
case tree @ Apply(Select(qual, name), args) if !isDynamicMethod(name) =>
575+
typedDynamicApply(qual, name, args, pt)(tree)
576+
case _ =>
577+
handleUnexpectedFunType(tree, fun1)
578+
}
556579
case _ => methPart(fun1).tpe match {
557580
case funRef: TermRef =>
558581
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/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 <:< defn.DynamicType && !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 <:< defn.DynamicType) {
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: 29 additions & 6 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
@@ -314,9 +315,12 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
314315

315316
def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = track("typedSelect") {
316317
def asSelect(implicit ctx: Context): Tree = {
317-
val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this))
318+
val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this))(ctx.retractMode(Mode.IgnoreNextDynamic))
318319
if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.pos)
319-
typedSelect(tree, pt, qual1)
320+
val select = typedSelect(tree, pt, qual1)
321+
if (select.tpe == TryDynamicCallType && !ctx.mode.is(Mode.IgnoreNextDynamic)) {
322+
typedDynamicSelect(tree, pt)
323+
} else select
320324
}
321325

322326
def asJavaSelectFromTypeTree(implicit ctx: Context): Tree = {
@@ -471,7 +475,14 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
471475
def typedAssign(tree: untpd.Assign, pt: Type)(implicit ctx: Context) = track("typedAssign") {
472476
tree.lhs match {
473477
case lhs @ Apply(fn, args) =>
474-
typed(cpy.Apply(lhs)(untpd.Select(fn, nme.update), args :+ tree.rhs), pt)
478+
/* We type using Mode.IgnoreNextDynamic because fn might be a Select(foo, bar) where foo is of type
479+
* scala.Dynamic and bar is not one of its members. In which case the following transformation will be tried:
480+
* foo.bar(baz) = quux ~~> foo.selectDynamic("bar").update(baz, quux)
481+
* Mode.IgnoreNextDynamic will ensure that Apply(Select(fn, update), args) is not transformed
482+
* into fn.applyDynamic(update)(args) and will only transform the contents of fn if needed.
483+
* See: Dynamic.scala
484+
*/
485+
typed(cpy.Apply(lhs)(untpd.Select(fn, nme.update), args :+ tree.rhs), pt)(ctx.addMode(Mode.IgnoreNextDynamic))
475486
case untpd.TypedSplice(Apply(MaybePoly(Select(fn, app), targs), args)) if app == nme.apply =>
476487
val rawUpdate: untpd.Tree = untpd.Select(untpd.TypedSplice(fn), nme.update)
477488
val wrappedUpdate =
@@ -480,7 +491,13 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
480491
val appliedUpdate = cpy.Apply(fn)(wrappedUpdate, (args map untpd.TypedSplice) :+ tree.rhs)
481492
typed(appliedUpdate, pt)
482493
case lhs =>
483-
val lhsCore = typedUnadapted(lhs)
494+
/* We type using Mode.IgnoreNextDynamic because this tree might be transformed with:
495+
* foo.bar = baz ~~> foo.updateDynamic("bar")(baz)
496+
* Mode.IgnoreNextDynamic will ensure that Assign(Select(foo, bar), baz) will not be transformed
497+
* into foo.selectDynamic(bar) = baz during the typing of the lhs.
498+
* See: Dynamic.scala
499+
*/
500+
val lhsCore = typedUnadapted(lhs)(ctx.addMode(Mode.IgnoreNextDynamic))
484501
def lhs1 = typed(untpd.TypedSplice(lhsCore))
485502
def canAssign(sym: Symbol) = // allow assignments from the primary constructor to class fields
486503
sym.is(Mutable, butNot = Accessor) ||
@@ -508,6 +525,12 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
508525
case _ =>
509526
reassignmentToVal
510527
}
528+
case TryDynamicCallType =>
529+
tree match {
530+
case Assign(Select(qual, name), rhs) if !isDynamicMethod(name) =>
531+
typedDynamicAssign(qual, name, rhs, pt)
532+
case _ => reassignmentToVal
533+
}
511534
case tpe =>
512535
reassignmentToVal
513536
}
@@ -1665,7 +1688,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
16651688
tree match {
16661689
case _: MemberDef | _: PackageDef | _: Import | _: WithoutTypeOrPos[_] => tree
16671690
case _ => tree.tpe.widen match {
1668-
case ErrorType =>
1691+
case _: ErrorType =>
16691692
tree
16701693
case ref: TermRef =>
16711694
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+
}

0 commit comments

Comments
 (0)