Skip to content

Conversation

@lihaoyi
Copy link
Contributor

@lihaoyi lihaoyi commented Oct 29, 2025

Fixes #24300

Tested manually with the reproduction in the original ticket, seems that quotes.reflect.Position.ofMacroExpansion.startLine and .sourceFile now correctly considers the ///MAGIC:build.sbt comment

lihaoyi scala3$ bin/scala -Ymagic-offset-header:MAGIC
The `--offline` option is experimental
Please bear in mind that non-ideal user experience should be expected.
If you encounter any bugs or have feedback to share, make sure to reach out to the maintenance team at https://github.com/VirtusLab/scala-cli
Welcome to Scala 3.8.0-RC1-bin-SNAPSHOT-git-840804f (17.0.14, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> import scala.quoted.*
     | def lineImpl(using Quotes): Expr[Int] = {
     |   val sourceFile = quotes.reflect.Position.ofMacroExpansion.sourceFile
     |   val line = quotes.reflect.Position.ofMacroExpansion.startLine + 1
     |   Expr(line)
     | }
     | inline implicit def line: Int = ${ lineImpl }
     |
     | def fileImpl(using Quotes): Expr[String] = {
     |   import quotes.reflect._
     |   Expr(quotes.reflect.Position.ofMacroExpansion.sourceFile.path)
     | }
     |
     | inline implicit def file: String = ${ fileImpl }
def lineImpl(using x$1: scala.quoted.Quotes): scala.quoted.Expr[Int]
def line: Int
def fileImpl(using x$1: scala.quoted.Quotes): scala.quoted.Expr[String]
def file: String

scala> {
     |
     |
     | ///MAGIC:build.sbt
     | println(file)
     | println(line)
     | def bar = ???
     | bar
     | }
build.sbt
1
scala.NotImplementedError: an implementation is missing
  at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
  at rs$line$3$.bar(rs$line$3:2)
  ... 32 elided

Copy link
Contributor

@Linyxus Linyxus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. But I wonder why we created a new SourcePos in the first place. I am not super familiar with the macro part, so I will want for @jchyb to approve.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-Ymagic-offset-header does not affect quotes.reflect.Position.ofMacroExpansion

3 participants