Skip to content

First string part of interpolation is mispositioned #8627

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

Closed
martijnhoekstra opened this issue Mar 28, 2020 · 4 comments · Fixed by #8629
Closed

First string part of interpolation is mispositioned #8627

martijnhoekstra opened this issue Mar 28, 2020 · 4 comments · Fixed by #8629

Comments

@martijnhoekstra
Copy link
Contributor

minimized code

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")
        }
      }
    }
  }
}

Compilation output

[info] Compiling 1 Scala source to /home/martijn/dotty/compiler/target/scala-0.24/test-classes ...

expectation

Position of the string literal parts coincides with the actual source position.

@smarter
Copy link
Member

smarter commented Mar 28, 2020

[info] Compiling 1 Scala source to /home/martijn/dotty/compiler/target/scala-0.24/test-classes ...

Did you mean to paste the test output here ?

@martijnhoekstra
Copy link
Contributor Author

No, that's compilation output

@smarter
Copy link
Member

smarter commented Mar 28, 2020

I mean sure, but it's not very useful here :). You don't have to follow rigidly the issue template if there's some other useful information you can provide.

@martijnhoekstra
Copy link
Contributor Author

martijnhoekstra commented Mar 28, 2020

More information: the tree is mispositioned for interpolators that don't actually interpolate stuff, s"foo$bar" is unaffected. The string is mispositioned with the correct length at the first quote, so offset by 1 for a single quoted interpolation, and offset by 3 for a triple quoted interpolation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants