Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport "Encode the name of the attribute in Selectable.selectDynamic" to LTS #20750

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Loading