Skip to content

Commit 7dea0bc

Browse files
committed
Keep tree of type ascriptions of quote pattern splices
These trees where removed as part of an optimization. However, they should be kept around to allow for better error messages and IDE navigation. The type of the ascription in pickled in the TASTy file but is elided in the pickled quote pattern (same optimization already done later). Fixes scala#18409
1 parent ca6a80e commit 7dea0bc

7 files changed

+45
-13
lines changed

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,8 @@ trait QuotesAndSplices {
259259
pat.symbol.addAnnotation(Annotation(New(ref(defn.QuotedRuntimePatterns_fromAboveAnnot.typeRef)).withSpan(pat.span)))
260260
allTypeBindings += pat
261261
TypeTree(pat.symbol.typeRef).withSpan(pat.span)
262-
263-
case Typed(splice: SplicePattern, tpt) if !tpt.tpe.derivesFrom(defn.RepeatedParamClass) =>
264-
// We drop the type ascription because it is redundant, the SplicePattern contains the same type
265-
transform(tpt) // Collect type bindings
266-
splice
267262
case _: SplicePattern =>
268263
tree
269-
270264
case _ =>
271265
super.transform(tree)
272266
}

tests/neg-macros/i7264d.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import scala.quoted.*
22
class Foo {
33
def f[T2: Type](e: Expr[T2])(using Quotes) = e match {
4-
case '{ $x: *:[Int, Any] } => // error: Type argument Any does not conform to upper bound Tuple
4+
case '{ $x: *:[Int, Any] } => // error // error: Type argument Any does not conform to upper bound Tuple
55

66
}
77
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
-- Warning: tests/neg-macros/quote-type-variable-no-inference-2.scala:5:22 ---------------------------------------------
2-
5 | case '{ $_ : F[t, t]; () } => // warn // error
2+
5 | case '{ $_ : F[t, t]; () } => // warn // error // error
33
| ^
44
| Ignored bound <: Double
55
|
66
| Consider defining bounds explicitly:
77
| '{ type t <: Int & Double; ... }
88
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference-2.scala:5:12 --------------------------
9-
5 | case '{ $_ : F[t, t]; () } => // warn // error
9+
5 | case '{ $_ : F[t, t]; () } => // warn // error // error
1010
| ^
1111
| Type argument t does not conform to upper bound Double in inferred type F[t, t]
1212
|
1313
| longer explanation available when compiling with `-explain`
14+
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference-2.scala:5:20 --------------------------
15+
5 | case '{ $_ : F[t, t]; () } => // warn // error // error
16+
| ^
17+
| Type argument t does not conform to upper bound Double
18+
|
19+
| longer explanation available when compiling with `-explain`

tests/neg-macros/quote-type-variable-no-inference-2.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted.*
22

33
def test2(x: Expr[Any])(using Quotes) =
44
x match
5-
case '{ $_ : F[t, t]; () } => // warn // error
5+
case '{ $_ : F[t, t]; () } => // warn // error // error
66
case '{ type u <: Int & Double; $_ : F[u, u] } =>
77

88
type F[X <: Int, Y <: Double]

tests/neg-macros/quote-type-variable-no-inference-3.check

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Warning: tests/neg-macros/quote-type-variable-no-inference-3.scala:5:22 ---------------------------------------------
2-
5 | case '{ $_ : F[t, t]; () } => // warn // error
2+
5 | case '{ $_ : F[t, t]; () } => // warn // error // error
33
| ^
44
| Ignored bound <: Comparable[U]
55
|
@@ -13,8 +13,14 @@
1313
| Consider defining bounds explicitly:
1414
| '{ type u <: Comparable[u] & Comparable[Any]; ... }
1515
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference-3.scala:5:12 --------------------------
16-
5 | case '{ $_ : F[t, t]; () } => // warn // error
16+
5 | case '{ $_ : F[t, t]; () } => // warn // error // error
1717
| ^
1818
| Type argument t does not conform to upper bound Comparable[t] in inferred type F[t, t]
1919
|
2020
| longer explanation available when compiling with `-explain`
21+
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference-3.scala:5:20 --------------------------
22+
5 | case '{ $_ : F[t, t]; () } => // warn // error // error
23+
| ^
24+
| Type argument t does not conform to upper bound Comparable[t]
25+
|
26+
| longer explanation available when compiling with `-explain`

tests/neg-macros/quote-type-variable-no-inference-3.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted.*
22

33
def test2(x: Expr[Any])(using Quotes) =
44
x match
5-
case '{ $_ : F[t, t]; () } => // warn // error
5+
case '{ $_ : F[t, t]; () } => // warn // error // error
66
case '{ type u <: Comparable[`u`]; $_ : F[u, u] } =>
77

88
type F[T, U <: Comparable[U]]

tests/pos-macros/i18409.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// scalac: -Werror -Wunused:all
2+
3+
import scala.quoted.*
4+
5+
object model {
6+
trait Transformer[Source, Dest] {
7+
def transform(from: Source): Dest
8+
}
9+
object Transformer {
10+
trait ForProduct[A, B] extends Transformer[A, B]
11+
}
12+
}
13+
14+
object Ops {
15+
import model.Transformer // unused import false-positive
16+
17+
def unapply(using Quotes)(term: quotes.reflect.Term): Option[String] = {
18+
term.asExpr match {
19+
case '{
20+
($transformer: Transformer.ForProduct[a, b]).transform($appliedTo)
21+
} =>
22+
Some("")
23+
case other => None
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)