Skip to content

Commit f310b11

Browse files
committed
Add error codes E001-E100 (excluding inactive)
1 parent 73b9729 commit f310b11

Some content is hidden

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

95 files changed

+4945
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: E001: Empty Catch Block
3+
kind: Error
4+
---
5+
6+
# E001: Empty Catch Block
7+
8+
This error is emitted when a `try` expression has a `catch` block that does not contain any case handlers.
9+
10+
A `try` expression should be followed by some mechanism to handle any exceptions
11+
thrown. Typically a `catch` expression follows the `try` and pattern matches
12+
on any expected exceptions. For example:
13+
14+
```scala
15+
try {
16+
println("hello")
17+
} catch {
18+
case e: Exception => ???
19+
}
20+
```
21+
22+
It is also possible to follow a `try` immediately by a `finally` - letting the
23+
exception propagate - but still allowing for some clean up in `finally`:
24+
25+
```scala
26+
try {
27+
println("hello")
28+
} finally {
29+
// perform your cleanup here!
30+
}
31+
```
32+
33+
It is recommended to use the `NonFatal` extractor to catch all exceptions as it
34+
correctly handles transfer functions like `return`.
35+
36+
---
37+
38+
## Example
39+
40+
```scala sc:fail
41+
@main def test() =
42+
try {
43+
println("hello")
44+
} catch { }
45+
```
46+
47+
### Error
48+
49+
```scala sc:nocompile
50+
-- [E001] Syntax Error: example.scala:4:4
51+
4 | } catch { }
52+
| ^^^^^^^^^
53+
| The catch block does not contain a valid expression, try
54+
| adding a case like - case e: Exception => to the block
55+
```
56+
57+
### Solution
58+
59+
```scala sc:compile
60+
// Remove redundant 'try' block
61+
println("hello")
62+
```
63+
64+
```scala sc:compile
65+
// Alternative: Add a case handler to catch exceptions
66+
import scala.util.control.NonFatal
67+
68+
try {
69+
println("hello")
70+
} catch {
71+
case NonFatal(e) => println(s"Caught: $e")
72+
}
73+
```
74+
75+
```scala sc:compile
76+
// Alternative: use finally instead if you only need cleanup
77+
try {
78+
println("hello")
79+
} finally {
80+
println("cleanup")
81+
}
82+
```
83+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: E002: Empty Catch And Finally Block
3+
kind: Warning
4+
---
5+
6+
# E002: Empty Catch And Finally Block
7+
8+
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.
9+
10+
A `try` expression should be followed by some mechanism to handle any exceptions
11+
thrown. Typically a `catch` expression follows the `try` and pattern matches
12+
on any expected exceptions. For example:
13+
14+
```scala
15+
try {
16+
println("hello")
17+
} catch {
18+
case e: Exception => ???
19+
}
20+
```
21+
22+
It is also possible to follow a `try` immediately by a `finally` - letting the
23+
exception propagate - but still allowing for some clean up in `finally`:
24+
25+
```scala
26+
try {
27+
println("hello")
28+
} finally {
29+
// perform your cleanup here!
30+
}
31+
```
32+
33+
It is recommended to use the `NonFatal` extractor to catch all exceptions as it
34+
correctly handles transfer functions like `return`.
35+
36+
---
37+
38+
## Example
39+
40+
```scala sc:fail sc-opts:-Werror
41+
@main def test() =
42+
try {
43+
println("hello")
44+
}
45+
```
46+
47+
### Warning
48+
49+
```scala sc:nocompile
50+
-- [E002] Syntax Warning: example.scala:2:2
51+
2 | try {
52+
| ^
53+
| A try without catch or finally is equivalent to putting
54+
| its body in a block; no exceptions are handled.
55+
3 | println("hello")
56+
4 | }
57+
```
58+
59+
### Solution
60+
61+
```scala sc:compile sc-opts:-Werror
62+
// Remove redundant 'try' block
63+
println("hello")
64+
```
65+
66+
```scala sc:compile sc-opts:-Werror
67+
// Alternative: Add a catch block to handle exceptions
68+
import scala.util.control.NonFatal
69+
70+
try {
71+
println("hello")
72+
} catch {
73+
case NonFatal(e) => println(s"Caught: $e")
74+
}
75+
```
76+
77+
```scala sc:compile sc-opts:-Werror
78+
// Alternative: Add a finally block for cleanup
79+
try {
80+
println("hello")
81+
} finally {
82+
println("cleanup")
83+
}
84+
```
85+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: E003: Deprecated With Operator
3+
kind: Warning
4+
---
5+
6+
# E003: Deprecated With Operator
7+
8+
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 `&`.
9+
10+
Dotty introduces intersection types - `&` types. These replace the
11+
use of the `with` keyword. There are a few differences in
12+
semantics between intersection types and using `with`.
13+
14+
---
15+
16+
## Example
17+
18+
```scala sc:fail sc-opts:-Werror
19+
trait A
20+
trait B
21+
def test(x: A with B): Unit = ()
22+
```
23+
24+
### Warning
25+
26+
```scala sc:nocompile
27+
-- [E003] Syntax Warning: example.scala:3:14
28+
3 |def test(x: A with B): Unit = ()
29+
| ^^^^
30+
|with as a type operator has been deprecated; use & instead
31+
|This construct can be rewritten automatically under -rewrite -source 3.4-migration.
32+
```
33+
34+
### Solution
35+
36+
```scala sc:compile sc-opts:-Werror
37+
// Use intersection type operator & instead of with
38+
trait A
39+
trait B
40+
def test(x: A & B): Unit = ()
41+
```
42+
43+
```scala sc:compile sc-opts:-Werror
44+
// The change also applies to type aliases and class definitions
45+
trait Readable
46+
trait Writable
47+
48+
type ReadWrite = Readable & Writable
49+
50+
class File extends Readable, Writable
51+
```
52+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: E004: Case Class Missing Param List
3+
kind: Error
4+
---
5+
6+
# E004: Case Class Missing Param List
7+
8+
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.
9+
10+
`Empty` must have at least one parameter list. If you would rather
11+
have a singleton representation of `Empty`, use a `case object`.
12+
Or, add an explicit `()` as a parameter list to `Empty`.
13+
14+
---
15+
16+
## Example
17+
18+
```scala sc:fail
19+
case class Empty
20+
```
21+
22+
### Error
23+
24+
```scala sc:nocompile
25+
-- [E004] Syntax Error: example.scala:1:11
26+
1 |case class Empty
27+
| ^^^^^
28+
| A case class must have at least one parameter list
29+
```
30+
31+
### Solution
32+
33+
```scala sc:compile
34+
// Use case object for singleton representation
35+
case object Empty
36+
```
37+
38+
```scala sc:compile
39+
// Add an explicit empty parameter list
40+
case class Empty()
41+
```
42+
43+
```scala sc:compile
44+
// Or define actual parameters
45+
case class Empty(value: String)
46+
```
47+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: E005: Duplicate Bind
3+
kind: Error
4+
---
5+
6+
# E005: Duplicate Bind
7+
8+
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.
9+
10+
For each `case` bound variable names have to be unique. In:
11+
12+
```scala
13+
case (a, a) => a
14+
```
15+
16+
`a` is not unique. Rename one of the bound variables!
17+
18+
---
19+
20+
## Example
21+
22+
```scala sc:fail
23+
def test(x: Any) = x match
24+
case (a, a) => a
25+
```
26+
27+
### Error
28+
29+
```scala sc:nocompile
30+
-- [E005] Naming Error: example.scala:2:11
31+
2 | case (a, a) => a
32+
| ^
33+
| duplicate pattern variable: a
34+
```
35+
36+
### Solution
37+
38+
```scala sc:compile
39+
// Use unique names for each bound variable
40+
def test(x: Any) = x match
41+
case (a, b) => (a, b)
42+
```
43+
44+
```scala sc:compile
45+
// Use wildcard _ if you don't need the value
46+
def test(x: Any) = x match
47+
case (a, _) => a
48+
```
49+
50+
```scala sc:compile
51+
// Use a guard if you want to match equal values
52+
def test(x: Any) = x match
53+
case (a, b) if a == b => a
54+
```
55+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: E006: Missing Ident
3+
kind: Error
4+
---
5+
6+
# E006: Missing Ident
7+
8+
This error is emitted when a referenced identifier (value, method, type, etc.) cannot be found in the current scope.
9+
10+
Each identifier in Scala needs a matching declaration. There are two kinds of
11+
identifiers: type identifiers and value identifiers. Value identifiers are introduced
12+
by `val`, `def`, or `object` declarations. Type identifiers are introduced by `type`,
13+
`class`, `enum`, or `trait` declarations.
14+
15+
Identifiers refer to matching declarations in their environment, or they can be
16+
imported from elsewhere.
17+
18+
Possible reasons why no matching declaration was found:
19+
* The declaration or the use is mis-spelt.
20+
* An import is missing.
21+
22+
---
23+
24+
## Example
25+
26+
```scala sc:fail
27+
val result = unknownIdentifier
28+
```
29+
30+
### Error
31+
32+
```scala sc:nocompile
33+
-- [E006] Not Found Error: example.scala:1:13
34+
1 |val result = unknownIdentifier
35+
| ^^^^^^^^^^^^^^^^^
36+
| Not found: unknownIdentifier
37+
```
38+
39+
### Solution
40+
41+
```scala sc:compile
42+
// Declare the identifier before using it
43+
val unknownIdentifier = 42
44+
val result = unknownIdentifier
45+
```
46+
47+
```scala sc:compile
48+
// Or import it from another scope
49+
import scala.math.Pi
50+
val result = Pi
51+
```
52+
53+
```scala sc:compile
54+
// Fix the spelling if it was a typo
55+
val knownIdentifier = 42
56+
val result = knownIdentifier
57+
```
58+

0 commit comments

Comments
 (0)