Skip to content

Undefined += in inner quote #6772

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

Closed
nicolasstucki opened this issue Jul 1, 2019 · 3 comments · Fixed by #8796
Closed

Undefined += in inner quote #6772

nicolasstucki opened this issue Jul 1, 2019 · 3 comments · Fixed by #8796
Assignees

Comments

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Jul 1, 2019

Implementing scala.quoted.ExprOps.toExprOfList with

    /** Lifts this list of expressions into an expression of a list
     *
     *  Transforms a list of expression
     *    `e1 :: e2 :: ... :: Nil` where `ei: Expr[T]`
     *  to an expression equivalent to
     *    `'{ $e1 :: $e2 :: ... :: Nil }` typed as an `Expr[List[T]]`
     */
    def (list: List[Expr[T]]) toExprOfList[T] given Type[T], QuoteContext: Expr[List[T]] = '{
      val buff = List.newBuilder[T]
      ${ Expr.block(list.map(v => '{ buff +=  $v }), '{ buff.result() }) }
    }

fails with

35 |      ${ Expr.block(list.map(v => '{ buff `+=` $v }), '{ buff.result() }) }
   |                                     ^^^^^^^^^^^^
   |                                 undefined: buff.+= # 189467: TermRef(TermRef(NoPrefix,val buff),+=) at frontend
   | This location is in code that was inlined at quoted_2.scala:6
@nicolasstucki
Copy link
Contributor Author

This might be related to #7110

@nicolasstucki
Copy link
Contributor Author

nicolasstucki commented Sep 5, 2019

Self contained version:

import scala.quoted._

object Macros {

  inline def m() : Any = ${  mImpl() }

  def mImpl()(using QuoteContext): Expr[Any] =
    List(Expr(1), Expr(2), Expr(3)).toExprOfList

  def (list: List[Expr[T]]).toExprOfList[T](using Type[T], QuoteContext): Expr[List[T]] = '{
    val buff = List.newBuilder[T]
    ${ Expr.block(list.map(v => '{ buff +=  $v }), '{ buff.result() }) }
  }
}
import scala.quoted._
import Macros._

object Test {
  def main(args: Array[String]): Unit = {
    println(m())
  }
}

@liufengyun
Copy link
Contributor

The error now is different, it complains phase consistency for T. Maybe it is simpler to fix? /ping @nicolasstucki

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Apr 29, 2020
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Apr 29, 2020
Fix scala#6140, fix scala#6772, fix scala#7030, fix scala#7892, fix scala#7997, fix scala#8651 and improve scala#8100.

Differences with previous implementation
* Only track and check levels within quotes or splices
* Track levels of all symbols not at level 0
* Split level checking into specialized variants for types and terms (healType/healTermType)
* Detect inconsistent types rather than try to detect consistent ones
* Check/heal term inconsistencies only on leaf nodes (TypeTree, RefTree, Ident, This)
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 4, 2020
Fix scala#6140, fix scala#6772, fix scala#7030, fix scala#7892, fix scala#7997, fix scala#8651 and improve scala#8100.

Differences with previous implementation
* Only track and check levels within quotes or splices
* Track levels of all symbols not at level 0
* Split level checking into specialized variants for types and terms (healType/healTermType)
* Detect inconsistent types rather than try to detect consistent ones
* Check/heal term inconsistencies only on leaf nodes (TypeTree, RefTree, Ident, This)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants