diff --git a/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala b/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala index eee791852fde..67e828ea9894 100644 --- a/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala +++ b/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala @@ -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. * @@ -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) } diff --git a/library/src/scala/reflect/Selectable.scala b/library/src/scala/reflect/Selectable.scala index f0bc1f197f5d..483032e097da 100644 --- a/library/src/scala/reflect/Selectable.scala +++ b/library/src/scala/reflect/Selectable.scala @@ -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 => @@ -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]]: _*) diff --git a/tests/run/i18612-a.scala b/tests/run/i18612-a.scala new file mode 100644 index 000000000000..286c538ab354 --- /dev/null +++ b/tests/run/i18612-a.scala @@ -0,0 +1,6 @@ +class X extends scala.reflect.Selectable: + val `+` = "1" + +@main def Test = + val x = X() + assert(x.selectDynamic("+") == "1") \ No newline at end of file diff --git a/tests/run/i18612-b.scala b/tests/run/i18612-b.scala new file mode 100644 index 000000000000..ed02ea296e41 --- /dev/null +++ b/tests/run/i18612-b.scala @@ -0,0 +1,6 @@ +class X extends scala.reflect.Selectable: + val plus = "1" + +@main def Test = + val x = X() + assert(x.selectDynamic("plus") == "1") \ No newline at end of file diff --git a/tests/run/i18612-c.scala b/tests/run/i18612-c.scala new file mode 100644 index 000000000000..0f4ca99eac38 --- /dev/null +++ b/tests/run/i18612-c.scala @@ -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") \ No newline at end of file diff --git a/tests/run/i18612-d.scala b/tests/run/i18612-d.scala new file mode 100644 index 000000000000..82bc9f67f86a --- /dev/null +++ b/tests/run/i18612-d.scala @@ -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") \ No newline at end of file