scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Cannot use implicits with type projections in implicit macros #7959

Open scabug opened 11 years ago

scabug commented 11 years ago

Requesting an implicit parameter using a type projection does not work in an implicit macro. The following macro cannot be resolved implicitly at the call site:

  implicit def jdbcType[E <: AutoMappedJdbcTypeBase](implicit ct: ClassTag[E], jt: JdbcType[E#Underlying]): JdbcType[E] with BaseTypedType[E] =
    macro AutoMappedJdbcType.applyMacroImpl[E]

  def applyMacroImpl[E <: AutoMappedJdbcTypeBase](c: Context)(ct: c.Expr[ClassTag[E]], jt: c.Expr[JdbcType[E#Underlying]])(implicit e: c.WeakTypeTag[E], eu: c.WeakTypeTag[E#Underlying]): c.Expr[JdbcType[E] with BaseTypedType[E]] = ...

Replacing "macro AutoMappedJdbcType.applyMacroImpl[ E ]" with "???", thus turning it into a regular implicit method, makes the call site compile.

-Xshow-implicits gives the following details:

[info] /Users/szeiger/code/slick/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala:344: materializing requested scala.reflect.type.ClassTag[ID] using `package`.this.materializeClassTag[ID]()
[info]       def id = column[ID]("id", O.PrimaryKey)
[info]                          ^
[info] /Users/szeiger/code/slick/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala:344: jdbc.this.AutoMappedJdbcType.jdbcType is not a valid implicit value for scala.slick.ast.TypedType[ID] because:
[info] exception during macro expansion:
[info] java.lang.IllegalArgumentException: wrong number of arguments
[info]       def id = column[ID]("id", O.PrimaryKey)
[info]                          ^
[error] /Users/szeiger/code/slick/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala:344: could not find implicit value for parameter tm: scala.slick.ast.TypedType[ID]
[error]       def id = column[ID]("id", O.PrimaryKey)
[error]                          ^
[error] one error found

Trying to pass the type projection as a separate type argument to the macro, fails as well:

  implicit def jdbcType[E <: AutoMappedJdbcTypeBase](implicit ct: ClassTag[E], jt: JdbcType[E#Underlying]): JdbcType[E] with BaseTypedType[E] =
    macro AutoMappedJdbcType.applyMacroImpl[E, E#Underlying]

  def applyMacroImpl[E <: AutoMappedJdbcTypeBase, U](c: Context)(ct: c.Expr[ClassTag[E]], jt: c.Expr[JdbcType[U]])(implicit e: c.WeakTypeTag[E], eu: c.WeakTypeTag[U]): c.Expr[JdbcType[E] with BaseTypedType[E]] = ...

In this case, the error shown by -Xshow-implicits is:

[info] /Users/szeiger/code/slick/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala:319: materializing requested scala.reflect.type.ClassTag[ID] using `package`.this.materializeClassTag[ID]()
[info]       def id = column[ID]("id", O.PrimaryKey)
[info]                          ^
[info] /Users/szeiger/code/slick/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala:319: jdbc.this.AutoMappedJdbcType.jdbcType is not a valid implicit value for scala.slick.ast.TypedType[ID] because:
[info] hasMatchingSymbol reported error: type mismatch;
[info]  found   : AutoMappedJdbcTypeBase.this.Underlying
[info]  required: Int
[info]       def id = column[ID]("id", O.PrimaryKey)
[info]                          ^
[error] /Users/szeiger/code/slick/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala:319: could not find implicit value for parameter tm: scala.slick.ast.TypedType[ID]
[error]       def id = column[ID]("id", O.PrimaryKey)
[error]                          ^
[error] one error found
scabug commented 11 years ago

Imported From: https://issues.scala-lang.org/browse/SI-7959?orig=1 Reporter: @szeiger Affected Versions: 2.10.3, 2.11.0-M6

SethTisue commented 6 years ago

@szeiger should this stay open?