Skip to content

Commit

Permalink
Add missing span in QuoteMatcher
Browse files Browse the repository at this point in the history
Fixes #18155
Fixes #17610
  • Loading branch information
nicolasstucki committed Jul 12, 2023
1 parent d0403b6 commit ddf41a0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ class QuoteMatcher(debug: Boolean) {
// After matching and doing all subtype checks, we have to approximate all the type bindings
// that we have found, seal them in a quoted.Type and add them to the result
val typeHoleApproximations = typeHoles.map(typeHoleApproximation)
val matchedTypes = typeHoleApproximations.map(tpe => new TypeImpl(TypeTree(tpe), spliceScope))
val matchedTypes = typeHoleApproximations.map { tpe =>
new TypeImpl(TypeTree(tpe).withSpan(scrutinee.span), spliceScope)
}
val matchedExprs =
val typeHoleMap: Type => Type =
if typeHoles.isEmpty then identity
Expand Down
34 changes: 34 additions & 0 deletions tests/pos-macros/i17610/Macros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Macros.scala
import Main._
import scala.quoted.*

object Macros {
inline def apply(): ProviderProcessor =
${ Macros.processorExpr }

def processorExpr[I: Type](using q: Quotes): Expr[ProviderProcessor] = '{
new ProviderProcessor {
override def apply(simple: Simple): MyF[Int] =
${ Macros.methodProcessorImpl('simple) }
}
}

def methodProcessorImpl[I: Type](using q: Quotes)(service: Expr[Simple]): Expr[MyF[Int]] = {
import q.reflect._

val returnType = TypeRepr.of[Int]
returnType.asType match {
case '[rt] =>
'{
${
import quotes.reflect._
TypeApply(
Select.unique('{ ???.asInstanceOf[Codec] }.asTerm, "apply"),
List(TypeTree.of[rt]) // generates the error, directly using Int instead of rt makes it disappear
).asExpr
}
???
}
}
}
}
20 changes: 20 additions & 0 deletions tests/pos-macros/i17610/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Main.scala
object Main {
type MyF[A]

trait ProviderProcessor {
def apply(simple: Simple): MyF[Int]
}

trait Codec {
def apply[A]: MyF[A]
}

trait Simple {
def a0: Int
}

def test(): Unit = {
val p= Macros()
}
}
15 changes: 15 additions & 0 deletions tests/pos-macros/i18155/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.quoted.*

object Macro:
transparent inline def foo: Any = ${ fooImpl }

def fooImpl(using Quotes): Expr[Any] =
import quotes.reflect.*
'{
val xxx = ${
Type.of[Int] match
case '[tpe] =>
Typed(Expr(1).asTerm, TypeTree.of[tpe]).asExpr
}
xxx
}
2 changes: 2 additions & 0 deletions tests/pos-macros/i18155/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def run() =
println(Macro.foo)

0 comments on commit ddf41a0

Please sign in to comment.