Skip to content

Commit 2bab1ad

Browse files
committed
Fix existing and document remaining error codes
1 parent 7e288f3 commit 2bab1ad

File tree

223 files changed

+9257
-1224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+9257
-1224
lines changed

docs/_docs/reference/error-codes/E001.md

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: E001: Empty Catch Block
33
kind: Error
44
---
5-
65
# E001: Empty Catch Block
76

87
This error is emitted when a `try` expression has a `catch` block that does not contain any case handlers.
@@ -37,8 +36,8 @@ correctly handles transfer functions like `return`.
3736

3837
## Example
3938

40-
```scala sc:fail
41-
@main def test() =
39+
```scala sc:fail sc-opts:-explain
40+
def example() =
4241
try {
4342
println("hello")
4443
} catch { }
@@ -47,37 +46,61 @@ correctly handles transfer functions like `return`.
4746
### Error
4847

4948
```scala sc:nocompile
50-
-- [E001] Syntax Error: example.scala:4:4
49+
-- [E001] Syntax Error: example.scala:4:4 --------------------------------------
5150
4 | } catch { }
5251
| ^^^^^^^^^
5352
| The catch block does not contain a valid expression, try
5453
| adding a case like - case e: Exception => to the block
54+
|-----------------------------------------------------------------------------
55+
| Explanation (enabled by `-explain`)
56+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57+
| A try expression should be followed by some mechanism to handle any exceptions
58+
| thrown. Typically a catch expression follows the try and pattern matches
59+
| on any expected exceptions. For example:
60+
|
61+
| import scala.util.control.NonFatal
62+
|
63+
| try {
64+
| println("hello")
65+
| } catch {
66+
| case NonFatal(e) => ???
67+
| }
68+
|
69+
| It is also possible to follow a try immediately by a finally - letting the
70+
| exception propagate - but still allowing for some clean up in finally:
71+
|
72+
| try {
73+
| println("hello")
74+
| } finally {
75+
| // perform your cleanup here!
76+
| }
77+
|
78+
| It is recommended to use the NonFatal extractor to catch all exceptions as it
79+
| correctly handles transfer functions like return.
80+
-----------------------------------------------------------------------------
5581
```
5682

57-
### Solution
83+
### Solution
5884

59-
```scala sc:compile
85+
```scala sc:compile
6086
// Remove redundant 'try' block
61-
println("hello")
62-
```
87+
def example() =
88+
println("hello")
89+
```
6390

64-
```scala sc:compile
91+
```scala sc:compile
6592
// Alternative: Add a case handler to catch exceptions
6693
import scala.util.control.NonFatal
6794

68-
try {
69-
println("hello")
70-
} catch {
71-
case NonFatal(e) => println(s"Caught: $e")
72-
}
73-
```
95+
def example() =
96+
try println("hello")
97+
catch { case NonFatal(e) => println(s"Caught: $e") }
98+
```
7499

75100
```scala sc:compile
76101
// Alternative: use finally instead if you only need cleanup
77-
try {
78-
println("hello")
79-
} finally {
80-
println("cleanup")
81-
}
102+
def example() =
103+
try println("hello")
104+
finally println("cleanup")
82105
```
83106

docs/_docs/reference/error-codes/E002.md

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: E002: Empty Catch And Finally Block
33
kind: Warning
44
---
5-
65
# E002: Empty Catch And Finally Block
76

87
This warning is emitted when a `try` expression has neither a `catch` block nor a `finally` block. Such a `try` is redundant since no exceptions are handled.
@@ -37,8 +36,8 @@ correctly handles transfer functions like `return`.
3736

3837
## Example
3938

40-
```scala sc:fail sc-opts:-Werror
41-
@main def test() =
39+
```scala sc:fail sc-opts:-explain,-Werror
40+
@main def example() =
4241
try {
4342
println("hello")
4443
}
@@ -47,39 +46,67 @@ correctly handles transfer functions like `return`.
4746
### Warning
4847

