Skip to content
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

Multiple inlines make position data on reflect module trees inconsistent #21910

Open
felher opened this issue Nov 7, 2024 · 3 comments
Open
Labels

Comments

@felher
Copy link
Contributor

felher commented Nov 7, 2024

Compiler version

Tested on both 3.5.2 and 3.6.1

I tested on 3.6.1 as well because it already contains #21793

Main.scala

@main def hello(): Unit =
  Outer.outer:
    println("hi")

Outer.scala

object Outer:
  inline def outer(inline x: Any): Unit =
    Inner.pos(x)

Inner.scala

import scala.quoted.*

object Inner:
  inline def pos(inline x: Any): Unit =
    ${ posImpl('x) }

  def posImpl(x: Expr[Any])(using Quotes): Expr[Unit] =
    import quotes.reflect.*
    def printPos(p: Position): Unit =
      println(s"${p.start}..${p.end} of ${p.sourceFile.path} with length ${p.sourceFile.content.get.length}")

    printPos(x.asTerm.pos)
    printPos(x.asTerm.asInstanceOf[Inlined].body.pos)
    '{}

Output

70..71 of /tmp/down/inline-pos/src/main/scala/Main.scala with length 59
70..71 of /tmp/down/inline-pos/src/main/scala/Outer.scala with length 73

Expectation

Something different. The range for both cases is the x in Inner.pos(x) in Outer.scala, but the source file for the first one is Main.scala.

I thought maybe using inlining means that they are allowed to get inconsistent, but the docs on .start, .end clearly state that they are offsets in the source file and apply on the PositionModule states that "The range must be contained in the file."

@felher felher added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 7, 2024
@som-snytt
Copy link
Contributor

I don't know the API but printPos(Position.ofMacroExpansion) is correct.

@Gedochao Gedochao added area:positions area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 12, 2024
@Gedochao
Copy link
Contributor

Note, for the given examples I'm getting:

70..71 of /tmp//position-bug/Main.scala with length 58
70..71 of /tmp/position-bug/Outer.scala with length 72

likely an extra line was added somewhere to get the +1 result.

Anyway, tagging @jchyb & @bishabosha for opinions on this.

@som-snytt
Copy link
Contributor

On "terminal newline" at EOF, which Scala 2 adds to source text for ease of use, really does simplify, as a sentinel. "Trim to end of line" instead of "EOL or EOF". (That was my recent use case.)

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

No branches or pull requests

3 participants