Skip to content

Commit 3b5106a

Browse files
committed
Encode the name of the attribute in Selectable.applyDynamic
1 parent 6d6e154 commit 3b5106a

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

library/src/scala/reflect/Selectable.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ trait Selectable extends scala.Selectable:
2020
/** Select member with given name */
2121
final def selectDynamic(name: String): Any =
2222
val rcls = selectedValue.getClass
23-
val encodedName = NameTransformer.encode(name)
2423
try
25-
val fld = rcls.getField(encodedName).nn
24+
val fld = rcls.getField(NameTransformer.encode(name)).nn
2625
ensureAccessible(fld)
2726
fld.get(selectedValue)
2827
catch case ex: NoSuchFieldException =>
29-
applyDynamic(encodedName)()
28+
applyDynamic(name)()
3029

3130
// The Scala.js codegen relies on this method being final for correctness
3231
/** Select method and apply to arguments.
@@ -36,7 +35,7 @@ trait Selectable extends scala.Selectable:
3635
*/
3736
final def applyDynamic(name: String, paramTypes: Class[?]*)(args: Any*): Any =
3837
val rcls = selectedValue.getClass
39-
val mth = rcls.getMethod(name, paramTypes*).nn
38+
val mth = rcls.getMethod(NameTransformer.encode(name), paramTypes*).nn
4039
ensureAccessible(mth)
4140
mth.invoke(selectedValue, args.asInstanceOf[Seq[AnyRef]]*)
4241

tests/run/i18612-c.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class X extends scala.reflect.Selectable:
2+
def + = "1"
3+
4+
@main def Test =
5+
val x = X()
6+
assert(x.selectDynamic("+") == "1")
7+
assert(x.applyDynamic("+")() == "1")

tests/run/i18612-d.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class X extends scala.reflect.Selectable:
2+
def plus = "1"
3+
4+
@main def Test =
5+
val x = X()
6+
assert(x.selectDynamic("plus") == "1")
7+
assert(x.applyDynamic("plus")() == "1")

0 commit comments

Comments
 (0)