4948
```scala sc:nocompile
50-
-- [E002] Syntax Warning: example.scala:2:2
49+
-- [E002] Syntax Warning: example.scala:2:2 ------------------------------------
5150
2 | try {
5251
| ^
5352
| A try without catch or finally is equivalent to putting
5453
| its body in a block; no exceptions are handled.
5554
3 | println("hello")
5655
4 | }
56+
|-----------------------------------------------------------------------------
57+
| Explanation (enabled by `-explain`)
58+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
59+
| A try expression should be followed by some mechanism to handle any exceptions
60+
| thrown. Typically a catch expression follows the try and pattern matches
61+
| on any expected exceptions. For example:
62+
|
63+
| import scala.util.control.NonFatal
64+
|
65+
| try {
66+
| println("hello")
67+
| } catch {
68+
| case NonFatal(e) => ???
69+
| }
70+
|
71+
| It is also possible to follow a try immediately by a finally - letting the
72+
| exception propagate - but still allowing for some clean up in finally:
73+
|
74+
| try {
75+
| println("hello")
76+
| } finally {
77+
| // perform your cleanup here!
78+
| }
79+
|
80+
| It is recommended to use the NonFatal extractor to catch all exceptions as it
81+
| correctly handles transfer functions like return.
82+
-----------------------------------------------------------------------------
5783
```
5884

59-
### Solution
85+
### Solution
6086

6187
```scala sc:compile sc-opts:-Werror
6288
// Remove redundant 'try' block
63-
println("hello")
64-
```
89+
def example() =
90+
println("hello")
91+
```
6592

6693
```scala sc:compile sc-opts:-Werror
6794
// Alternative: Add a catch block to handle exceptions
6895
import scala.util.control.NonFatal
6996

70-
try {
71-
println("hello")
72-
} catch {
73-
case NonFatal(e) => println(s"Caught: $e")
74-
}
75-
```
97+
def example() =
98+
try
99+
println("hello")
100+
catch
101+
case NonFatal(e) => println(s"Caught: $e")
102+
```
76103

77104
```scala sc:compile sc-opts:-Werror
78105
// Alternative: Add a finally block for cleanup
79-
try {
80-
println("hello")
81-
} finally {
82-
println("cleanup")
83-
}
106+
def example() =
107+
try
108+
println("hello")
109+
finally
110+
println("cleanup")
84111
```
85112

docs/_docs/reference/error-codes/E003.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: E003: Deprecated With Operator
33
kind: Warning
44
---
5-
65
# E003: Deprecated With Operator
76

87
This warning is emitted when using `with` as a type operator to create compound types. In Scala 3, `with` has been deprecated in favor of intersection types using `&`.
@@ -15,7 +14,7 @@ semantics between intersection types and using `with`.
1514

1615
## Example
1716

18-
```scala sc:fail sc-opts:-Werror
17+
```scala sc:fail sc-opts:-explain,-Werror
1918
trait A
2019
trait B
2120
def test(x: A with B): Unit = ()
@@ -24,21 +23,28 @@ def test(x: A with B): Unit = ()
2423
### Warning
2524

2625
```scala sc:nocompile
27-
-- [E003] Syntax Warning: example.scala:3:14
26+
-- [E003] Syntax Warning: example.scala:3:14 -----------------------------------
2827
3 |def test(x: A with B): Unit = ()
2928
| ^^^^
3029
|with as a type operator has been deprecated; use & instead
3130
|This construct can be rewritten automatically under -rewrite -source 3.4-migration.
31+
|-----------------------------------------------------------------------------
32+
| Explanation (enabled by `-explain`)
33+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34+
| Dotty introduces intersection types - & types. These replace the
35+
| use of the with keyword. There are a few differences in
36+
| semantics between intersection types and using with.
37+
-----------------------------------------------------------------------------
3238
```
3339

34-
### Solution
40+
### Solution
3541

3642
```scala sc:compile sc-opts:-Werror
3743
// Use intersection type operator & instead of with
3844
trait A
3945
trait B
4046
def test(x: A & B): Unit = ()
41-
```
47+
```
4248

4349
```scala sc:compile sc-opts:-Werror
4450
// The change also applies to type aliases and class definitions

docs/_docs/reference/error-codes/E004.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: E004: Case Class Missing Param List
33
kind: Error
44
---
5-
65
# E004: Case Class Missing Param List
76

87
This error is emitted when a `case class` is defined without any parameter list. In Scala 3, case classes must have at least one parameter list.
@@ -15,32 +14,39 @@ Or, add an explicit `()` as a parameter list to `Empty`.
1514

1615
## Example
1716

18-
```scala sc:fail
17+
```scala sc:fail sc-opts:-explain
1918
case class Empty
2019
```
2120

2221
### Error
2322

2423
```scala sc:nocompile
25-
-- [E004] Syntax Error: example.scala:1:11
24+
-- [E004] Syntax Error: example.scala:1:11 -------------------------------------
2625
1 |case class Empty
2726
| ^^^^^
2827
| A case class must have at least one parameter list
28+
|-----------------------------------------------------------------------------
29+
| Explanation (enabled by `-explain`)
30+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31+
| Empty must have at least one parameter list, if you would rather
32+
| have a singleton representation of Empty, use a "case object".
33+
| Or, add an explicit () as a parameter list to Empty.
34+
-----------------------------------------------------------------------------
2935
```
3036

31-
### Solution
37+
### Solution
3238

33-
```scala sc:compile
39+
```scala sc:compile
3440
// Use case object for singleton representation
3541
case object Empty
36-
```
42+
```
3743

38-
```scala sc:compile
44+
```scala sc:compile
3945
// Add an explicit empty parameter list
4046
case class Empty()
41-
```
47+
```
4248

43-
```scala sc:compile
49+
```scala sc:compile
4450
// Or define actual parameters
4551
case class Empty(value: String)
4652
```

docs/_docs/reference/error-codes/E005.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: E005: Duplicate Bind
33
kind: Error
44
---
5-
65
# E005: Duplicate Bind
76

87
This error is emitted when the same variable name is used more than once in a pattern match case. Each bound variable in a `case` pattern must have a unique name.
@@ -19,35 +18,46 @@ case (a, a) => a
1918

2019
## Example
2120

22-
```scala sc:fail
21+
```scala sc:fail sc-opts:-explain
2322
def test(x: Any) = x match
2423
case (a, a) => a
2524
```
2625

2726
### Error
2827

2928
```scala sc:nocompile
30-
-- [E005] Naming Error: example.scala:2:11
29+
-- [E005] Naming Error: example.scala:2:11 -------------------------------------
3130
2 | case (a, a) => a
3231
| ^
3332
| duplicate pattern variable: a
33+
|-----------------------------------------------------------------------------
34+
| Explanation (enabled by `-explain`)
35+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36+
| For each case bound variable names have to be unique. In:
37+
|
38+
| case (a, a) => {
39+
| a
40+
| }
41+
|
42+
| a is not unique. Rename one of the bound variables!
43+
-----------------------------------------------------------------------------
3444
```
3545

36-
### Solution
46+
### Solution
3747

38-
```scala sc:compile
48+
```scala sc:compile
3949
// Use unique names for each bound variable
4050
def test(x: Any) = x match
4151
case (a, b) => (a, b)
42-
```
52+
```
4353

44-
```scala sc:compile
54+
```scala sc:compile
4555
// Use wildcard _ if you don't need the value
4656
def test(x: Any) = x match
4757
case (a, _) => a
48-
```
58+
```
4959

50-
```scala sc:compile
60+
```scala sc:compile
5161
// Use a guard if you want to match equal values
5262
def test(x: Any) = x match
5363
case (a, b) if a == b => a

0 commit comments

Comments
 (0)