Skip to content

Commit b915f44

Browse files
kiritsukuretronym
authored andcommitted
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.
1 parent 2ad9666 commit b915f44

File tree

6 files changed

+86
-5
lines changed

6 files changed

+86
-5
lines changed

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4061,7 +4061,10 @@ trait Typers extends Modes with Adaptations with Tags {
40614061
case Some((opName, treeInfo.Applied(_, targs, _))) =>
40624062
val fun = gen.mkTypeApply(Select(qual, opName), targs)
40634063
if (opName == nme.updateDynamic) suppressMacroExpansion(fun) // SI-7617
4064-
atPos(qual.pos)(Apply(fun, Literal(Constant(name.decode)) :: Nil))
4064+
val nameStringLit = atPos(treeSelection.pos.withStart(treeSelection.pos.point).makeTransparent) {
4065+
Literal(Constant(name.decode))
4066+
}
4067+
atPos(qual.pos)(Apply(fun, List(nameStringLit)))
40654068
case _ =>
40664069
setError(tree)
40674070
}

test/files/neg/applydynamic_sip.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ applydynamic_sip.scala:18: error: type mismatch;
1313
error after rewriting to Test.this.bad1.selectDynamic("sel")
1414
possible cause: maybe a wrong Dynamic method signature?
1515
bad1.sel
16-
^
16+
^
1717
applydynamic_sip.scala:19: error: type mismatch;
1818
found : String("sel")
1919
required: Int
2020
error after rewriting to Test.this.bad1.applyDynamic("sel")
2121
possible cause: maybe a wrong Dynamic method signature?
2222
bad1.sel(1)
23-
^
23+
^
2424
applydynamic_sip.scala:20: error: type mismatch;
2525
found : String("sel")
2626
required: Int
2727
error after rewriting to Test.this.bad1.applyDynamicNamed("sel")
2828
possible cause: maybe a wrong Dynamic method signature?
2929
bad1.sel(a = 1)
30-
^
30+
^
3131
applydynamic_sip.scala:21: error: type mismatch;
3232
found : String("sel")
3333
required: Int
3434
error after rewriting to Test.this.bad1.updateDynamic("sel")
3535
possible cause: maybe a wrong Dynamic method signature?
3636
bad1.sel = 1
37-
^
37+
^
3838
applydynamic_sip.scala:29: error: Int does not take parameters
3939
error after rewriting to Test.this.bad2.selectDynamic("sel")
4040
possible cause: maybe a wrong Dynamic method signature?
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[[syntax trees at end of typer]] // newSource1.scala
2+
[0:67]package [0:0]<empty> {
3+
[0:67]object X extends [9:67][67]scala.AnyRef {
4+
[9]def <init>(): [9]X.type = [9]{
5+
[9][9][9]X.super.<init>();
6+
[9]()
7+
};
8+
[17:30]private[this] val d: [21]D = [25:30][25:30][25:30]new [29:30]D();
9+
[21]<stable> <accessor> def d: [21]D = [21][21]X.this.d;
10+
[37:49][37:38][37:38][37]X.this.d.applyDynamic(<39:45>"method")([46:48]10);
11+
[56:61]<56:57><56:57>[56]X.this.d.applyDynamic(<56:57>"apply")([58:60]10)
12+
}
13+
}
14+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import scala.tools.partest.DirectTest
2+
3+
object Test extends DirectTest {
4+
5+
override def extraSettings: String =
6+
s"-usejavacp -Xprint-pos -Xprint:typer -Yrangepos -Ystop-after:typer -d ${testOutput.path}"
7+
8+
override def code = """
9+
object X {
10+
val d = new D
11+
d.method(10)
12+
d(10)
13+
}
14+
""".trim
15+
16+
override def show(): Unit = {
17+
Console.withErr(System.out) {
18+
compile()
19+
}
20+
}
21+
}
22+
23+
import language.dynamics
24+
class D extends Dynamic {
25+
def applyDynamic(name: String)(value: Any) = ???
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[syntax trees at end of typer]] // newSource1.scala
2+
[0:50]package [0:0]<empty> {
3+
[0:50]object X extends [9:50][50]scala.AnyRef {
4+
[9]def <init>(): [9]X.type = [9]{
5+
[9][9][9]X.super.<init>();
6+
[9]()
7+
};
8+
[17:30]private[this] val d: [21]D = [25:30][25:30][25:30]new [29:30]D();
9+
[21]<stable> <accessor> def d: [21]D = [21][21]X.this.d;
10+
[37:38][37:38][37]X.this.d.selectDynamic(<39:44>"field")
11+
}
12+
}
13+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import scala.tools.partest.DirectTest
2+
3+
object Test extends DirectTest {
4+
5+
override def extraSettings: String =
6+
s"-usejavacp -Xprint-pos -Xprint:typer -Yrangepos -Ystop-after:typer -d ${testOutput.path}"
7+
8+
override def code = """
9+
object X {
10+
val d = new D
11+
d.field
12+
}
13+
""".trim
14+
15+
override def show(): Unit = {
16+
Console.withErr(System.out) {
17+
compile()
18+
}
19+
}
20+
}
21+
22+
import language.dynamics
23+
class D extends Dynamic {
24+
def selectDynamic(name: String) = ???
25+
}

0 commit comments

Comments
 (0)