Scala Programming Language
  1. Scala Programming Language
  2. SI-7777

applyDynamic macro fails for nested application

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.3-RC1, Scala 2.11.0-M4
    • Fix Version/s: Scala 2.11.0-M8
    • Component/s: Macros

      Description

      Consider this implementation of applyDynamic:

      object Dyn extends Dynamic {
        def applyDynamic(s: String)(xs: Any*): Dyn.type = {
          println(s + xs.map(_.toString).mkString("(", ", ", ")"))
          Dyn
        }
      }
      

      This works as expected:

        Dyn.foo(1, 2)              // prints "foo(1, 2)"
        Dyn.foo(3).bar(4, 5)       // prints "foo(3)", then "bar(4, 5)"
        Dyn(6).bar(7)              // prints "apply(6)", then "bar(7)"
        Dyn.foo(8)(9)              // prints "foo(8)", then "apply(9)"
      

      This somewhat analogous macro implementation works in all but the last case:

      class DynMacro extends Dynamic {
        def applyDynamic(s: String)(xs: Any*): DynMacro =
          macro DynMacro.applyDynamicMacro
      }
      
      object DynMacro extends DynMacro {
        def applyDynamicMacro(c: Context)(s: c.Expr[String])(xs: c.Expr[Any]*): c.Expr[DynMacro] = {
          import c.universe._
          val Literal(Constant(n: String)) = s.tree
          val args = xs.map(_.tree.toString).mkString("(", ", ", ")")
          c.Expr(q"println(${ n + args }); ${c.prefix.tree}")
        }
      }
      
        DynMacro.foo(1, 2)         // prints "foo(1, 2)"
        DynMacro.foo(3).bar(4, 5)  // prints "bar(4, 5)", then "foo(3)"
        DynMacro(6).bar(7)         // prints "bar(7)", then "apply(6)"
        DynMacro.foo(8)(9)         // Fails!
      

      The last case results in an unhandled AssertionError "assertion failed: DynMacro.foo(8)(9)" somewhere in SuperAccessors, but the actual error probably happens much earlier.

      This may or may not be related to SI-7059.

        Issue Links

          Activity

          Hide
          Eugene Burmako added a comment -

          Looks like an unfortunate mismatch of points in time when macros and dynamics are handled. How urgent is this bug for you to fix?

          Show
          Eugene Burmako added a comment - Looks like an unfortunate mismatch of points in time when macros and dynamics are handled. How urgent is this bug for you to fix?
          Hide
          Sandro Stucki added a comment -

          Experimenting with dynamic macros. Not a blocker for now.

          Show
          Sandro Stucki added a comment - Experimenting with dynamic macros. Not a blocker for now.
          Hide
          Eugene Burmako added a comment - - edited

          Seems related to https://issues.scala-lang.org/browse/SI-7914, because changing `DynMacro.foo(8)(9)` to `DynMacro.foo(8).apply(9)` works.

          Show
          Eugene Burmako added a comment - - edited Seems related to https://issues.scala-lang.org/browse/SI-7914 , because changing `DynMacro.foo(8)(9)` to `DynMacro.foo(8).apply(9)` works.
          Hide
          Eugene Burmako added a comment -

          Pull request: https://github.com/scala/scala/pull/3309. Thanks for the detailed report!

          Show
          Eugene Burmako added a comment - Pull request: https://github.com/scala/scala/pull/3309 . Thanks for the detailed report!

            People

            • Assignee:
              Eugene Burmako
              Reporter:
              Sandro Stucki
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development