Skip to content

Commit 8744018

Browse files
authored
Backport "Add missing span in QuoteMatcher" to LTS (#19111)
Backports #18178 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents e00b934 + bfe8fab commit 8744018

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ class QuoteMatcher(debug: Boolean) {
135135
// After matching and doing all subtype checks, we have to approximate all the type bindings
136136
// that we have found, seal them in a quoted.Type and add them to the result
137137
val typeHoleApproximations = typeHoles.map(typeHoleApproximation)
138-
val matchedTypes = typeHoleApproximations.map(tpe => new TypeImpl(TypeTree(tpe), spliceScope))
138+
val matchedTypes = typeHoleApproximations.map { tpe =>
139+
new TypeImpl(TypeTree(tpe).withSpan(scrutinee.span), spliceScope)
140+
}
139141
val matchedExprs =
140142
val typeHoleMap: Type => Type =
141143
if typeHoles.isEmpty then identity

tests/pos-macros/i17610/Macros.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Macros.scala
2+
import Main._
3+
import scala.quoted.*
4+
5+
object Macros {
6+
inline def apply(): ProviderProcessor =
7+
${ Macros.processorExpr }
8+
9+
def processorExpr[I: Type](using q: Quotes): Expr[ProviderProcessor] = '{
10+
new ProviderProcessor {
11+
override def apply(simple: Simple): MyF[Int] =
12+
${ Macros.methodProcessorImpl('simple) }
13+
}
14+
}
15+
16+
def methodProcessorImpl[I: Type](using q: Quotes)(service: Expr[Simple]): Expr[MyF[Int]] = {
17+
import q.reflect._
18+
19+
val returnType = TypeRepr.of[Int]
20+
returnType.asType match {
21+
case '[rt] =>
22+
'{
23+
${
24+
import quotes.reflect._
25+
TypeApply(
26+
Select.unique('{ ???.asInstanceOf[Codec] }.asTerm, "apply"),
27+
List(TypeTree.of[rt]) // generates the error, directly using Int instead of rt makes it disappear
28+
).asExpr
29+
}
30+
???
31+
}
32+
}
33+
}
34+
}

tests/pos-macros/i17610/Test.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Main.scala
2+
object Main {
3+
type MyF[A]
4+
5+
trait ProviderProcessor {
6+
def apply(simple: Simple): MyF[Int]
7+
}
8+
9+
trait Codec {
10+
def apply[A]: MyF[A]
11+
}
12+
13+
trait Simple {
14+
def a0: Int
15+
}
16+
17+
def test(): Unit = {
18+
val p= Macros()
19+
}
20+
}

tests/pos-macros/i18155/Macro_1.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted.*
2+
3+
object Macro:
4+
transparent inline def foo: Any = ${ fooImpl }
5+
6+
def fooImpl(using Quotes): Expr[Any] =
7+
import quotes.reflect.*
8+
'{
9+
val xxx = ${
10+
Type.of[Int] match
11+
case '[tpe] =>
12+
Typed(Expr(1).asTerm, TypeTree.of[tpe]).asExpr
13+
}
14+
xxx
15+
}

tests/pos-macros/i18155/Test_2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@main def run() =
2+
println(Macro.foo)

0 commit comments

Comments
 (0)