Skip to content

Commit 604b354

Browse files
authored
Merge pull request #2966 from lrytz/mgs3
Add page about Scala 2 `-Xsource:3` to migration guide
2 parents 42a6e8e + 4297425 commit 604b354

25 files changed

+122
-36
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
This repository contains the source for the Scala documentation website, as well as the source for "Scala Improvement Process" (SIP) documents.
66

7+
## Dependencies ##
8+
9+
This site uses a Jekyll, a Ruby framework. You'll need Ruby and Bundler installed; see [Jekyll installation instructions](https://jekyllrb.com/docs/installation/) for the details.
10+
711
## Quickstart ##
812

913
To build and view the site locally:
1014

11-
gem install --user-install bundler jekyll
15+
bundle install
1216
bundle exec jekyll serve -I
1317

1418
([Trouble on MacOS?](https://github.com/scala/docs.scala-lang/issues/1150))
@@ -53,10 +57,6 @@ Small changes, or corrected typos will generally be pulled in right away. Large
5357
existing documents will be thoroughly reviewed-- please keep in mind that, generally, new documents must be very well-polished, complete, and maintained
5458
in order to be accepted.
5559

56-
## Dependencies ##
57-
58-
This site uses a Jekyll, a Ruby framework. You'll need Ruby and Bundler installed; see [Jekyll installation instructions](https://jekyllrb.com/docs/installation/) for the details.
59-
6060
## Building & Viewing ##
6161

6262
cd into the directory where you cloned this repository, then install the required gems with `bundle install`. This will automatically put the gems into `./vendor/bundle`.

_overviews/scala3-migration/external-resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: External Resources
33
type: chapter
44
description: This section lists external resources about the migration to Scala 3.
5-
num: 28
5+
num: 29
66
previous-page: plugin-kind-projector
77
next-page:
88
---

_overviews/scala3-migration/incompat-contextual-abstractions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Contextual Abstractions
33
type: section
44
description: This chapter details all incompatibilities caused by the redesign of contextual abstractions
5-
num: 18
5+
num: 19
66
previous-page: incompat-dropped-features
77
next-page: incompat-other-changes
88
---

_overviews/scala3-migration/incompat-dropped-features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Dropped Features
33
type: section
44
description: This chapter details all the dropped features
5-
num: 17
5+
num: 18
66
previous-page: incompat-syntactic
77
next-page: incompat-contextual-abstractions
88
---

_overviews/scala3-migration/incompat-other-changes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Other Changed Features
33
type: section
44
description: This chapter details all incompatibilities caused by changed features
5-
num: 19
5+
num: 20
66
previous-page: incompat-contextual-abstractions
77
next-page: incompat-type-checker
88
---

_overviews/scala3-migration/incompat-syntactic.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Syntactic Changes
33
type: section
44
description: This chapter details all the incompatibilities caused by syntactic changes
5-
num: 16
5+
num: 17
66
previous-page: incompatibility-table
77
next-page: incompat-dropped-features
88
---

_overviews/scala3-migration/incompat-type-checker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Type Checker
33
type: section
44
description: This chapter details the unsoundness fixes in the type checker
5-
num: 20
5+
num: 21
66
previous-page: incompat-other-changes
77
next-page: incompat-type-inference
88
---

_overviews/scala3-migration/incompat-type-inference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Type Inference
33
type: section
44
description: This chapter details the incompatibilities caused by the new type inference algorithm
5-
num: 21
5+
num: 22
66
previous-page: incompat-type-checker
77
next-page: options-intro
88
---

_overviews/scala3-migration/incompatibility-table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Incompatibility Table
33
type: chapter
44
description: This chapter list all the known incompatibilities between Scala 2.13 and Scala 3
5-
num: 15
5+
num: 16
66
previous-page: tooling-syntax-rewriting
77
next-page: incompat-syntactic
88
---

_overviews/scala3-migration/options-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Compiler Options
33
type: chapter
44
description: This chapter shows the difference between Scala 2.13 and Scala 3 compiler options
5-
num: 22
5+
num: 23
66
previous-page: incompat-type-inference
77
next-page: options-lookup
88
---

_overviews/scala3-migration/options-lookup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Compiler Options Lookup Table
33
type: section
44
description: This section contains the compiler options lookup tables
5-
num: 23
5+
num: 24
66
previous-page: options-intro
77
next-page: options-new
88
---

_overviews/scala3-migration/options-new.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: New Compiler Options
33
type: section
44
description: This chapter lists all the new compiler options in Scala 3
5-
num: 24
5+
num: 25
66
previous-page: options-lookup
77
next-page: scaladoc-settings-compatibility
88
---

_overviews/scala3-migration/plugin-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Compiler Plugins
33
type: chapter
44
description: This section shows how to migrate from using Scala 2 compiler plugins
5-
num: 26
5+
num: 27
66
previous-page: options-new
77
next-page: plugin-kind-projector
88
---

_overviews/scala3-migration/plugin-kind-projector.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Kind Projector Migration
33
type: section
44
description: This section shows how to migrate from the kind-projector plugin to Scala 3 kind-projector syntax
5-
num: 27
5+
num: 28
66
previous-page: plugin-intro
77
next-page: external-resources
88
---

_overviews/scala3-migration/scala3-migrate.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Porting an sbt Project (using sbt-scala3-migrate)
33
type: section
44
description: This section shows how to use scala3-migrate to migrate a project
5-
num: 10
5+
num: 11
66
previous-page: tutorial-prerequisites
77
next-page: tutorial-sbt
88
---
@@ -17,12 +17,13 @@ It consists of four sbt commands:
1717
Each one of these commands is described in details below.
1818

1919
> #### Requirements
20-
> - Scala 2.13, preferred 2.13.11
20+
> - Scala 2.13, preferred 2.13.13
2121
> - sbt 1.5 or higher
2222
> - **Disclaimer:** This tool cannot migrate libraries containing macros.
2323
>
2424
> #### Recommendation
25-
> Before the migration, add `-Xsource:3` to your scalac options to enable some Scala 3 syntax and behavior.
25+
> Before the migration, add `-Xsource:3` to your scalac options to enable Scala 3 migration warnings in the Scala 2 compiler.
26+
> See the page [Scala 2 with -Xsource:3](tooling-scala2-xsource3.html) for more details.
2627
2728
In this tutorial, we will migrate the project in [scalacenter/scala3-migration-example](https://github.com/scalacenter/scala3-migration-example).
2829
To learn about the migration, and train yourself, you can clone this repository and follow the tutorial steps.

_overviews/scala3-migration/scaladoc-settings-compatibility.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Scaladoc settings compatibility between Scala2 and Scala3
33
type: section
44
description: This chapter lists all the scaladoc options for Scala 2 and Scala 3, and explains the relations between them.
5-
num: 25
5+
num: 26
66
previous-page: options-new
77
next-page: plugin-intro
88
---

_overviews/scala3-migration/tooling-migration-mode.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: Scala 3 Migration Mode
33
type: chapter
44
description: This section describes the migration mode of the Scala 3 compiler
5-
num: 7
6-
previous-page: tooling-tour
5+
num: 8
6+
previous-page: tooling-scala2-xsource3
77
next-page: tutorial-intro
88
---
99

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Scala 2 with -Xsource:3
3+
type: chapter
4+
description: This section describes the Scala 2 compiler's -Xsource:3 flag
5+
num: 7
6+
previous-page: tooling-tour
7+
next-page: tooling-migration-mode
8+
---
9+
10+
The Scala 2.13 compiler issues helpful migration warnings with the `-Xsource:3` flag.
11+
12+
Before moving to the Scala 3 compiler, it's recommended to enable this flag in Scala 2 and address the new warnings.
13+
14+
There is also a variant, `-Xsource:3-cross`; see below.
15+
16+
This page explains the details behind the flags. An overview is shown using `scalac -Xsource:help`.
17+
18+
## Migration vs cross-building
19+
20+
With Scala 2.13.13 and newer, the `-Xsource:3` flag comes in two variants:
21+
22+
- `Xsource:3` enables warnings relevant for migrating a codebase to Scala 3.
23+
In addition to new warnings, the flag enables certain benign Scala 3 syntaxes such as `import p.*`.
24+
- `Xsource:3-cross` is useful for projects that cross-build between Scala 2 and 3 for a longer period of time.
25+
For certain language constructs that trigger a warning with `-Xsource:3`, the behavior changes to match Scala 3.
26+
27+
Details about individual warnings are listed below on this page.
28+
29+
## Fatal warnings and quick fixes
30+
31+
By default, Scala 3 migration warnings emitted by Scala 2.13 are fatal, i.e., they are reported as errors.
32+
This can be changed using `-Wconf`, for example `-Wconf:cat=scala3-migration:w` changes them to be reported as warnings.
33+
Alternatively, `-Xmigration` has the same effect.
34+
35+
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.
36+
37+
The Scala 2.13 compiler implements quick fixes for many Scala 3 migration warnings.
38+
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`.
39+
See also `scalac -quickfix:help`.
40+
41+
## Enabled Scala 3 syntax
42+
43+
The `-Xsource:3` flag enables the following Scala 3 syntaxes in Scala 2:
44+
45+
- `import p.*`
46+
- `import p.m as n`
47+
- `import p.{given, *}`
48+
- `case C(xs*)` as an alias for `case C(xs @ _*)`
49+
- `A & B` type intersection as an alias for `A with B`
50+
- Selecting a method `x.f` performs an eta-expansion (`x.f _`), even without an expected type
51+
52+
## Scala 3 migration warnings in detail
53+
54+
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:
55+
56+
{% highlight text %}
57+
scala> object O { implicit val s = "" }
58+
^
59+
error: Implicit definition must have explicit type (inferred String) [quickfixable]
60+
{% endhighlight %}
61+
62+
The following table explains warnings where where the behavior changes between `-Xsource:3` and `-Xsource:3-cross`.
63+
64+
| Feature | `-Xsource:3` | `-Xsource:3-cross` |
65+
|--- |--- |--- |
66+
| `(x: Any) + ""` is deprecated | deprecation warning | does not compile, implicit `any2stringadd` is not inferred |
67+
| Unicode escapes in triple-quoted strings and raw interpolations (`"""\u0061"""`) | fatal warning, escape is processed | escape is not processed |
68+
| Leading infix operators continue the previous line <sup>1</sup> | fatal warning, second line is a separate expression | operation continues the previous line |
69+
| 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` |
70+
| 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 |
71+
| 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` |
72+
| 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 |
73+
74+
Example 1:
75+
76+
{% highlight text %}
77+
def f =
78+
1
79+
+ 2
80+
{% endhighlight %}
81+
82+
Example 2:
83+
84+
{% highlight text %}
85+
trait A { def f: Object }
86+
class B extends A { def f = "hi" }
87+
{% endhighlight %}

_overviews/scala3-migration/tooling-syntax-rewriting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Scala 3 Syntax Rewriting
33
type: chapter
44
description: This section describes the syntax rewriting capability of the Scala 3 compiler
5-
num: 14
5+
num: 15
66
previous-page: tutorial-macro-mixing
77
next-page: incompatibility-table
88
---

_overviews/scala3-migration/tooling-tour.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type: chapter
44
description: This chapter is a tour of the migration tooling ecosystem
55
num: 6
66
previous-page: compatibility-metaprogramming
7-
next-page: tooling-migration-mode
7+
next-page: tooling-scala2-xsource3
88
---
99

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

1414
### The Scala 2.13 Compiler
1515

16-
The Scala 2.13 compiler supports `-Xsource:3`, an option that enables some Scala 3 syntax and behavior:
17-
- Most deprecated syntax generates an error.
18-
- Infix operators can start a line in the middle of a multiline expression.
19-
- Implicit search and overload resolution follow Scala 3 handling of contravariance when checking specificity.
16+
The Scala 2.13 compiler supports `-Xsource:3`, an option that enables migration warnings and certain Scala 3 syntax and behavior.
17+
18+
The [Scala 2 with -Xsource:3](tooling-scala2-xsource3.html) page explains the flag in detail.
2019

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

2321
### The Scala 3 Compiler
2422

_overviews/scala3-migration/tutorial-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Migration Tutorial
33
type: chapter
44
description: This chapter contains the tutorials for porting a Scala 2.13 project to Scala 3
5-
num: 8
5+
num: 9
66
previous-page: tooling-migration-mode
77
next-page: tutorial-prerequisites
88
---

_overviews/scala3-migration/tutorial-macro-cross-building.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Cross-Building a Macro Library
33
type: section
44
description: This section shows how to cross-build a macro library
5-
num: 12
5+
num: 13
66
previous-page: tutorial-sbt
77
next-page: tutorial-macro-mixing
88
---

_overviews/scala3-migration/tutorial-macro-mixing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Mixing Scala 2.13 and Scala 3 Macros
33
type: section
44
description: This section shows how to mix Scala 2.13 and Scala 3 macros in a single artifact
5-
num: 13
5+
num: 14
66
previous-page: tutorial-macro-mixing
77
next-page: tooling-syntax-rewriting
88
---

_overviews/scala3-migration/tutorial-prerequisites.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Prerequisites
33
type: section
44
description: This section details the prerequisites of migration to Scala 3
5-
num: 9
5+
num: 10
66
previous-page: tutorial-intro
77
next-page: scala3-migrate
88
---

_overviews/scala3-migration/tutorial-sbt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Porting an sbt Project (by hand)
33
type: section
44
description: This section shows how to port an sbt project
5-
num: 11
5+
num: 12
66
previous-page: scala3-migrate
77
next-page: tutorial-macro-cross-building
88
---

0 commit comments

Comments
 (0)