Skip to content

Commit 628a177

Browse files
committed
only allow $_ in patterns
1 parent d14de3d commit 628a177

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ object Parsers {
597597
val isNegated = negOffset < in.offset
598598
atPos(negOffset) {
599599
if (in.token == SYMBOLLIT) atPos(in.skipToken()) { SymbolLit(in.strVal) }
600-
else if (in.token == INTERPOLATIONID) interpolatedString()
600+
else if (in.token == INTERPOLATIONID) interpolatedString(inPattern)
601601
else finish(in.token match {
602602
case CHARLIT => in.charVal
603603
case INTLIT => in.intVal(isNegated).toInt
@@ -621,11 +621,11 @@ object Parsers {
621621
in.nextToken()
622622
while (in.token == STRINGPART) {
623623
segmentBuf += Thicket(
624-
literal(),
624+
literal(inPattern = inPattern),
625625
atPos(in.offset) {
626626
if (in.token == IDENTIFIER)
627627
termIdent()
628-
else if (in.token == USCORE) {
628+
else if (in.token == USCORE && inPattern) {
629629
in.nextToken()
630630
Ident(nme.WILDCARD)
631631
}
@@ -637,12 +637,12 @@ object Parsers {
637637
if (inPattern) Block(Nil, inBraces(pattern()))
638638
else expr()
639639
else {
640-
ctx.error(InterpolatedStringError())
640+
ctx.error(InterpolatedStringError(), source atPos Position(in.offset))
641641
EmptyTree
642642
}
643643
})
644644
}
645-
if (in.token == STRINGLIT) segmentBuf += literal()
645+
if (in.token == STRINGLIT) segmentBuf += literal(inPattern = inPattern)
646646
InterpolatedString(interpolator, segmentBuf.toList)
647647
}
648648

@@ -1448,7 +1448,7 @@ object Parsers {
14481448
case XMLSTART =>
14491449
xmlLiteralPattern()
14501450
case _ =>
1451-
if (isLiteral) literal()
1451+
if (isLiteral) literal(inPattern = true)
14521452
else {
14531453
syntaxErrorOrIncomplete(IllegalStartOfSimplePattern())
14541454
errorTermTree

tests/neg/i1779.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Test {
2+
implicit class Foo(sc: StringContext) {
3+
object q {
4+
def apply(arg: Any*): Int = 3
5+
}
6+
}
7+
8+
def f = {
9+
val _parent = 3
10+
q"val hello = $_parent"
11+
q"class $_" // error // error
12+
}
13+
}

0 commit comments

Comments
 (0)