Skip to content

Commit

Permalink
Backport "Encode the name of the attribute in Selectable.selectDynami…
Browse files Browse the repository at this point in the history
…c" to LTS (#20750)

Backports #18928 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jun 23, 2024
2 parents d44cbd7 + e0c235f commit 41e45c5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import dotty.tools.dotc.transform.sjs.JSSymUtils._

import JSEncoding._
import ScopedVar.withScopedVars
import scala.reflect.NameTransformer

/** Main codegen for Scala.js IR.
*
Expand Down Expand Up @@ -4218,7 +4219,7 @@ class JSCodeGen()(using genCtx: Context) {
}
}

val methodName = MethodName.reflectiveProxy(methodNameStr, formalParamTypeRefs)
val methodName = MethodName.reflectiveProxy(NameTransformer.encode(methodNameStr), formalParamTypeRefs)

js.Apply(js.ApplyFlags.empty, selectedValueTree, js.MethodIdent(methodName), actualArgs)(jstpe.AnyType)
}
Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/reflect/Selectable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait Selectable extends scala.Selectable:
final def selectDynamic(name: String): Any =
val rcls = selectedValue.getClass
try
val fld = rcls.getField(name).nn
val fld = rcls.getField(NameTransformer.encode(name)).nn
ensureAccessible(fld)
fld.get(selectedValue)
catch case ex: NoSuchFieldException =>
Expand All @@ -35,7 +35,7 @@ trait Selectable extends scala.Selectable:
*/
final def applyDynamic(name: String, paramTypes: Class[_]*)(args: Any*): Any =
val rcls = selectedValue.getClass
val mth = rcls.getMethod(name, paramTypes: _*).nn
val mth = rcls.getMethod(NameTransformer.encode(name), paramTypes: _*).nn
ensureAccessible(mth)
mth.invoke(selectedValue, args.asInstanceOf[Seq[AnyRef]]: _*)

Expand Down
6 changes: 6 additions & 0 deletions tests/run/i18612-a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class X extends scala.reflect.Selectable:
val `+` = "1"

@main def Test =
val x = X()
assert(x.selectDynamic("+") == "1")
6 changes: 6 additions & 0 deletions tests/run/i18612-b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class X extends scala.reflect.Selectable:
val plus = "1"

@main def Test =
val x = X()
assert(x.selectDynamic("plus") == "1")
7 changes: 7 additions & 0 deletions tests/run/i18612-c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class X extends scala.reflect.Selectable:
def + = "1"

@main def Test =
val x = X()
assert(x.selectDynamic("+") == "1")
assert(x.applyDynamic("+")() == "1")
7 changes: 7 additions & 0 deletions tests/run/i18612-d.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class X extends scala.reflect.Selectable:
def plus = "1"

@main def Test =
val x = X()
assert(x.selectDynamic("plus") == "1")
assert(x.applyDynamic("plus")() == "1")

0 comments on commit 41e45c5

Please sign in to comment.