Skip to content

Commit

Permalink
compiler-js: some cleanups to avoid warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Jun 14, 2023
1 parent 2e2ba72 commit c64af30
Show file tree
Hide file tree
Showing 32 changed files with 160 additions and 466 deletions.
8 changes: 4 additions & 4 deletions common/jvm/src/main/scala/scalan/reflection/JavaImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class JRClass[T](val value: Class[T]) extends RClass[T] {
val methods = TrieMap.empty[(String, Seq[Class[_]]), RMethod]

override def getMethod(name: String, parameterTypes: Class[_]*): RMethod = {
memoize(methods)((name, parameterTypes), JRMethod(this, value.getMethod(name, parameterTypes:_*)))
memoize(methods)((name, parameterTypes), JRMethod(value.getMethod(name, parameterTypes:_*)))
}

override def getSimpleName: String = value.getSimpleName
Expand Down Expand Up @@ -62,7 +62,7 @@ class JRClass[T](val value: Class[T]) extends RClass[T] {

override def isAssignableFrom(cls: Class[_]): Boolean = value.isAssignableFrom(cls)

override def getDeclaredMethods(): Array[RMethod] = value.getDeclaredMethods.map(JRMethod(this, _))
override def getDeclaredMethods(): Array[RMethod] = value.getDeclaredMethods.map(JRMethod(_))

override def equals(other: Any): Boolean = (this eq other.asInstanceOf[AnyRef]) || (other match {
case that: JRClass[_] =>
Expand Down Expand Up @@ -133,7 +133,7 @@ object JRConstructor {
* @param declaringClass The JRClass that declares this method.
* @param value The [[java.lang.reflect.Method]] instance that this JRMethod represents.
*/
class JRMethod private (declaringClass: JRClass[_], val value: Method) extends RMethod {
class JRMethod private (val value: Method) extends RMethod {
override def invoke(obj: Any, args: AnyRef*): AnyRef = {
// val name = value.getName
// val parameterTypes: Seq[Class[_]] = value.getParameterTypes
Expand All @@ -156,5 +156,5 @@ class JRMethod private (declaringClass: JRClass[_], val value: Method) extends R
override def toString: String = s"JRMethod($value)"
}
object JRMethod {
private[reflection] def apply(clazz: JRClass[_], value: Method): RMethod = new JRMethod(clazz, value)
private[reflection] def apply(value: Method): RMethod = new JRMethod(value)
}
26 changes: 14 additions & 12 deletions common/jvm/src/main/scala/scalan/reflection/Platform.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package scalan.reflection


import scalan.util.ReflectionUtil.isSpecialChar

import scala.collection.concurrent.TrieMap

/** Platform dependent implementation of reflection methods. */
Expand All @@ -16,16 +14,6 @@ object Platform {
/** Thread-safe storage of class information. */
private val classes = TrieMap.empty[Class[_], JRClass[_]]

/** Check class registration. Should be used only for debugging. */
private def checkRegisteredClass[T](clazz: Class[T]): Unit = {
CommonReflection.classes.get(clazz) match {
case Some(c) =>
assert(c.clazz == clazz)
case _ =>
sys.error(s"Cannot find RClass data for $clazz")
}
}

/** Returns an RClass instance for the given class.
*
* @param clazz The class for which to retrieve an RClass instance.
Expand All @@ -38,6 +26,15 @@ object Platform {
val cls = memoize(classes)(clazz, new JRClass[T](clazz)).asInstanceOf[JRClass[T]]
// Uncomment the following lines to collect missing reflection data and generate Scala code for it
// Should be used only for debugging and never in production.
// /** Check class registration. Should be used only for debugging. */
// def checkRegisteredClass[T](clazz: Class[T]): Unit = {
// CommonReflection.classes.get(clazz) match {
// case Some(c) =>
// assert(c.clazz == clazz)
// case _ =>
// sys.error(s"Cannot find RClass data for $clazz")
// }
// }
// try {
// checkRegisteredClass(clazz)
// } catch {
Expand Down Expand Up @@ -66,6 +63,11 @@ object Platform {
def getOrElseUpdate(key: K, value: => V): V = map.getOrElseUpdate(key, value)
}

/** Special character in the name. */
private def isSpecialChar(c: Char): Boolean = {
('0' <= c && c <= '9') || c == '$'
}

/** Safe version of `getSimpleName` that works around a bug in Scala compilers 2.11, 2.12.
* This method is only used for debugging and testing purposes.
*
Expand Down
5 changes: 4 additions & 1 deletion common/shared/src/main/scala/scalan/TypeDesc.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scalan

import scala.reflect.ClassTag
import scala.annotation.implicitNotFound
import scala.annotation.{implicitNotFound, unused}
import scala.language.implicitConversions

/** Base type for all runtime type descriptors. Sigma uses type descriptors to
Expand Down Expand Up @@ -112,6 +112,7 @@ object RType {

case class ArrayType[A](tA: RType[A]) extends RType[Array[A]] {
val classTag: ClassTag[Array[A]] = {
@unused // avoid warning about unused ctA
implicit val ctA: ClassTag[A] = tA.classTag
scala.reflect.classTag[Array[A]]
}
Expand All @@ -122,6 +123,7 @@ object RType {

case class OptionType[A](tA: RType[A]) extends RType[Option[A]] {
val classTag: ClassTag[Option[A]] = {
@unused // avoid warning about unused ctA
implicit val ctA: ClassTag[A] = tA.classTag
scala.reflect.classTag[Option[A]]
}
Expand All @@ -135,6 +137,7 @@ object RType {

case class ThunkType[A](tA: RType[A]) extends RType[ThunkData[A]] {
val classTag: ClassTag[ThunkData[A]] = {
@unused // avoid warning about unused ctA
implicit val ctA: ClassTag[A] = tA.classTag
scala.reflect.classTag[ThunkData[A]]
}
Expand Down
20 changes: 10 additions & 10 deletions common/shared/src/main/scala/scalan/util/CollectionUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ object CollectionUtil {
def concatArrays[T](xs: Array[T], ys: Array[T]): Array[T] = {
val len = xs.length + ys.length
val result = (xs match {
case arr: Array[AnyRef] => new Array[AnyRef](len) // creates an array with invalid type descriptor (i.e. when T == Tuple2)
case arr: Array[Byte] => new Array[Byte](len)
case arr: Array[Short] => new Array[Short](len)
case arr: Array[Int] => new Array[Int](len)
case arr: Array[Long] => new Array[Long](len)
case arr: Array[Char] => new Array[Char](len)
case arr: Array[Float] => new Array[Float](len)
case arr: Array[Double] => new Array[Double](len)
case arr: Array[Boolean] => new Array[Boolean](len)
case _: Array[AnyRef] => new Array[AnyRef](len) // creates an array with invalid type descriptor (i.e. when T == Tuple2)
case _: Array[Byte] => new Array[Byte](len)
case _: Array[Short] => new Array[Short](len)
case _: Array[Int] => new Array[Int](len)
case _: Array[Long] => new Array[Long](len)
case _: Array[Char] => new Array[Char](len)
case _: Array[Float] => new Array[Float](len)
case _: Array[Double] => new Array[Double](len)
case _: Array[Boolean] => new Array[Boolean](len)
}).asInstanceOf[Array[T]]
Array.copy(xs, 0, result, 0, xs.length)
Array.copy(ys, 0, result, xs.length, ys.length)
Expand Down Expand Up @@ -177,7 +177,7 @@ object CollectionUtil {
* @return original collection `xs` casted to Source[B]
* @throws java.lang.AssertionError if at least one item cannot be cast to `B`
*/
def cast[B:ClassTag](implicit cbf: BuildFrom[Source[A], B, Source[B]]): Source[B] = {
def cast[B:ClassTag]: Source[B] = {
for (x <- xs) {
assert(x match { case _: B => true case _ => false}, s"Value $x doesn't conform to type ${reflect.classTag[B]}")
}
Expand Down
1 change: 0 additions & 1 deletion common/shared/src/main/scala/scalan/util/Extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package scalan.util

import java.math.BigInteger
import java.nio.ByteBuffer
import scala.language.higherKinds

object Extensions {
implicit class BooleanOps(val b: Boolean) extends AnyVal {
Expand Down
11 changes: 0 additions & 11 deletions common/shared/src/main/scala/scalan/util/ReflectionUtil.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ package rewriting

import scalan.reflection.{Platform, RClass, RConstructor}

import scala.collection.concurrent.TrieMap

/**
* Strategy-based term rewriting in the style of Stratego (http://strategoxt.org/).
* The implementation here is partially based on the semantics given in "Program
Expand Down Expand Up @@ -104,7 +102,7 @@ trait Rewriter {
(t : Any) => {
val of = anyf andThen (_ => Some(t))
try {
of.applyOrElse(t, (a : Any) => None)
of.applyOrElse(t, (_ : Any) => None)
} catch {
case _ : ClassCastException =>
None
Expand All @@ -129,7 +127,7 @@ trait Rewriter {
mkStrategy(
(t : Any) =>
try {
of.applyOrElse(t, (a : Any) => None)
of.applyOrElse(t, (_ : Any) => None)
} catch {
case _ : ClassCastException =>
None
Expand Down Expand Up @@ -158,7 +156,7 @@ trait Rewriter {
(t : Any) => {
val of = anyf andThen (_.apply(t))
try {
of.applyOrElse(t, (a : Any) => None)
of.applyOrElse(t, (_ : Any) => None)
} catch {
case _ : ClassCastException =>
None
Expand All @@ -184,7 +182,7 @@ trait Rewriter {
mkStrategy(
(t : Any) =>
try {
of.applyOrElse(t, (a : Any) => None)
of.applyOrElse(t, (_ : Any) => None)
} catch {
case _ : ClassCastException =>
None
Expand Down Expand Up @@ -243,7 +241,7 @@ trait Rewriter {
// are trying to duplicate one of these then we want to return the same
// singleton so we use an identity duper.
clazz.getField("MODULE$")
(t : Any, children : Array[AnyRef]) => t
(t : Any, _ : Array[AnyRef]) => t
} catch {
// Otherwise, this is a normal class, so we try to make a
// duper that uses the first constructor.
Expand All @@ -252,7 +250,7 @@ trait Rewriter {
if (ctors.length == 0)
sys.error(s"dup no constructors for ${clazz.getName}")
else
(t : Any, children : Array[AnyRef]) =>
(_ : Any, children : Array[AnyRef]) =>
makeInstance(ctors(0), children)
}

Expand All @@ -261,7 +259,7 @@ trait Rewriter {
try {
ctor.newInstance(unboxPrimitives(ctor, children) : _*)
} catch {
case e : IllegalArgumentException =>
case _ : IllegalArgumentException =>
sys.error(s"""dup illegal arguments: $ctor got (${children.mkString(",")})
|Common cause: term classes are nested in another class, move them to the top level""".stripMargin)
}
Expand Down Expand Up @@ -508,7 +506,7 @@ trait Rewriter {
t.foldLeft((false, 0)) {
case ((changed, i), ct) =>
s(ct) match {
case Some(ti @ (tix, tiy)) =>
case Some(ti @ (_, _)) =>
b += ti
(changed || !same(ct, ti), i + 1)
case _ =>
Expand Down Expand Up @@ -616,9 +614,9 @@ trait Rewriter {
case (add, ct) =>
if (add)
s(ct) match {
case Some(ti @ (tix, tiy)) if same(ct, ti) =>
case Some(ti @ (_, _)) if same(ct, ti) =>
return Some(t)
case Some(ti @ (tix, tiy)) =>
case Some(ti @ (_, _)) =>
b += ti
false
case Some(ti) =>
Expand Down Expand Up @@ -745,7 +743,7 @@ trait Rewriter {
t.foldLeft((false, false)) {
case ((success, changed), ct) =>
s(ct) match {
case Some(ti @ (tix, tiy)) =>
case Some(ti @ (_, _)) =>
b += ti
(true, changed || !same(ct, ti))
case _ =>
Expand Down
6 changes: 3 additions & 3 deletions common/shared/src/test/scala/scalan/util/BenchmarkUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scalan.util

import debox.cfor

import scala.concurrent.{Future, Await}
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global

Expand All @@ -15,7 +15,7 @@ object BenchmarkUtil {
var sum = 0L
cfor(0)(_ < nIters, _ + 1) { i =>
val start = System.currentTimeMillis()
val res = action(i)
action(i)
val end = System.currentTimeMillis()
val iterTime = end - start
if (okShowIterTime)
Expand Down Expand Up @@ -44,7 +44,7 @@ object BenchmarkUtil {
def runTasks(nTasks: Int)(block: Int => Unit) = {
val (_, total) = measureTime {
val tasks = (1 to nTasks).map(iTask => Future(block(iTask)))
val res = Await.result(Future.sequence(tasks), Duration.Inf)
Await.result(Future.sequence(tasks), Duration.Inf)
}
println(s"Completed $nTasks tasks in $total msec")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import scala.reflect.ClassTag

class CollectionUtilTests extends BaseTests {
import scalan.util.CollectionUtil._
import java.lang.{Byte => JByte, Integer}

test("updateMany") {
val xs: Seq[Byte] = Array[Byte](1,2,3)
Expand Down Expand Up @@ -37,9 +36,9 @@ class CollectionUtilTests extends BaseTests {
}

def joinSeqs(l: Seq[Int], r: Seq[Int]) =
outerJoinSeqs(l, r)(l => l, r => r)((_,l) => l, (_,r) => r, (k,l,r) => l + r).map(_._2)
outerJoinSeqs(l, r)(l => l, r => r)((_,l) => l, (_,r) => r, (_,l,r) => l + r).map(_._2)
def joinPairs(l: Seq[(String,Int)], r: Seq[(String,Int)]) =
outerJoinSeqs(l, r)(l => l._1, r => r._1)((_,l) => l._2, (_,r) => r._2, (k,l,r) => l._2 + r._2)
outerJoinSeqs(l, r)(l => l._1, r => r._1)((_,l) => l._2, (_,r) => r._2, (_,l,r) => l._2 + r._2)

test("joinSeqs") {
def key(p : (Int, String)): Int = p._1
Expand Down
Loading

0 comments on commit c64af30

Please sign in to comment.