From b915f440eb3738d2991b1f96b7c810cc87b88d0c Mon Sep 17 00:00:00 2001 From: Simon Schaefer Date: Sat, 23 Nov 2013 23:45:59 +0100 Subject: SI-7463,SI-8003 Correct wrong position for {select,apply}Dynamic calls The new positions are range positions that directly refer to the beginning and the end of the method calls in the sources instead of simply point to the beginning of the expression. This allows the scala-ide to semantically highlight select- and applyDynamic method calls, because it has only to traverse the tree and apply the color ranges to the given position ranges. This also fixes the position marker of an error messages related to a wrong Dynamic method signature. --- test/files/neg/applydynamic_sip.check | 8 ++++---- test/files/run/dynamic-applyDynamic.check | 14 ++++++++++++++ test/files/run/dynamic-applyDynamic.scala | 26 ++++++++++++++++++++++++++ test/files/run/dynamic-selectDynamic.check | 13 +++++++++++++ test/files/run/dynamic-selectDynamic.scala | 25 +++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 test/files/run/dynamic-applyDynamic.check create mode 100644 test/files/run/dynamic-applyDynamic.scala create mode 100644 test/files/run/dynamic-selectDynamic.check create mode 100644 test/files/run/dynamic-selectDynamic.scala (limited to 'test/files') diff --git a/test/files/neg/applydynamic_sip.check b/test/files/neg/applydynamic_sip.check index dcf97b29fc..b50858356d 100644 --- a/test/files/neg/applydynamic_sip.check +++ b/test/files/neg/applydynamic_sip.check @@ -13,28 +13,28 @@ applydynamic_sip.scala:18: error: type mismatch; error after rewriting to Test.this.bad1.selectDynamic("sel") possible cause: maybe a wrong Dynamic method signature? bad1.sel - ^ + ^ applydynamic_sip.scala:19: error: type mismatch; found : String("sel") required: Int error after rewriting to Test.this.bad1.applyDynamic("sel") possible cause: maybe a wrong Dynamic method signature? bad1.sel(1) - ^ + ^ applydynamic_sip.scala:20: error: type mismatch; found : String("sel") required: Int error after rewriting to Test.this.bad1.applyDynamicNamed("sel") possible cause: maybe a wrong Dynamic method signature? bad1.sel(a = 1) - ^ + ^ applydynamic_sip.scala:21: error: type mismatch; found : String("sel") required: Int error after rewriting to Test.this.bad1.updateDynamic("sel") possible cause: maybe a wrong Dynamic method signature? bad1.sel = 1 - ^ + ^ applydynamic_sip.scala:29: error: Int does not take parameters error after rewriting to Test.this.bad2.selectDynamic("sel") possible cause: maybe a wrong Dynamic method signature? diff --git a/test/files/run/dynamic-applyDynamic.check b/test/files/run/dynamic-applyDynamic.check new file mode 100644 index 0000000000..89a0d55282 --- /dev/null +++ b/test/files/run/dynamic-applyDynamic.check @@ -0,0 +1,14 @@ +[[syntax trees at end of typer]] // newSource1.scala +[0:67]package [0:0] { + [0:67]object X extends [9:67][67]scala.AnyRef { + [9]def (): [9]X.type = [9]{ + [9][9][9]X.super.(); + [9]() + }; + [17:30]private[this] val d: [21]D = [25:30][25:30][25:30]new [29:30]D(); + [21] def d: [21]D = [21][21]X.this.d; + [37:49][37:38][37:38][37]X.this.d.applyDynamic(<39:45>"method")([46:48]10); + [56:61]<56:57><56:57>[56]X.this.d.applyDynamic(<56:57>"apply")([58:60]10) + } +} + diff --git a/test/files/run/dynamic-applyDynamic.scala b/test/files/run/dynamic-applyDynamic.scala new file mode 100644 index 0000000000..b06041194c --- /dev/null +++ b/test/files/run/dynamic-applyDynamic.scala @@ -0,0 +1,26 @@ +import scala.tools.partest.DirectTest + +object Test extends DirectTest { + + override def extraSettings: String = + s"-usejavacp -Xprint-pos -Xprint:typer -Yrangepos -Ystop-after:typer -d ${testOutput.path}" + + override def code = """ + object X { + val d = new D + d.method(10) + d(10) + } + """.trim + + override def show(): Unit = { + Console.withErr(System.out) { + compile() + } + } +} + +import language.dynamics +class D extends Dynamic { + def applyDynamic(name: String)(value: Any) = ??? +} \ No newline at end of file diff --git a/test/files/run/dynamic-selectDynamic.check b/test/files/run/dynamic-selectDynamic.check new file mode 100644 index 0000000000..7f95ed3d19 --- /dev/null +++ b/test/files/run/dynamic-selectDynamic.check @@ -0,0 +1,13 @@ +[[syntax trees at end of typer]] // newSource1.scala +[0:50]package [0:0] { + [0:50]object X extends [9:50][50]scala.AnyRef { + [9]def (): [9]X.type = [9]{ + [9][9][9]X.super.(); + [9]() + }; + [17:30]private[this] val d: [21]D = [25:30][25:30][25:30]new [29:30]D(); + [21] def d: [21]D = [21][21]X.this.d; + [37:38][37:38][37]X.this.d.selectDynamic(<39:44>"field") + } +} + diff --git a/test/files/run/dynamic-selectDynamic.scala b/test/files/run/dynamic-selectDynamic.scala new file mode 100644 index 0000000000..bd6c138c50 --- /dev/null +++ b/test/files/run/dynamic-selectDynamic.scala @@ -0,0 +1,25 @@ +import scala.tools.partest.DirectTest + +object Test extends DirectTest { + + override def extraSettings: String = + s"-usejavacp -Xprint-pos -Xprint:typer -Yrangepos -Ystop-after:typer -d ${testOutput.path}" + + override def code = """ + object X { + val d = new D + d.field + } + """.trim + + override def show(): Unit = { + Console.withErr(System.out) { + compile() + } + } +} + +import language.dynamics +class D extends Dynamic { + def selectDynamic(name: String) = ??? +} -- cgit v1.2.3 From 696545d53f89d8a764273dacca5c6d3043911722 Mon Sep 17 00:00:00 2001 From: Simon Schaefer Date: Sun, 24 Nov 2013 00:01:25 +0100 Subject: SI-8004 Resolve NoPosition error for applyDynamicNamed method call Previously, there were no positions created for the tuples that are generated while doing the transformation for an applyDynamicNamed call. This led to an NoPosition error in scalac when one tries to show position information in the AST. Furthermore, this simplifies semantic highlighting in the scala-ide because no position information for color ranges have to be created anymore. --- .../scala/tools/nsc/typechecker/Typers.scala | 9 ++++++-- test/files/run/dynamic-applyDynamicNamed.check | 14 ++++++++++++ test/files/run/dynamic-applyDynamicNamed.scala | 26 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test/files/run/dynamic-applyDynamicNamed.check create mode 100644 test/files/run/dynamic-applyDynamicNamed.scala (limited to 'test/files') diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 2bbb6d79dd..a18bc5ddcf 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -3999,9 +3999,14 @@ trait Typers extends Modes with Adaptations with Tags { def typedNamedApply(orig: Tree, fun: Tree, args: List[Tree], mode: Int, pt: Type): Tree = { def argToBinding(arg: Tree): Tree = arg match { - case AssignOrNamedArg(Ident(name), rhs) => gen.mkTuple(List(CODE.LIT(name.toString), rhs)) - case _ => gen.mkTuple(List(CODE.LIT(""), arg)) + case AssignOrNamedArg(i @ Ident(name), rhs) => + atPos(i.pos.withEnd(rhs.pos.endOrPoint)) { + gen.mkTuple(List(atPos(i.pos)(CODE.LIT(name.toString)), rhs)) + } + case _ => + gen.mkTuple(List(CODE.LIT(""), arg)) } + val t = treeCopy.Apply(orig, fun, args map argToBinding) wrapErrors(t, _.typed(t, mode, pt)) } diff --git a/test/files/run/dynamic-applyDynamicNamed.check b/test/files/run/dynamic-applyDynamicNamed.check new file mode 100644 index 0000000000..17fa496646 --- /dev/null +++ b/test/files/run/dynamic-applyDynamicNamed.check @@ -0,0 +1,14 @@ +[[syntax trees at end of typer]] // newSource1.scala +[0:97]package [0:0] { + [0:97]object X extends [9:97][97]scala.AnyRef { + [9]def (): [9]X.type = [9]{ + [9][9][9]X.super.(); + [9]() + }; + [17:30]private[this] val d: [21]D = [25:30][25:30][25:30]new [29:30]D(); + [21] def d: [21]D = [21][21]X.this.d; + [37:70][37:38][37:38][37]X.this.d.applyDynamicNamed(<39:43>"meth")([44:55][44][44][44]scala.this.Tuple2.apply[[44]String, [44]Int]([44:50]"value1", [53:55]10), [57:69][57][57][57]scala.this.Tuple2.apply[[57]String, [57]Int]([57:63]"value2", [66:69]100)); + [77:91]<77:78><77:78>[77]X.this.d.applyDynamicNamed(<77:78>"apply")([79:90][79][79][79]scala.this.Tuple2.apply[[79]String, [79]Int]([79:85]"value1", [88:90]10)) + } +} + diff --git a/test/files/run/dynamic-applyDynamicNamed.scala b/test/files/run/dynamic-applyDynamicNamed.scala new file mode 100644 index 0000000000..cc59f9058b --- /dev/null +++ b/test/files/run/dynamic-applyDynamicNamed.scala @@ -0,0 +1,26 @@ +import scala.tools.partest.DirectTest + +object Test extends DirectTest { + + override def extraSettings: String = + s"-usejavacp -Xprint-pos -Xprint:typer -Yrangepos -Ystop-after:typer -d ${testOutput.path}" + + override def code = """ + object X { + val d = new D + d.meth(value1 = 10, value2 = 100) + d(value1 = 10) + } + """.trim + + override def show(): Unit = { + Console.withErr(System.out) { + compile() + } + } +} + +import language.dynamics +class D extends Dynamic { + def applyDynamicNamed(name: String)(value: (String, Any)*) = ??? +} -- cgit v1.2.3 From 3629b645cc2b1403c51925dd9c696a57008c0ce2 Mon Sep 17 00:00:00 2001 From: Simon Schaefer Date: Sun, 24 Nov 2013 00:07:31 +0100 Subject: SI-8005 Fixes NoPositon error for updateDynamic calls Previously there occurred a NoPosition error when one asks for position information in the AST because no positions were set to the trees created during the transformation for updateDynamic calls. This commit applies range positions to the trees in order to being able to highlight them inside of the scala-ide. --- .../scala/tools/nsc/typechecker/Typers.scala | 4 +++- test/files/run/dynamic-updateDynamic.check | 14 +++++++++++ test/files/run/dynamic-updateDynamic.scala | 28 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/files/run/dynamic-updateDynamic.check create mode 100644 test/files/run/dynamic-updateDynamic.scala (limited to 'test/files') diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index a18bc5ddcf..b80dcefef8 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -4273,7 +4273,9 @@ trait Typers extends Modes with Adaptations with Tags { } else if(dyna.isDynamicallyUpdatable(lhs1)) { val rhs1 = typed(rhs, EXPRmode | BYVALmode, WildcardType) - val t = Apply(lhs1, List(rhs1)) + val t = atPos(lhs1.pos.withEnd(rhs1.pos.endOrPoint)) { + Apply(lhs1, List(rhs1)) + } dyna.wrapErrors(t, _.typed1(t, mode, pt)) } else fail() diff --git a/test/files/run/dynamic-updateDynamic.check b/test/files/run/dynamic-updateDynamic.check new file mode 100644 index 0000000000..3e21b7dfdc --- /dev/null +++ b/test/files/run/dynamic-updateDynamic.check @@ -0,0 +1,14 @@ +[[syntax trees at end of typer]] // newSource1.scala +[0:69]package [0:0] { + [0:69]object X extends [9:69][69]scala.AnyRef { + [9]def (): [9]X.type = [9]{ + [9][9][9]X.super.(); + [9]() + }; + [17:30]private[this] val d: [21]D = [25:30][25:30][25:30]new [29:30]D(); + [21] def d: [21]D = [21][21]X.this.d; + [37:49][37:38][37:38][37]X.this.d.updateDynamic(<39:44>"field")([47:49]10); + [56:57][56:57][56]X.this.d.selectDynamic(<58:63>"field") + } +} + diff --git a/test/files/run/dynamic-updateDynamic.scala b/test/files/run/dynamic-updateDynamic.scala new file mode 100644 index 0000000000..80fe0ea35f --- /dev/null +++ b/test/files/run/dynamic-updateDynamic.scala @@ -0,0 +1,28 @@ +import scala.tools.partest.DirectTest + +object Test extends DirectTest { + + override def extraSettings: String = + s"-usejavacp -Xprint-pos -Xprint:typer -Yrangepos -Ystop-after:typer -d ${testOutput.path}" + + override def code = """ + object X { + val d = new D + d.field = 10 + d.field + } + """.trim + + override def show(): Unit = { + Console.withErr(System.out) { + compile() + } + } +} + +import language.dynamics +class D extends Dynamic { + def selectDynamic(name: String): Any = ??? + def updateDynamic(name: String)(value: Any): Unit = ??? +} + -- cgit v1.2.3