scala / bug

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

extra () in Tree causing pain #9320

Closed scabug closed 6 years ago

scabug commented 9 years ago

I'm running into some issues trying to combine Trees with quasiquoting:

This is no problem:

scala> q"package foo { sealed class Hello; object Bar extends Hello }"
res18: reflect.runtime.universe.PackageDef

But:

scala> val inner = q"sealed class Hello; object Bar extends Hello"
inner: reflect.runtime.universe.Tree

scala> showCode(inner)
res46: String =
{
  sealed class Hello;
  object Bar extends Hello;
  ()
}

scala> q"package foo { $inner }"
java.lang.IllegalArgumentException: not legal package stat: { ...

And:

scala> q"package foo { ..${inner.children} }"
java.lang.IllegalArgumentException: not legal package stat: ()

Sorry if not a bug!

scabug commented 9 years ago

Imported From: https://issues.scala-lang.org/browse/SI-9320?orig=1 Reporter: @refried

scabug commented 9 years ago

@som-snytt said: Blocks have result expressions, so the Unit is supplied. But you can use inner.children.init to exclude it.

scabug commented 9 years ago

@refried said (edited on May 20, 2015 12:59:50 PM UTC): How can I predict whether a tree contains an extraneous element to exclude though? I don’t want to chop off something important by accident.

scabug commented 9 years ago

@som-snytt said: The value at the end of a block can never be a top-level element in a package. Basically, filter on

scala> import PartialFunction.{ cond => when }
import PartialFunction.{cond=>when}

scala> def isTop(t: Tree) = when (t) { case _: ImplDef | _: Import => true }
isTop: (t: Tree)Boolean

Or for arbitrary trees, maybe you can get away with putting them in a package object.

SethTisue commented 6 years ago

closing all quasiquotes tickets; see #10755

som-snytt commented 6 years ago

@SethTisue I was lobbying to rename cond already years ago, see previous comment.

SethTisue commented 6 years ago

I was lobbying

drain the swamp!