Skip to content

Commit bcce959

Browse files
Merge pull request #8771 from dotty-staging/fix-#8749
Fix #8749: Error on $ at the start of a name in quoted patterns
2 parents 63447e8 + 4d6f907 commit bcce959

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@ object Trees {
353353
}
354354
}
355355
else span
356+
357+
/** The source position of the name defined by this definition.
358+
* This is a point position if the definition is synthetic, or a range position
359+
* if the definition comes from source.
360+
*/
361+
def namePos: SourcePosition = source.atSpan(nameSpan)
356362
}
357363

358364
/** Tree defines a new symbol and carries modifiers.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ trait QuotesAndSplices {
229229
}
230230
}
231231
cpy.AppliedTypeTree(tree)(transform(tpt), args1)
232+
case tree: NamedDefTree =>
233+
if tree.name.isTermName && !tree.nameSpan.isSynthetic && tree.name.startsWith("$") then
234+
ctx.error("Names cannot start with $ quote pattern ", tree.namePos)
235+
super.transform(tree)
232236
case _ =>
233237
super.transform(tree)
234238
}

tests/neg/i8749.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted._
2+
3+
object FunObject {
4+
def fun(t: String => String) = t
5+
}
6+
7+
def test(using QuoteContext)(x: Expr[String => String]) =
8+
x match
9+
case '{ FunObject.fun(($arg: String) => $out) } => // error

tests/neg/quotedPatterns-3.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
object Test {
22
def test(x: quoted.Expr[Int])(using scala.quoted.QuoteContext) = x match {
3-
case '{ val `$y`: Int = 2; 1 } =>
3+
case '{ val `$y`: Int = 2; 1 } => // error
44
y // error: Not found: y
5-
case '{ ((`$y`: Int) => 3); 2 } =>
5+
case '{ ((`$y`: Int) => 3); 2 } => // error
66
y // error: Not found: y
7-
case '{ def `$f`: Int = 8; 2 } =>
7+
case '{ def `$f`: Int = 8; 2 } => // error
88
f // error: Not found: f
99
case _ =>
1010
}

tests/run/i8746b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Macro {
55
inline def mac(inline tree: Any): String = ${ macImpl('tree) }
66
def macImpl(tree: Expr[Any])(using qctx: QuoteContext): Expr[String] = {
77
tree match {
8-
case '{ ($in: $tpe1) => ($out: $tpe2) } => Expr(out.toString)
8+
case '{ (in: $tpe1) => ($out: $tpe2) } => Expr(out.toString)
99
case _ => Expr("not matched")
1010
}
1111
}

0 commit comments

Comments
 (0)