Skip to content

Commit b9e014e

Browse files
Merge pull request #2194 from scalacenter/add-scala-3-contributing-guide
Add scala 3 contributing guide - MVP
2 parents dacc9be + 3a276c1 commit b9e014e

28 files changed

+1600
-5
lines changed

_config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ defaults:
116116
overview-name: "Scala 3 Migration Guide"
117117
layout: multipage-overview
118118
permalink: "/scala3/guides/migration/:title.html"
119+
-
120+
scope:
121+
path: "_overviews/scala3-contribution"
122+
values:
123+
scala3: true
124+
partof: scala3-contribution
125+
type: section
126+
overview-name: "Guide to Scala 3 Compiler Contribution"
127+
layout: multipage-overview
128+
permalink: "/scala3/guides/contribution/:title.html"
119129
-
120130
scope:
121131
path: "_overviews/scala3-macros"

_data/scala3-doc-nav-header.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
url: "/scala3/getting-started.html"
88
- title: Scala 3 Book
99
url: "/scala3/book/introduction.html"
10+
- title: Contributing Guide
11+
url: "/scala3/guides/contribution/contribution-intro.html"
1012
- title: Macro Tutorial
1113
url: "/scala3/guides/macros/index.html"
1214
- title: Migrate
@@ -15,3 +17,5 @@
1517
url: "/scala3/reference/overview.html"
1618
- title: API
1719
url: "https://scala-lang.org/api/3.x/"
20+
- title: Contributing Guide
21+
url: "/scala3/guides/contribution/contribution-intro.html"

