Skip to content

Commit

Permalink
Encode the name of the attribute in Selectable.selectDynamic (#18928)
Browse files Browse the repository at this point in the history
Fixes #18612
  • Loading branch information
sjrd authored Nov 15, 2023
2 parents b573b5f + dee9ed7 commit 41740f4
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 41740f4

Please sign in to comment.