Skip to content

Commit

Permalink
Fix #18407: Ignore Quote/Slice in init checker (#18848)
Browse files Browse the repository at this point in the history
Fix #18407: Ignore Quote/Slice in init checker

We should never be able to encounter them in usage of macros --- because
macros will expand to normal trees without quote and splices.

The particular test case is actually problematic: The macro did not
expand in `base_0.scala`.
  • Loading branch information
sjrd authored Nov 6, 2023
2 parents ef97ee2 + b09e900 commit 5623c1b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/init/Semantic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1383,11 +1383,11 @@ object Semantic:
case tpl: Template =>
init(tpl, thisV, klass)

case _: Import | _: Export =>
case _: Import | _: Export | _: Quote | _: Splice | _: QuotePattern | _: SplicePattern =>
Hot

case _ =>
report.warning("[Internal error] unexpected tree" + Trace.show, expr)
report.warning("[Internal error] unexpected tree: " + expr.getClass + ", trace:\n" + Trace.show, expr)
Hot

/** Handle semantics of leaf nodes
Expand Down
4 changes: 4 additions & 0 deletions tests/init/pos/i18407/base_0.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// base_0.scala
trait BaseTest extends AnyFreeSpecLike {
"empty-test" - {} // ok if we comment out this line
}
37 changes: 37 additions & 0 deletions tests/init/pos/i18407/macros_0.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// macros_0.scala
object source {
import scala.quoted._

class Position()

object Position {
def withPosition[T](
fun: Expr[Position => T]
)(using quotes: Quotes, typeOfT: Type[T]): Expr[T] = {
'{
${ fun }.apply(new source.Position())
}
}
}
}

trait AnyFreeSpecLike {
import scala.language.implicitConversions

protected final class FreeSpecStringWrapper(
string: String,
pos: source.Position
) {
def -(fun: => Unit): Unit = fun
}

inline implicit def convertToFreeSpecStringWrapper(
s: String
): FreeSpecStringWrapper = {
${
source.Position.withPosition[FreeSpecStringWrapper]('{
(pos: source.Position) => new FreeSpecStringWrapper(s, pos)
})
}
}
}
4 changes: 4 additions & 0 deletions tests/init/pos/i18407/test_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class MyTest extends BaseTest {
"empty-test" - {}
private val myObject = new {}
}

0 comments on commit 5623c1b

Please sign in to comment.