Skip to content

Fix -Ymagic-offset-header for DoubleDefinition error#25706

Open
lihaoyi wants to merge 1 commit intoscala:mainfrom
lihaoyi:fix-magic-header
Open

Fix -Ymagic-offset-header for DoubleDefinition error#25706
lihaoyi wants to merge 1 commit intoscala:mainfrom
lihaoyi:fix-magic-header

Conversation

@lihaoyi
Copy link
Copy Markdown
Contributor

@lihaoyi lihaoyi commented Apr 3, 2026

This makes the reported error properly use remapped file paths and line numbers, where previously it did not. This was encountered trying to use -Ymagic-offset-header to properly refer to Mill's source .mill scripts during error reporting rather than wrapped generated source files

How much have you relied on LLM-based tools in this contribution?

Mostly vibe coded with claude

How was the solution tested?

Added a new unit test

Additional notes

Root cause: Symbol.sourcePos in Symbols.scala:388-391 returns source.atSpan(span) directly without checking the magic offset header. This means any error reported using
decl.srcPos (which goes through Symbol.sourcePos) — like DoubleDefinition — won't have its position remapped. In contrast, Positioned.sourcePos in Positioned.scala:55-63 does
check WrappedSourceFile.locateMagicHeader and remaps accordingly.

Fix: Added the same magic header check to Symbol.sourcePos:

final def sourcePos(using Context): SourcePosition = {
  val src = if source.exists then source else ctx.source
  WrappedSourceFile.locateMagicHeader(src) match                                                                                                                                   
    case WrappedSourceFile.MagicHeaderInfo.HasHeader(offset, originalFile)                                                                                                         
      if span.exists && span.start >= offset =>                                                                                                                                    
        originalFile.atSpan(span.shift(-offset))                                                                                                                                   
    case _ => src.atSpan(span)                                                                                                                                                     
}                                                                                                                                                                                  

Test: tests/neg/magic-offset-header-e_wrapper.scala — a wrapper with a def foo: String before the marker and def foo: Int after, triggering a DoubleDefinition error. The test
verifies the error position is remapped to the original file.

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.

1 participant