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

implicit params of macro defs don't play well with context bounds of macro impls #6281

Closed
scabug opened this issue Aug 25, 2012 · 3 comments
Closed
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Aug 25, 2012

import annotation.tailrec
import scala.math.{min, max}
import scala.{specialized => spec}

import language.experimental.macros

import scala.reflect.{ClassTag, TypeTag}
import scala.reflect.macros.Context

object Macros {
  def alloc[@spec A:ClassTag](src:Array[A], s1:Int, len:Int) = {
    val as = Array.ofDim[A](len)
    System.arraycopy(src, s1, as, 0, len)
    as
  }

  /**
   * Efficient alternative to Array.apply.
   *
   * "As seen on scala-internals!"
   */
  def array[A](as:A*)(implicit ct: ClassTag[A]) = macro arrayMacro[A]

  /**
   * Takes in something like:
   *   ArrayUtil.alloc[Int](11, 22, 33, 44)(ct)
   *
   * and builds a tree like:
   *   {
   *     val arr:Array[Int] = ct.newArray(4)
   *     arr.update(0, 11)
   *     arr.update(1, 22)
   *     arr.update(2, 33)
   *     arr.update(3, 44)
   *     arr
   *   }
   */
  def arrayMacro[A:c.AbsTypeTag](c:Context)(as:c.Expr[A]*)(ct: c.Expr[ClassTag[A]]): c.Expr[Array[A]] = {
    import c.mirror._
    import c.universe._
    def const(x:Int) = Literal(Constant(x))

    val n = as.length
    val arr = newTermName("arr")

    val create = Apply(Select(ct.tree, "newArray"), List(const(n)))
    val arrtpe = TypeTree(implicitly[c.AbsTypeTag[Array[A]]].tpe)
    val valdef = ValDef(Modifiers(), arr, arrtpe, create)

    val updates = (0 until n).map {
      i => Apply(Select(Ident(arr), "update"), List(const(i), as(i).tree))
    }

    val exprs = Seq(valdef) ++ updates ++ Seq(Ident(arr))
    val block = Block(exprs:_*)

    c.Expr[Array[A]](block)
  }
}
object Test extends App {
  import Macros._
  println(array(1, 2, 3))
}
C:\Projects\Kepler\sandbox @ 2.10.x>myke compile .
Test_2.scala:3: error: exception during macro expansion:
java.lang.IllegalArgumentException: argument type mismatch

  println(array(1, 2, 3))
               ^
one error found
@scabug
Copy link
Author

scabug commented Aug 25, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6281?orig=1
Reporter: @xeno-by

@scabug
Copy link
Author

scabug commented Aug 25, 2012

@xeno-by said:
scala/scala#1194

@scabug scabug closed this as completed Aug 25, 2012
@scabug
Copy link
Author

scabug commented Aug 25, 2012

@xeno-by said:
Workaround: use context bounds, remove implicit parameters from a macro def and summon them manually in a macro impl using c.inferImplicitValue.

@scabug scabug added this to the 2.10.0-M7 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants