Skip to content

Commit

Permalink
Add page about Scala 2 -Xsoruce:3 to migration guide
Browse files Browse the repository at this point in the history
  • Loading branch information
lrytz committed Feb 12, 2024
1 parent f3b233f commit f81f935
Show file tree
Hide file tree
Showing 24 changed files with 115 additions and 31 deletions.
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/external-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: External Resources
type: chapter
description: This section lists external resources about the migration to Scala 3.
num: 28
num: 29
previous-page: plugin-kind-projector
next-page:
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Contextual Abstractions
type: section
description: This chapter details all incompatibilities caused by the redesign of contextual abstractions
num: 18
num: 19
previous-page: incompat-dropped-features
next-page: incompat-other-changes
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/incompat-dropped-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Dropped Features
type: section
description: This chapter details all the dropped features
num: 17
num: 18
previous-page: incompat-syntactic
next-page: incompat-contextual-abstractions
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/incompat-other-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Other Changed Features
type: section
description: This chapter details all incompatibilities caused by changed features
num: 19
num: 20
previous-page: incompat-contextual-abstractions
next-page: incompat-type-checker
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/incompat-syntactic.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Syntactic Changes
type: section
description: This chapter details all the incompatibilities caused by syntactic changes
num: 16
num: 17
previous-page: incompatibility-table
next-page: incompat-dropped-features
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/incompat-type-checker.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Type Checker
type: section
description: This chapter details the unsoundness fixes in the type checker
num: 20
num: 21
previous-page: incompat-other-changes
next-page: incompat-type-inference
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/incompat-type-inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Type Inference
type: section
description: This chapter details the incompatibilities caused by the new type inference algorithm
num: 21
num: 22
previous-page: incompat-type-checker
next-page: options-intro
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/incompatibility-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Incompatibility Table
type: chapter
description: This chapter list all the known incompatibilities between Scala 2.13 and Scala 3
num: 15
num: 16
previous-page: tooling-syntax-rewriting
next-page: incompat-syntactic
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/options-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Compiler Options
type: chapter
description: This chapter shows the difference between Scala 2.13 and Scala 3 compiler options
num: 22
num: 23
previous-page: incompat-type-inference
next-page: options-lookup
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/options-lookup.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Compiler Options Lookup Table
type: section
description: This section contains the compiler options lookup tables
num: 23
num: 24
previous-page: options-intro
next-page: options-new
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/options-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: New Compiler Options
type: section
description: This chapter lists all the new compiler options in Scala 3
num: 24
num: 25
previous-page: options-lookup
next-page: scaladoc-settings-compatibility
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/plugin-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Compiler Plugins
type: chapter
description: This section shows how to migrate from using Scala 2 compiler plugins
num: 26
num: 27
previous-page: options-new
next-page: plugin-kind-projector
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/plugin-kind-projector.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Kind Projector Migration
type: section
description: This section shows how to migrate from the kind-projector plugin to Scala 3 kind-projector syntax
num: 27
num: 28
previous-page: plugin-intro
next-page: external-resources
---
Expand Down
7 changes: 4 additions & 3 deletions _overviews/scala3-migration/scala3-migrate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Porting an sbt Project (using sbt-scala3-migrate)
type: section
description: This section shows how to use scala3-migrate to migrate a project
num: 10
num: 11
previous-page: tutorial-prerequisites
next-page: tutorial-sbt
---
Expand All @@ -17,12 +17,13 @@ It consists of four sbt commands:
Each one of these commands is described in details below.

> #### Requirements
> - Scala 2.13, preferred 2.13.11
> - Scala 2.13, preferred 2.13.13
> - sbt 1.5 or higher
> - **Disclaimer:** This tool cannot migrate libraries containing macros.
>
> #### Recommendation
> Before the migration, add `-Xsource:3` to your scalac options to enable some Scala 3 syntax and behavior.
> Before the migration, add `-Xsource:3` to your scalac options to enable Scala 3 migration warnings in the Scala 2 compiler.
> See the page [Scala 2 with -Xsource:3](tooling-scala2-xsource3.html) for more details.
In this tutorial, we will migrate the project in [scalacenter/scala3-migration-example](https://github.com/scalacenter/scala3-migration-example).
To learn about the migration, and train yourself, you can clone this repository and follow the tutorial steps.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Scaladoc settings compatibility between Scala2 and Scala3
type: section
description: This chapter lists all the scaladoc options for Scala 2 and Scala 3, and explains the relations between them.
num: 25
num: 26
previous-page: options-new
next-page: plugin-intro
---
Expand Down
4 changes: 2 additions & 2 deletions _overviews/scala3-migration/tooling-migration-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: Scala 3 Migration Mode
type: chapter
description: This section describes the migration mode of the Scala 3 compiler
num: 7
previous-page: tooling-tour
num: 8
previous-page: tooling-scala2-xsource3
next-page: tutorial-intro
---

Expand Down
85 changes: 85 additions & 0 deletions _overviews/scala3-migration/tooling-scala2-xsource3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
title: Scala 2 with -Xsource:3
type: chapter
description: This section describes the Scala 2 compiler's -Xsource:3 flag
num: 7
previous-page: tooling-tour
next-page: tooling-migration-mode
---

The Scala 2.13 compiler issues helpful migration warnings with the `-Xsource:3` flag.
Before moving to the Scala 3 compiler, it's recommended to enable this flag in Scala 2 and address the new warnings.
In addition to new warnigns, the flag enables certain benign Scala 3 syntax such as `import p.*`.

This page explains the details behind the flag, an overview is shown using `scalac -Xsource:help`.

## Migration vs cross-building

With Scala 2.13.13 and newer, the `-Xsource:3` flag comes in two variants:

- `Xsource:3` enables warnings relevant for migrating a codebase to Scala 3.
- `Xsource:3-cross` is useful for projects that cross-build between Scala 2 and 3 for a longer period of time.
For certain language constructs that trigger a warning with `-Xsource:3`, the behavior changes to match Scala 3.

Details about individual warnings are listed below on this page.

## Fatal warnings and quick fixes

By default, Scala 3 migration warnings emitted by Scala 2.13 are fatal, i.e., they are reported as errors.
This can be changed using `-Wconf`, for example `-Wconf:cat=scala3-migration:w` changes them to be reported as warnings.
Alternatively, `-Xmigration` has the same effect.

The [`@nowarn` annotation](https://www.scala-lang.org/api/current/scala/annotation/nowarn.html) can be used to suppress individual warnings, which also works with fatal warnings enabled.

The Scala 2.13 compiler implements quick fixes for many Scala 3 migration warnings.
Quick fixes are displayed in Metals-based IDEs (not yet in IntelliJ), and they can be applied directly to the source code using the `-quickfix` flag, for example `-quickfix:cat=scala3-migration`.
See also `scalac -quickfix:help`.

## Enabled Scala 3 syntax

The `-Xsource:3` flag enables the following Scala 3 syntax in Scala 2:

- `import p.*`
- `import p.m as n`
- `import p.{given, *}`
- `case C(xs*)` as an alias for `case C(xs @ _*)`
- `A & B` type intersection as an alias for `A with B`
- Selecting a method `x.f` performs an eta-expansion (`x.f _`), even without an expected type


## Scala 3 migration warnings in detail

Many Scala 3 migration warnings are easy to understand and identical under `-Xsource:3` and `-Xsource:3-cross`, e.g., for implicit definitions without an explicit type:

{% highlight text %}
scala> object O { implicit val s = "" }
^
error: Implicit definition must have explicit type (inferred String) [quickfixable]
{% endhighlight %}

The following table explains warnings where where the behavior changes between `-Xsource:3` and `-Xsource:3-cross`.

| Feature | `-Xsource:3` | `-Xsource:3-cross` |
|--- |--- |--- |
| `(x: Any) + ""` is deprecated | deprecation warning | does not compile, implicit `any2stringadd` is not inferred |
| Unicode escapes in triple-quoted strings and raw interpolations (`"""\u0061"""`) | fatal warning, escape is processed | escape is not processed |
| Leading infix operators continue the previous line <sup>1</sup> | fatal warning, second line is a separate expression | operation continues the previous line |
| Desugaring of string interpolators using `StringContext` | fatal warning if the interpolation references a `StringContext` in scope different from `scala.StringContext` | desugaring always uses `scala.StringContext` |
| Modifiers of `apply` and `copy` methods of case classes with non-public constructors: `case class C private[p] (x: Int)` | fatal warning about the generated `apply` and `copy` being public | `apply` and `copy` inherit the access modifiers from the constructor |
| Inferred type of an overriding member <sup>2</sup> | fatal warning, the type of `B.f` is `String` | the type of `B.f` is `Object` |
| An implicit for type `p.A` is found in the package prefix `p` | fatal warning | the package prefix `p` is no longer part of the implicit search scope |

Example 1:

{% highlight text %}
def f =
1
+ 2
{% endhighlight %}

Example 2:

{% highlight text %}
trait A { def f: Object }
class B extends A { def f = "hi" }
{% endhighlight %}
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/tooling-syntax-rewriting.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Scala 3 Syntax Rewriting
type: chapter
description: This section describes the syntax rewriting capability of the Scala 3 compiler
num: 14
num: 15
previous-page: tutorial-macro-mixing
next-page: incompatibility-table
---
Expand Down
10 changes: 4 additions & 6 deletions _overviews/scala3-migration/tooling-tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type: chapter
description: This chapter is a tour of the migration tooling ecosystem
num: 6
previous-page: compatibility-metaprogramming
next-page: tooling-migration-mode
next-page: tooling-scala2-xsource3
---

## The Scala Compilers
Expand All @@ -13,12 +13,10 @@ The migration has been carefully prepared beforehand in each of the two compiler

### The Scala 2.13 Compiler

The Scala 2.13 compiler supports `-Xsource:3`, an option that enables some Scala 3 syntax and behavior:
- Most deprecated syntax generates an error.
- Infix operators can start a line in the middle of a multiline expression.
- Implicit search and overload resolution follow Scala 3 handling of contravariance when checking specificity.
The Scala 2.13 compiler supports `-Xsource:3`, an option that enables migration warnings and certain Scala 3 syntax and behavior.

The [Scala 2 with -Xsource:3](tooling-scala2-xsource3.html) page explains the flag in detail.

The `-Xsource:3` option is intended to encourage early migration.

### The Scala 3 Compiler

Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/tutorial-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Migration Tutorial
type: chapter
description: This chapter contains the tutorials for porting a Scala 2.13 project to Scala 3
num: 8
num: 9
previous-page: tooling-migration-mode
next-page: tutorial-prerequisites
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Cross-Building a Macro Library
type: section
description: This section shows how to cross-build a macro library
num: 12
num: 13
previous-page: tutorial-sbt
next-page: tutorial-macro-mixing
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/tutorial-macro-mixing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Mixing Scala 2.13 and Scala 3 Macros
type: section
description: This section shows how to mix Scala 2.13 and Scala 3 macros in a single artifact
num: 13
num: 14
previous-page: tutorial-macro-mixing
next-page: tooling-syntax-rewriting
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/tutorial-prerequisites.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Prerequisites
type: section
description: This section details the prerequisites of migration to Scala 3
num: 9
num: 10
previous-page: tutorial-intro
next-page: scala3-migrate
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-migration/tutorial-sbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Porting an sbt Project (by hand)
type: section
description: This section shows how to port an sbt project
num: 11
num: 12
previous-page: scala3-migrate
next-page: tutorial-macro-cross-building
---
Expand Down

0 comments on commit f81f935

Please sign in to comment.