_ja/scala3/contribute-to-docs.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Scala 3 の高品質なドキュメンテーションを作るためのいくつ
3434
- [Issues](https://github.com/scala/docs.scala-lang/issues)
3535

3636
## Macros Tutorial
37-
[Macros Tutorial](/scala3/guides/macros)は Nicolas Stucki 氏 が書いている。この本では Scala 3 のマクロとそのベストプラクティスについて詳しく説明している。
37+
[Macros Tutorial](/scala3/guides/macros)は Nicolas Stucki 氏 が書いている。この本では Scala 3 のマクロとそのベストプラクティスについて詳しく説明している。
3838

3939
- [Sources](https://github.com/scala/docs.scala-lang/tree/main/_overviews/scala3-macros)
4040
- [Issues](https://github.com/scala/docs.scala-lang/issues)
@@ -45,6 +45,12 @@ Scala 3 の高品質なドキュメンテーションを作るためのいくつ
4545
- [Source](https://github.com/scala/docs.scala-lang/tree/main/_overviews/scala3-migration)
4646
- [Issues](https://github.com/scalacenter/docs.scala-lang/issues)
4747

48+
## Scala 3 Contribution Guide
49+
[Scala 3 Contribution Guide](/scala3/guides/contribution/contribution-intro.html)
50+
Scala 3 コンパイラとライブラリへの貢献と内部に関する包括的な概要が含まれています
51+
52+
- [Source](https://github.com/scala/docs.scala-lang/tree/main/_overviews/scala3-contribution)
53+
- [Issues](https://github.com/scala/docs.scala-lang/issues)
4854

4955
## Scala 3 Language Reference
5056
The [Dotty reference](/scala3/reference/overview.html) は Scala 3 になる予定である。これにはさまざまな言語仕様に関する公式のプレゼンテーションや技術的情報が含まれている。
@@ -54,4 +60,3 @@ The [Dotty reference](/scala3/reference/overview.html) は Scala 3 になる予
5460

5561

5662
[scala3-book]: {% link _overviews/scala3-book/introduction.md %}
57-

_ja/scala3/new-in-scala3.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Scala 3 は Scala 2 から大幅な改善が行われ、さまざまな新機能
1313
- [Syntax Summary][syntax-summary] では Scala 3 で新しく追加されたシンタックスを解説している。
1414
- [Language Reference][reference] を見ればScala 2 と Scala 3 の変更点を詳しく確認できる。
1515
- Scala 2 から Scala 3 への移行を考えている方は[Migration Guide][migration] を参照。
16+
- [Contribution Guide][contribution] は、問題を修正するためのガイドを含め、コンパイラーをさらに深く掘り下げます。
1617

1718
## What's new in Scala 3
1819
Scala 3 は Scala 2 を徹底的に見直して再設計されている。核心部分で、型システムの多くの面が変更されより原理原則に基づいたものになった。この変更によって新機能(ユニオン型)が使えるようになったことにくわえて、なにより型システムがさらに使いやすくなった。 例えば、[型推論][type-inference] や overload resolution が改善された.
@@ -37,7 +38,7 @@ Scala 3 ではひとつの強力な機能として`implicit`を提供するの
3738
- **Providing Type-class instances**. [Given instances][contextual-givens] を使ってある型に対応する _canonical value_ を定義することができる。実装を公開することなく、型クラスを使ったプログラミングをよりわかりやすく書ける。
3839

3940
- **Retroactively extending classes**. Scala 2 では拡張メソッドは暗黙の変換か implicit classを使って書くことができた. 一方 Scala 3 では [extension methods][contextual-extension] が直接的に言語仕様に含まれているのでよりわかりやすいエラーメッセージを表示できる。型推論も改善された。
40-
- **Viewing one type as another**. 暗黙の変換は型クラス`Conversion`のインスタンスとしてゼロから [再設計][contextual-conversions]された。
41+
- **Viewing one type as another**. 暗黙の変換は型クラス`Conversion`のインスタンスとしてゼロから [再設計][contextual-conversions]された。
4142
- **Higher-order contextual abstractions**. 全く新しい機能である [context functions][contextual-functions] は暗黙の引数をとる関数型を第一級オブジェクトとして扱う。この機能はライブラリ作者にとって重要である。また、簡潔なドメイン特化言語(DSL)を記述するのにも役立つ。
4243

4344
- **Actionable feedback from the compiler**. コンパイラが暗黙の引数の解決に失敗した場合、解決に役立つ [import suggestions](https://www.scala-lang.org/blog/2020/05/05/scala-3-import-suggestions.html) を提示する。
@@ -99,6 +100,7 @@ Scala 3 のメタプログラミングについてもっと知りたいかたは
99100
[reference]: {{ site.scala3ref }}/overview.html
100101
[creator]: {{ site.scala3ref }}/other-new-features/creator-applications.html
101102
[migration]: {% link _overviews/scala3-migration/compatibility-intro.md %}
103+
[contribution]: {% link _overviews/scala3-contribution/contribution-intro.md %}
102104

103105
[implicits]: {{ site.scala3ref }}/contextual.html
104106
[contextual-using]: {{ site.scala3ref }}/contextual/using-clauses.html
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Contexts
3+
type: section
4+
description: This page describes symbols in the Scala 3 compiler.
5+
num: 18
6+
previous-page: arch-symbols
7+
next-page:
8+
---
9+
10+
> (The following is work in progress), adapted from dotty.epfl.ch
11+
12+
The `Context` contains the state of the compiler, for example
13+
14+
* `settings`
15+
* `freshNames` (`FreshNameCreator`)
16+
* `period` (run and phase id)
17+
* `compilationUnit`
18+
* `phase`
19+
* `tree` (current tree)
20+
* `typer` (current typer)
21+
* `mode` (type checking mode)
22+
* `typerState` (for example undetermined type variables)
23+
* ...
24+
25+
### Contexts in the typer
26+
The type checker passes contexts through all methods and adapts fields where
27+
necessary, e.g.
28+
29+
```scala
30+
case tree: untpd.Block => typedBlock(desugar.block(tree), pt)(ctx.fresh.withNewScope)
31+
```
32+
33+
A number of fields in the context are typer-specific (`mode`, `typerState`).
34+
35+
### In other phases
36+
Other phases need a context for many things, for example to access the
37+
denotation of a symbols (depends on the period). However they typically don't
38+
need to modify / extend the context while traversing the AST. For these phases
39+
the context can be simply an implicit class parameter that is then available in
40+
all members.
41+
42+
**Careful**: beware of memory leaks. Don't hold on to contexts in long lived
43+
objects.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: High Level Architecture
3+
type: chapter
4+
description: This page introduces the high level architecture of the Scala 3 compiler.
5+
num: 12
6+
previous-page: procedures-checklist
7+
next-page: arch-lifecycle
8+
---
9+
10+
This chapter of the guide describes the architecture and concepts of `dotc`,
11+
the Scala 3 compiler, including answers to questions such as:
12+
- "What are the transformations that happen to my code?"
13+
- "How do I run a compiler programatically?"
14+
- "What are symbols, denotations, names and types?"
15+
- "What is a compiler phase?"
16+
- "What is the compiler Context?"
17+
18+
and many more.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: Compiler Overview
3+
type: section
4+
description: This page describes the lifecycle for the Scala 3 compiler.
5+
num: 13
6+
previous-page: arch-intro
7+
next-page: arch-phases
8+
---
9+
10+
As mentioned in [what is a compiler?][1] `dotc` produces programs that can run on your machine,
11+
first by verifying its input is a valid Scala program, and then transforming it into a
12+
representation that can run on one of Scala's target platforms.
13+
14+
## Introducing the Compiler's Lifecycle
15+
16+
#### Core
17+
At a high level, `dotc` centers its work around a [Compiler][4], which maintains an ordered
18+
list of [phases][3], and is responsible for creating new [runs][2].
19+
A run is a complete iteration of the compiler's phases over a list of input sources.
20+
A compiler is designed to be reusable and can create many runs over its lifetime.
21+
22+
#### Runs
23+
During a run, the input sources are converted to [compilation units][5] (i.e. the abstraction of
24+
compiler state associated with each input source); then iteratively: a single phase is applied to
25+
every compilation unit before progressing to the next phase.
26+
27+
#### Phases
28+
A phase is an abstract transformation over a compilation unit, it is usually responsible
29+
for transforming the trees and types representing the code of a source file. The primary phases of
30+
the compiler are the `parser`, which converts text that matches Scala's
31+
[syntax][10] into abstract syntax trees, ASTs, and the `typer`, which checks that
32+
trees conform to expected types, and performs other operations such as implicit search.
33+
[You can read more about phases here][9].
34+
35+
#### Drivers
36+
37+
The core compiler also requires a lot of state to be initialised before use, such as [settings][8]
38+
and the [Context]. For convenience, the [Driver] class contains high level functions for
39+
configuring the compiler and invoking it programatically. The object [Main] inherits from `Driver`
40+
and is invoked by the `scalac` script.
41+
42+
<!-- #### Further Reading -->
43+
> [Read more about a compiler's lifecyle][6].
44+
45+
## Code Structure
46+
47+
The code of the compiler is found in the package [dotty.tools][7],
48+
containing the following sub-packages:
49+
```scala
50+
tools // contains helpers and the `scala` generic runner
51+
├── backend // Compiler backends (currently JVM and JS)
52+
├── dotc // The main compiler, with subpackages:
53+
├── ast // Abstract syntax trees
54+
   ├── classpath
55+
   ├── config // Compiler configuration, settings, platform specific definitions.
56+
   ├── core // Core data structures and operations, with specific subpackages for:
57+
      ├── classfile // Reading of Java classfiles into core data structures
58+
      ├── tasty // Reading and writing of TASTY files to/from core data structures
59+
      └── unpickleScala2 // Reading of Scala2 symbol information into core data structures
60+
   ├── decompiler // pretty printing TASTY as code
61+
   ├── fromtasty // driver for recompilation from TASTY
62+
   ├── interactive // presentation compiler and code completions
63+
   ├── parsing // Scanner and parser
64+
   ├── plugins // compile plugin definitions
65+
   ├── printing // Pretty-printing trees, types and other data
66+
   ├── profile // internals for profiling the compiler
67+
   ├── quoted // internals for quoted reflection
68+
   ├── reporting // Reporting of error messages, warnings and other info.
69+
   ├── rewrites // Helpers for rewriting Scala 2's constructs into Scala 3's.
70+
   ├── sbt // Helpers for communicating with the Zinc compiler.
71+
   ├── semanticdb // Helpers for exporting semanticdb from trees.
72+
   ├── transform // Miniphases and helpers for tree transformations.
73+
   ├── typer // Type-checking
74+
   └── util // General purpose utility classes and modules.
75+
├── io // Helper modules for file access and classpath handling.
76+
├── repl // REPL driver and interaction with the terminal
77+
├── runner // helpers for the `scala` generic runner script
78+
└── scripting // scala runner for the -script argument
79+
```
80+
81+
[1]: {% link _overviews/scala3-contribution/contribution-intro.md %}/#what-is-a-compiler
82+
[2]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Run.scala
83+
[3]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/core/Phases.scala
84+
[4]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Compiler.scala
85+
[5]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/CompilationUnit.scala
86+
[6]: {% link _overviews/scala3-contribution/arch-time.md %}
87+
[7]: https://github.com/lampepfl/dotty/tree/master/compiler/src/dotty/tools
88+
[8]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
89+
[9]: {% link _overviews/scala3-contribution/arch-phases.md %}
90+
[10]: {% link _scala3-reference/syntax.md %}
91+
[Main]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Main.scala
92+
[Driver]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Driver.scala
93+
[Compiler]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Compiler.scala
94+
[Run]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Run.scala
95+
[Context]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/core/Contexts.scala
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: Compiler Phases
3+
type: section
4+
description: This page describes the phases for the Scala 3 compiler.
5+
num: 14
6+
previous-page: arch-lifecycle
7+
next-page: arch-types
8+
---
9+
10+
As described in the [compiler overview][1], `dotc` is divided into a list of [phases][Phase],
11+
specified in the [Compiler] class.
12+
13+
#### Printing the phases of the Compiler
14+
15+
a flattened list of all the phases can be displayed by invoking
16+
the compiler with the `-Xshow-phases` flag:
17+
```
18+
$ scalac -Xshow-phases
19+
```
20+
21+
## Phase Groups
22+
23+
In class [Compiler] we can access the list of phases with the method `phases`:
24+
25+
```scala
26+
def phases: List[List[Phase]] =
27+
frontendPhases ::: picklerPhases ::: transformPhases ::: backendPhases
28+
```
29+
30+
We see that phases are actually grouped into sublists, given by the signature
31+
`List[List[Phase]]`; that is, each sublist forms a phase group that is then *fused* into a
32+
single tree traversal when a [Run] is executed.
33+
34+
Phase fusion allows each phase of a group to be small and modular,
35+
(each performing a single function), while reducing the number of tree traversals
36+
and increasing performance.
37+
38+
Phases are able to be grouped together if they inherit from [MiniPhase].
39+
40+
## Phase Categories
41+
42+
Phases fall into four categories, allowing customisation by sub-classes of [Compiler]:
43+
44+
### `frontendPhases`
45+
In the main compiler these include [parser], [typer], [posttyper],
46+
[prepjsinterop] and phases for producing SemanticDB and communicating with the
47+
incremental compiler Zinc.
48+
The [parser] reads source programs and generates untyped abstract syntax trees, which
49+
in [typer] are then typechecked and transformed into typed abstract syntax trees.
50+
Following is [posttyper], performing checks and cleanups that require a fully typed program.
51+
In particular, it
52+
- creates super accessors representing `super` calls in traits
53+
- creates implementations of compiler-implemented methods,
54+
such as `equals` and `hashCode` for case classes.
55+
- marks [compilation units][2] that require inline expansion, or quote pickling
56+
- simplifies trees of erased definitions
57+
- checks variance of type parameters
58+
- mark parameters passed unchanged from subclass to superclass for later pruning.
59+
60+
### `picklerPhases`
61+
These phases start with [pickler], which serializes typed trees
62+
produced by the `frontendPhases` into TASTy format. Following is [inlining],
63+
which expand calls to inline methods, and [postInlining] providing implementations
64+
of the Mirror framework for inlined calls.
65+
Finally are [staging], which ensures that quotes conform to the
66+
Phase Consistency Principle (PCP), and [pickleQuotes] which converts quoted
67+
trees to embedded TASTy strings.
68+
69+
### `transformPhases`
70+
These phases are concerned with tranformation into lower-level forms
71+
suitable for the runtime system, with two sub-groupings:
72+
- High-level transformations: All phases from [firstTransform] to [erasure].
73+
Most of these phases transform syntax trees, expanding high-level constructs
74+
to more primitive ones.
75+
- Some phases perform further checks on more primitive trees,
76+
e.g. [refchecks] verifies that no abstract methods exist in concrete classes,
77+
and [initChecker] checks that fields are not used before initialisation.
78+
- The last phase in the group, [erasure] translates all
79+
types into types supported directly by the JVM. To do this, it performs
80+
another type checking pass, but using the rules of the JVM's type system
81+
instead of Scala's.
82+
- Low-level transformations: All phases from `ElimErasedValueType` to
83+
`CollectSuperCalls`. These further transform trees until they are essentially a
84+
structured version of Java bytecode.
85+
86+
### `backendPhases`
87+
These map the transformed trees to Java classfiles or SJSIR files.
88+
89+
[1]: {% link _overviews/scala3-contribution/arch-lifecycle.md %}/#phases
90+
[2]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/CompilationUnit.scala
91+
[Compiler]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Compiler.scala
92+
[Phase]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/core/Phases.scala
93+
[MiniPhase]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala
94+
[Run]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Run.scala
95+
[parser]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/parsing/ParserPhase.scala
96+
[typer]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/typer/TyperPhase.scala
97+
[posttyper]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/PostTyper.scala
98+
[prepjsinterop]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala
99+
[pickler]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/Pickler.scala
100+
[inlining]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/Inlining.scala
101+
[postInlining]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/PostInlining.scala
102+
[staging]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/Staging.scala
103+
[pickleQuotes]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/PickleQuotes.scala
104+
[refchecks]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/typer/RefChecks.scala
105+
[initChecker]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/init/Checker.scala
106+
[firstTransform]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala
107+
[erasure]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/Erasure.scala

0 commit comments

Comments
 (0)