Skip to content

Fix #8749: Error on $ at the start of a name in quoted patterns #8771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ object Trees {
}
}
else span

/** The source position of the name defined by this definition.
* This is a point position if the definition is synthetic, or a range position
* if the definition comes from source.
*/
def namePos: SourcePosition = source.atSpan(nameSpan)
}

/** Tree defines a new symbol and carries modifiers.
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ trait QuotesAndSplices {
}
}
cpy.AppliedTypeTree(tree)(transform(tpt), args1)
case tree: NamedDefTree =>
if tree.name.isTermName && !tree.nameSpan.isSynthetic && tree.name.startsWith("$") then
ctx.error("Names cannot start with $ quote pattern ", tree.namePos)
super.transform(tree)
case _ =>
super.transform(tree)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/neg/i8749.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted._

object FunObject {
def fun(t: String => String) = t
}

def test(using QuoteContext)(x: Expr[String => String]) =
x match
case '{ FunObject.fun(($arg: String) => $out) } => // error
6 changes: 3 additions & 3 deletions tests/neg/quotedPatterns-3.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
object Test {
def test(x: quoted.Expr[Int])(using scala.quoted.QuoteContext) = x match {
case '{ val `$y`: Int = 2; 1 } =>
case '{ val `$y`: Int = 2; 1 } => // error
y // error: Not found: y
case '{ ((`$y`: Int) => 3); 2 } =>
case '{ ((`$y`: Int) => 3); 2 } => // error
y // error: Not found: y
case '{ def `$f`: Int = 8; 2 } =>
case '{ def `$f`: Int = 8; 2 } => // error
f // error: Not found: f
case _ =>
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run/i8746b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Macro {
inline def mac(inline tree: Any): String = ${ macImpl('tree) }
def macImpl(tree: Expr[Any])(using qctx: QuoteContext): Expr[String] = {
tree match {
case '{ ($in: $tpe1) => ($out: $tpe2) } => Expr(out.toString)
case '{ (in: $tpe1) => ($out: $tpe2) } => Expr(out.toString)
case _ => Expr("not matched")
}
}
Expand Down