Skip to content

Try using -Ymagic-offset-header to fixup line numbers#5932

Open
lihaoyi wants to merge 14 commits intocom-lihaoyi:mainfrom
lihaoyi:source-code-start
Open

Try using -Ymagic-offset-header to fixup line numbers#5932
lihaoyi wants to merge 14 commits intocom-lihaoyi:mainfrom
lihaoyi:source-code-start

Conversation

@lihaoyi
Copy link
Copy Markdown
Member

@lihaoyi lihaoyi commented Sep 27, 2025

This flag lets us remove a lot of our sketchy user-land workarounds to fix the filename and line numbers of errors:

  • AsmPositionUpdater to which handled runtime exception error positions
  • PositionalMapper and TransformingReporter which handled compile-time error positions
  • //SOURCECODE_ORIGINAL_FILE_PATH and //SOURCECODE_ORIGINAL_CODE_START_MARKER, which handled sourcecode.File/sourcecode.Line positioning

This replaces it with a single ///SOURCE_CODE_START comment referenced by -Ymagic-offset-header:SOURCE_CODE_START, which hooks in early enough in the compiler pipeline that all these downstream use cases can rely on it

The new ///SOURCE_CODE_START comment relies on character offsets unlike line numbers like the previous hacks, and so we needed to change our CodeGen.scala code to avoid mangling lines within the user code block. Concretely, we needed to stop translating object package into abstract class package extends blahblahblah. Instead we translate object package into class _package (which takes up the same number of characters to avoid affecting offsets) and move the extends blahblahblah into the generated code above the ///SOURCE_CODE_START header

@lihaoyi
Copy link
Copy Markdown
Member Author

lihaoyi commented Oct 29, 2025

Next blocker seems to be that Mill's codegen desugars

object `package` extends mill.Module {
abstract class package_  extends _root_.mill.util.MainRootModule, mill.Module {

This didn't affect the old error position fixup mechanism because it was line based, but the new one is offset based and so the longer line screws up all the subsequent offsets

@lihaoyi
Copy link
Copy Markdown
Member Author

lihaoyi commented Oct 29, 2025

Seems we might not be able to merge this until Mill 2.x, as the new codegen would require that MainModule be turned into a proper trait (without extending from class RootModule0) in order to make the only-inherit-from-one-class requirement line up.

Fixable, but would break binary compatibility to fix

@lihaoyi lihaoyi force-pushed the source-code-start branch 2 times, most recently from 318414b to dce77b0 Compare October 29, 2025 07:32
@lihaoyi
Copy link
Copy Markdown
Member Author

lihaoyi commented Oct 30, 2025

Got most tests passing with a modified codegen strategy, but will need to wait for Scala 3.8.0 to land with scala/scala3#24301 before we can merge this since that PR will likely miss 3.7.4

@lihaoyi
Copy link
Copy Markdown
Member Author

lihaoyi commented Dec 4, 2025

Can't land this yet because scala/scala3#24301 hasn't made it into 3.8.0-RC2

@lihaoyi lihaoyi force-pushed the source-code-start branch from febd40b to a004b45 Compare April 3, 2026 13:24
@lihaoyi lihaoyi force-pushed the source-code-start branch from f728354 to 856984d Compare April 3, 2026 13:58
@lihaoyi lihaoyi force-pushed the source-code-start branch from 65c14fa to 943d7b6 Compare April 3, 2026 16:18
@lihaoyi lihaoyi force-pushed the source-code-start branch from aec5708 to fa73b57 Compare April 4, 2026 04:00
@lihaoyi
Copy link
Copy Markdown
Member Author

lihaoyi commented Apr 4, 2026

Now blocked on scala/scala3#25706

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