Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Can/should I use both -Xsource:3 and -source:3.0-migration in a cross-compiled codebase? #17591

Discussion options

I'd like to refresh the compiler flags configured by sbt-typelevel which is used to cross-compile several libraries for both Scala 2 and 3. I have a question about:

  • -Xsource:3 in Scala 2, which "enables some Scala 3 syntax and behavior"

  • -source:3.0-migration in Scala 3, which "accepts some of the old Scala 2.13 syntax"

Ref: https://docs.scala-lang.org/scala3/guides/migration/tooling-tour.html

Does it make sense to use both of these flags in a cross-compiled codebase? I ask, because the former sounds like "make Scala 2 accept Scala 3-ish sources" and the latter sounds like "make Scala 3 accept Scala 2-ish sources". So I wonder if we should pick one strategy (aim for either Scala 2-ish or Scala 3-ish sources) and use the one flag that makes sense for that.

Also what about -source:3.3-migration? 😅 Can that be mixed with -Xsource:3?

Case Study

Consider the following program:

case class Foo private (bar: String)
object Main {
 def main(args: Array[String]): Unit =
 println(Foo("bar"))
}

Actually, this doesn't compile out-of-the-box with Scala 3.3.0.

[error] object Foo does not take parameters
[error] println(Foo("bar"))
[error] ^^^

Now here is what happens with various Scala version and compiler flag configurations:

Options / Scala 2.13.10 3.3.0
no options
-Xsource:3 n/a
-source:3.0-migration n/a
-source:3.3-migration n/a

Analysis:

  • -Xsource:3 and -source:3.0-migration have the opposite effect on their respective compilers. So using them together seems pretty dumb.
  • -Xsource:3 and -source:3.3-migration in this specific situation have the same result. So maybe they can be used together?
You must be logged in to vote

If you're cross-compiling, then I think -Xsource:3 isn't a problem because the things it reject will also be rejected by Scala 3 anyway, and it will mean you can use new syntax, but it's not a big deal either way.

Replies: 2 comments 9 replies

Comment options

There is an old ticket somewhere to document -Xsource for Scala 2.

The purpose of -Xsource:3 was to get source in a place where it compiles straight under scala 3.

You must be logged in to vote
0 replies
Comment options

I don't think a plugin should unilaterally touch -source, since it can lead to a lot of confusion down the road without the user realizing what the culprit is. For example -source:3.0-migration can break Scala 3 code because it changes implicit resolution rules (e.g., nested implicits will lead to ambiguity errors, but Scala 3 APIs using context function types routinely rely on that).

You must be logged in to vote
9 replies
Comment options

and ideally it should only be used temporarily during a migration

Hmm, you mean like migrating from Scala 2 to Scala 3? What about for libraries that are permanently cross-compiled?

Comment options

smarter May 25, 2023
Collaborator

Such libraries need to be written in the common subset of Scala 2 and 3 which has the same meaning in both languages (and use version-specific sources when that's not possible).

Comment options

Ok, thanks! So if I'm understanding correctly, cross-compiling libraries should use neither -Xsource:3 nor -source:3.x-migration ?

Comment options

smarter May 25, 2023
Collaborator

If you're cross-compiling, then I think -Xsource:3 isn't a problem because the things it reject will also be rejected by Scala 3 anyway, and it will mean you can use new syntax, but it's not a big deal either way.

Answer selected by ckipp01
Comment options

I disagree that -Xsource:3 doesn't matter. Modulo bugs, you can use leading infix. You can't use early defs. It was a bigger commitment when it included the implicits resolution change.

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

AltStyle によって変換されたページ (->オリジナル) /