Skip to content

Commit 79df0a3

Browse files
Merge pull request #7667 from dotty-staging/fix-#7603
Fix #7603: Emit error when a splice type is not an identifier
2 parents ccb7b13 + 58c7023 commit 79df0a3

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ trait QuotesAndSplices {
107107
if (ctx.mode.is(Mode.QuotedPattern)) spliceOwner(ctx.outer) else ctx.owner
108108
val name = tree.expr match {
109109
case Ident(name) => ("$" + name).toTypeName
110-
case Typed(Ident(name), _) => ("$" + name).toTypeName
111-
case Bind(name, _) => ("$" + name).toTypeName
112-
case _ => NameKinds.UniqueName.fresh("$".toTypeName)
110+
case expr =>
111+
ctx.error("expected a name binding", expr.sourcePos)
112+
"$error".toTypeName
113113
}
114114
val typeSym = ctx.newSymbol(spliceOwner(ctx), name, EmptyFlags, TypeBounds.empty, NoSymbol, tree.expr.span)
115115
typeSym.addAnnotation(Annotation(New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(tree.expr.span)))
@@ -233,7 +233,6 @@ trait QuotesAndSplices {
233233
tdef.symbol.addAnnotation(Annotation(New(ref(defn.InternalQuoted_fromAboveAnnot.typeRef)).withSpan(tdef.span)))
234234
val bindingType = getBinding(tdef.symbol).symbol.typeRef
235235
val bindingTypeTpe = AppliedType(defn.QuotedTypeClass.typeRef, bindingType :: Nil)
236-
assert(tdef.name.startsWith("$"))
237236
val bindName = tdef.name.toString.stripPrefix("$").toTermName
238237
val sym = ctx0.newPatternBoundSymbol(bindName, bindingTypeTpe, tdef.span, flags = ImplicitTerm)
239238
buff += Bind(sym, untpd.Ident(nme.WILDCARD).withType(bindingTypeTpe)).withSpan(tdef.span)

tests/neg/i7603.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted._
2+
class Foo {
3+
def f[T2: Type](e: Expr[T2])(given QuoteContext) = e match {
4+
case '{ $x: ${'[List[$t]]} } => // error
5+
case '{ $x: ${y @ '[List[$t]]} } => // error // error
6+
}
7+
}

tests/neg/quotedPatterns-6.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.quoted._
2+
object Test {
3+
def test(x: quoted.Expr[Int])(given QuoteContext) = x match {
4+
case '{ poly[${Foo(t)}]($x); 4 } => ??? // error
5+
case '{ type $t; poly[${Foo(y: quoted.Type[`$t`])}]($x); 4 } => ??? // error
6+
case _ =>
7+
}
8+
9+
def poly[T](x: T): Unit = ()
10+
11+
}
12+

tests/pos/quotedPatterns.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ object Test {
3232
val a: quoted.matching.Sym[[T] =>> T => Int] = ff
3333
z
3434
case '{ poly[$t]($x); 4 } => ???
35-
case '{ poly[${Foo(t)}]($x); 4 } => ???
3635
case '{ type $X; poly[`$X`]($x); 4 } => ???
37-
case '{ type $t; poly[${Foo(x: quoted.Type[`$t`])}]($x); 4 } => ???
3836
case '{ type $T; val x: `$T` = $a; val y: `$T` = x; 1 } => ???
3937
case '{ type $t <: AnyRef; val x: `$t` = $a; val y: `$t` = x; 1 } => ???
4038
case _ => '{1}

0 commit comments

Comments
 (0)