scala / bug

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

Macro expansion fails if names of variables defined by macro end in $ #8533

Closed scabug closed 6 years ago

scabug commented 10 years ago

Such macro will compile, but fails to expand.

def aggregate[T: c.WeakTypeTag, S: c.WeakTypeTag](c: Context)(z: c.Expr[S])(combop: c.Expr[(S, S) => S])(seqop: c.Expr[(S, T) => S]): c.Expr[S] = {

      import c.universe._
      val t = q"""$
      var zero$$ = $z
      val seqop$$ = $seqop
      val comboop$$ = $combop
      for(el <- 1 to 10) zero$$ = seqop$$(zero$$, el)
      zero$$
      """
      c.Expr[S](c.untypecheck(t))
      }

Compiler stack trace https://gist.github.com/DarkDimius/cd3e49d4740ce52a7db8

Adding '0' suffix to names of variables is a workaround.

scabug commented 10 years ago

Imported From: https://issues.scala-lang.org/browse/SI-8533?orig=1 Reporter: @DarkDimius Affected Versions: 2.11.0 See #8425

scabug commented 10 years ago

@retronym said: Sounds like a second helping of #8425.

scabug commented 10 years ago

@retronym said: Using consecutive '$' in an identifier is pretty much suicide.

scabug commented 10 years ago

@DarkDimius said (edited on Apr 24, 2014 11:37:44 AM UTC): AFAIK those wont end as consecutive '$$' as it's just a way to escape '$' inside string interpolators.

scabug commented 10 years ago

@retronym said: Okay, it must be added elsewhere.

scabug commented 10 years ago

@retronym said: PS: Because it is my hobby to point out fun issues with hygiene and quasiquotes, putting 1 to 10 in a quasiquote is unsafe as you can't be sure that the extension method to will always be the same one, it depends on the context of the call site. new *root*.....Range(1, 10) would be safer.

SethTisue commented 6 years ago

closing all quasiquotes tickets; see #10755

(comment and/or reopen if I'm casting my net too wide here)