Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions docs/_docs/reference/error-codes/E001.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
title: E001: Empty Catch Block
kind: Error
---

# E001: Empty Catch Block

This error is emitted when a `try` expression has a `catch` block that does not contain any case handlers.

A `try` expression should be followed by some mechanism to handle any exceptions
thrown. Typically a `catch` expression follows the `try` and pattern matches
on any expected exceptions. For example:

```scala
try {
println("hello")
} catch {
case e: Exception => ???
}
```

It is also possible to follow a `try` immediately by a `finally` - letting the
exception propagate - but still allowing for some clean up in `finally`:

```scala
try {
println("hello")
} finally {
// perform your cleanup here!
}
```

It is recommended to use the `NonFatal` extractor to catch all exceptions as it
correctly handles transfer functions like `return`.

---

## Example

```scala sc:fail
@main def test() =
try {
println("hello")
} catch { }
```

### Error

```scala sc:nocompile
-- [E001] Syntax Error: example.scala:4:4
4 | } catch { }
| ^^^^^^^^^
| The catch block does not contain a valid expression, try
| adding a case like - case e: Exception => to the block
```

### Solution

```scala sc:compile
// Remove redundant 'try' block
println("hello")
```

```scala sc:compile
// Alternative: Add a case handler to catch exceptions
import scala.util.control.NonFatal

try {
println("hello")
} catch {
case NonFatal(e) => println(s"Caught: $e")
}
```

```scala sc:compile
// Alternative: use finally instead if you only need cleanup
try {
println("hello")
} finally {
println("cleanup")
}
```

85 changes: 85 additions & 0 deletions docs/_docs/reference/error-codes/E002.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
title: E002: Empty Catch And Finally Block
kind: Warning
---

# E002: Empty Catch And Finally Block

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.

A `try` expression should be followed by some mechanism to handle any exceptions
thrown. Typically a `catch` expression follows the `try` and pattern matches
on any expected exceptions. For example:

```scala
try {
println("hello")
} catch {
case e: Exception => ???
}
```

It is also possible to follow a `try` immediately by a `finally` - letting the
exception propagate - but still allowing for some clean up in `finally`:

```scala
try {
println("hello")
} finally {
// perform your cleanup here!
}
```

It is recommended to use the `NonFatal` extractor to catch all exceptions as it
correctly handles transfer functions like `return`.

---

## Example

```scala sc:fail sc-opts:-Werror
@main def test() =
try {
println("hello")
}
```

### Warning

```scala sc:nocompile
-- [E002] Syntax Warning: example.scala:2:2
2 | try {
| ^
| A try without catch or finally is equivalent to putting
| its body in a block; no exceptions are handled.
3 | println("hello")
4 | }
```

### Solution

```scala sc:compile sc-opts:-Werror
// Remove redundant 'try' block
println("hello")
```

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

try {
println("hello")
} catch {
case NonFatal(e) => println(s"Caught: $e")
}
```

```scala sc:compile sc-opts:-Werror
// Alternative: Add a finally block for cleanup
try {
println("hello")
} finally {
println("cleanup")
}
```

52 changes: 52 additions & 0 deletions docs/_docs/reference/error-codes/E003.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: E003: Deprecated With Operator
kind: Warning
---

# E003: Deprecated With Operator

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 `&`.

Dotty introduces intersection types - `&` types. These replace the
use of the `with` keyword. There are a few differences in
semantics between intersection types and using `with`.

---

## Example

```scala sc:fail sc-opts:-Werror
trait A
trait B
def test(x: A with B): Unit = ()
```

### Warning

```scala sc:nocompile
-- [E003] Syntax Warning: example.scala:3:14
3 |def test(x: A with B): Unit = ()
| ^^^^
|with as a type operator has been deprecated; use & instead
|This construct can be rewritten automatically under -rewrite -source 3.4-migration.
```

### Solution

```scala sc:compile sc-opts:-Werror
// Use intersection type operator & instead of with
trait A
trait B
def test(x: A & B): Unit = ()
```

```scala sc:compile sc-opts:-Werror
// The change also applies to type aliases and class definitions
trait Readable
trait Writable

type ReadWrite = Readable & Writable

class File extends Readable, Writable
```

47 changes: 47 additions & 0 deletions docs/_docs/reference/error-codes/E004.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: E004: Case Class Missing Param List
kind: Error
---

# E004: Case Class Missing Param List

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.

`Empty` must have at least one parameter list. If you would rather
have a singleton representation of `Empty`, use a `case object`.
Or, add an explicit `()` as a parameter list to `Empty`.

---

## Example

```scala sc:fail
case class Empty
```

### Error

```scala sc:nocompile
-- [E004] Syntax Error: example.scala:1:11
1 |case class Empty
| ^^^^^
| A case class must have at least one parameter list
```

### Solution

```scala sc:compile
// Use case object for singleton representation
case object Empty
```

```scala sc:compile
// Add an explicit empty parameter list
case class Empty()
```

```scala sc:compile
// Or define actual parameters
case class Empty(value: String)
```

55 changes: 55 additions & 0 deletions docs/_docs/reference/error-codes/E005.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: E005: Duplicate Bind
kind: Error
---

# E005: Duplicate Bind

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.

For each `case` bound variable names have to be unique. In:

```scala
case (a, a) => a
```

`a` is not unique. Rename one of the bound variables!

---

## Example

```scala sc:fail
def test(x: Any) = x match
case (a, a) => a
```

### Error

```scala sc:nocompile
-- [E005] Naming Error: example.scala:2:11
2 | case (a, a) => a
| ^
| duplicate pattern variable: a
```

### Solution

```scala sc:compile
// Use unique names for each bound variable
def test(x: Any) = x match
case (a, b) => (a, b)
```

```scala sc:compile
// Use wildcard _ if you don't need the value
def test(x: Any) = x match
case (a, _) => a
```

```scala sc:compile
// Use a guard if you want to match equal values
def test(x: Any) = x match
case (a, b) if a == b => a
```

58 changes: 58 additions & 0 deletions docs/_docs/reference/error-codes/E006.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: E006: Missing Ident
kind: Error
---

# E006: Missing Ident

This error is emitted when a referenced identifier (value, method, type, etc.) cannot be found in the current scope.

Each identifier in Scala needs a matching declaration. There are two kinds of
identifiers: type identifiers and value identifiers. Value identifiers are introduced
by `val`, `def`, or `object` declarations. Type identifiers are introduced by `type`,
`class`, `enum`, or `trait` declarations.

Identifiers refer to matching declarations in their environment, or they can be
imported from elsewhere.

Possible reasons why no matching declaration was found:
* The declaration or the use is mis-spelt.
* An import is missing.

---

## Example

```scala sc:fail
val result = unknownIdentifier
```

### Error

```scala sc:nocompile
-- [E006] Not Found Error: example.scala:1:13
1 |val result = unknownIdentifier
| ^^^^^^^^^^^^^^^^^
| Not found: unknownIdentifier
```

### Solution

```scala sc:compile
// Declare the identifier before using it
val unknownIdentifier = 42
val result = unknownIdentifier
```

```scala sc:compile
// Or import it from another scope
import scala.math.Pi
val result = Pi
```

```scala sc:compile
// Fix the spelling if it was a typo
val knownIdentifier = 42
val result = knownIdentifier
```

Loading
Loading