Skip to content

Commit 41e45c5

Browse files
Backport "Encode the name of the attribute in Selectable.selectDynamic" to LTS (#20750)
Backports #18928 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents d44cbd7 + e0c235f commit 41e45c5

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed

Diff for: compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import dotty.tools.dotc.transform.sjs.JSSymUtils._
3636

3737
import JSEncoding._
3838
import ScopedVar.withScopedVars
39+
import scala.reflect.NameTransformer
3940

4041
/** Main codegen for Scala.js IR.
4142
*
@@ -4218,7 +4219,7 @@ class JSCodeGen()(using genCtx: Context) {
42184219
}
42194220
}
42204221

4221-
val methodName = MethodName.reflectiveProxy(methodNameStr, formalParamTypeRefs)
4222+
val methodName = MethodName.reflectiveProxy(NameTransformer.encode(methodNameStr), formalParamTypeRefs)
42224223

42234224
js.Apply(js.ApplyFlags.empty, selectedValueTree, js.MethodIdent(methodName), actualArgs)(jstpe.AnyType)
42244225
}

Diff for: library/src/scala/reflect/Selectable.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait Selectable extends scala.Selectable:
2121
final def selectDynamic(name: String): Any =
2222
val rcls = selectedValue.getClass
2323
try
24-
val fld = rcls.getField(name).nn
24+
val fld = rcls.getField(NameTransformer.encode(name)).nn
2525
ensureAccessible(fld)
2626
fld.get(selectedValue)
2727
catch case ex: NoSuchFieldException =>
@@ -35,7 +35,7 @@ trait Selectable extends scala.Selectable:
3535
*/
3636
final def applyDynamic(name: String, paramTypes: Class[_]*)(args: Any*): Any =
3737
val rcls = selectedValue.getClass
38-
val mth = rcls.getMethod(name, paramTypes: _*).nn
38+
val mth = rcls.getMethod(NameTransformer.encode(name), paramTypes: _*).nn
3939
ensureAccessible(mth)
4040
mth.invoke(selectedValue, args.asInstanceOf[Seq[AnyRef]]: _*)
4141

Diff for: tests/run/i18612-a.scala

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

Diff for: tests/run/i18612-b.scala

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

Diff for: tests/run/i18612-c.scala

+7
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")

Diff for: tests/run/i18612-d.scala

+7
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)