Skip to content

Correct offset for single-string interpolation #8629

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 6 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 8 additions & 4 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1245,12 +1245,16 @@ object Parsers {
}
})

if (in.token == STRINGPART)
nextSegment(in.offset + (if (isTripleQuoted) 3 else 1))
var offsetCorrection = if isTripleQuoted then 3 else 1
def offset = {
val result = in.offset + offsetCorrection
offsetCorrection = 0
result
}
while (in.token == STRINGPART)
nextSegment(in.offset)
nextSegment(offset)
if (in.token == STRINGLIT)
segmentBuf += literal(inPattern = inPattern, negOffset = in.offset, inStringInterpolation = true)
segmentBuf += literal(inPattern = inPattern, negOffset = offset, inStringInterpolation = true)

InterpolatedString(interpolator, segmentBuf.toList)
}
Expand Down
43 changes: 43 additions & 0 deletions compiler/test/dotty/tools/dotc/parsing/PositionTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dotty.tools
package dotc
package parsing

import ast.untpd._
import org.junit.Test

class PositionTest extends ParserTest {

val tq = "\"\"\""
val program = s"""
|class A {
| val expr = 42
| val s0 = s"string1"
| val s1 = s"string1$${expr}string2"
| val s2 = s"string1$${expr}string2$${expr}string3"
| val s0m = s${tq}string1${tq}
| val s1m = s${tq}string1$${expr}string2${tq}
| val s2m = s${tq}string1$${expr}string2$${expr}string3${tq}
|}""".stripMargin

@Test
def interpolationLiteralPosition: Unit = {
val t = parseText(program)
t match {
case PackageDef(_, List(TypeDef(_, Template(_, _, _, statements: List[Tree])))) => {
val interpolations = statements.collect{ case ValDef(_, _, InterpolatedString(_, int)) => int }
val lits = interpolations.flatten.flatMap {
case l @ Literal(_) => List(l)
case Thicket(trees) => trees.collect { case l @ Literal(_) => l }
}
for {
lit <- lits
Literal(c) = lit
str <- List(c.value).collect { case str: String => str}
} {
val fromPos = program.substring(lit.span.start, lit.span.end)
assert(fromPos == str, s"$fromPos == $str")
}
}
}
}
}