Skip to content

Commit 1773b37

Browse files
authored
Merge pull request #1780 from dotty-staging/fix-i1779
fix #1779: support $_ and $_id in interpolated string
2 parents ba14bbe + 628a177 commit 1773b37

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

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

Lines changed: 9 additions & 5 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,10 +621,14 @@ 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 && inPattern) {
629+
in.nextToken()
630+
Ident(nme.WILDCARD)
631+
}
628632
else if (in.token == THIS) {
629633
in.nextToken()
630634
This(EmptyTypeIdent)
@@ -633,12 +637,12 @@ object Parsers {
633637
if (inPattern) Block(Nil, inBraces(pattern()))
634638
else expr()
635639
else {
636-
ctx.error(InterpolatedStringError())
640+
ctx.error(InterpolatedStringError(), source atPos Position(in.offset))
637641
EmptyTree
638642
}
639643
})
640644
}
641-
if (in.token == STRINGLIT) segmentBuf += literal()
645+
if (in.token == STRINGLIT) segmentBuf += literal(inPattern = inPattern)
642646
InterpolatedString(interpolator, segmentBuf.toList)
643647
}
644648

@@ -1444,7 +1448,7 @@ object Parsers {
14441448
case XMLSTART =>
14451449
xmlLiteralPattern()
14461450
case _ =>
1447-
if (isLiteral) literal()
1451+
if (isLiteral) literal(inPattern = true)
14481452
else {
14491453
syntaxErrorOrIncomplete(IllegalStartOfSimplePattern())
14501454
errorTermTree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ object Scanners {
758758
finishStringPart()
759759
nextRawChar()
760760
next.token = LBRACE
761-
} else if (Character.isUnicodeIdentifierStart(ch)) {
761+
} else if (Character.isUnicodeIdentifierStart(ch) || ch == '_') {
762762
finishStringPart()
763763
do {
764764
putChar(ch)

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+
}

tests/run/i1779.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extends

tests/run/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 unapply(arg: Any): Option[(Any, Any)] =
5+
Some((sc.parts(0), sc.parts(1)))
6+
}
7+
}
8+
9+
def main(args: Array[String]): Unit = {
10+
val q"class $_ extends $_parent" = new Object
11+
println(_parent)
12+
}
13+
}

0 commit comments

Comments
 (0)