-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Document Scala compiler error codes #24734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WojciechMazur
wants to merge
14
commits into
scala:main
Choose a base branch
from
WojciechMazur:docs/error-codes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1b3d6bc
Add error codes E001-E100 (excluding inactive)
WojciechMazur cc89c51
Add scripts to validate, update and test error code snippets
WojciechMazur 1b7a3e6
Fix existing and document remaining error codes
WojciechMazur 3521254
Mark inactive error codes found when documenting, remove dead code fr…
WojciechMazur 7dc49e4
Add GHA job enusre error codes are up to date and snippets fail with …
WojciechMazur 61b838c
Fix building code snippets in markdown files when using `scaladoc/gen…
WojciechMazur 683dbff
Setup error-codes/index
WojciechMazur 799aa84
Fix typo in scaladoc.yaml workflow
WojciechMazur a85ced0
scaladoc.yaml: Ensure to install sbt
WojciechMazur 0040b6e
scaladoc.yaml: Fix scala-cli-setup
WojciechMazur e3f3f86
scaladoc.yaml: publish all bootstrapped artifacts, scala-cli requires…
WojciechMazur 95e178b
Apply reviewers suggests, fix typos, add comments, adjust examples an…
WojciechMazur 0c285bd
Revert spurious changes
WojciechMazur 130332d
Add additional validation to ensure all error code are listed in side…
WojciechMazur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| --- | ||
| 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 sc-opts:-explain | ||
| def example() = | ||
| try println("hello") | ||
| catch { } | ||
| ``` | ||
|
|
||
| ### Error | ||
|
|
||
| ```scala sc:nocompile | ||
| -- [E001] Syntax Error: example.scala:3:2 -------------------------------------- | ||
| 3 | catch { } | ||
| | ^^^^^^^^^ | ||
| | The catch block does not contain a valid expression, try | ||
| | adding a case like - case e: Exception => to the block | ||
| |----------------------------------------------------------------------------- | ||
| | Explanation (enabled by `-explain`) | ||
| |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| | 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: | ||
| | | ||
| | import scala.util.control.NonFatal | ||
| | | ||
| | try println("hello") catch { | ||
| | case NonFatal(e) => ??? | ||
| | } | ||
| | | ||
| | 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: | ||
| | | ||
| | 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. | ||
| ----------------------------------------------------------------------------- | ||
| ``` | ||
|
|
||
| ### Solution | ||
|
|
||
| ```scala sc:compile | ||
| // Remove redundant 'try' block | ||
| def example() = | ||
| println("hello") | ||
| ``` | ||
|
|
||
| ```scala sc:compile | ||
| // Alternative: Add a case handler to catch exceptions | ||
| import scala.util.control.NonFatal | ||
|
|
||
| def example() = | ||
| try println("hello") | ||
| catch { case NonFatal(e) => println(s"Caught: $e") } | ||
| ``` | ||
|
|
||
| ```scala sc:compile | ||
| // Alternative: use finally instead if you only need cleanup | ||
| def example() = | ||
| try println("hello") | ||
| finally println("cleanup") | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| --- | ||
| 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:-explain,-Werror | ||
| @main def example() = | ||
| try println("hello") | ||
| ``` | ||
|
|
||
| ### Warning | ||
|
|
||
| ```scala sc:nocompile | ||
| -- [E002] Syntax Warning: example.scala:2:2 ------------------------------------ | ||
| 2 | try println("hello") | ||
| | ^^^^^^^^^^^^^^^^^^^^ | ||
| | A try without catch or finally is equivalent to putting | ||
| | its body in a block; no exceptions are handled. | ||
| |----------------------------------------------------------------------------- | ||
| | Explanation (enabled by `-explain`) | ||
| |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| | 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: | ||
| | | ||
| | import scala.util.control.NonFatal | ||
| | | ||
| | try println("hello") catch { | ||
| | case NonFatal(e) => ??? | ||
| | } | ||
| | | ||
| | 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: | ||
| | | ||
| | 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. | ||
| ----------------------------------------------------------------------------- | ||
| ``` | ||
|
|
||
| ### Solution | ||
|
|
||
| ```scala sc:compile sc-opts:-Werror | ||
| // Remove redundant 'try' block | ||
| def example() = | ||
| println("hello") | ||
| ``` | ||
|
|
||
| ```scala sc:compile sc-opts:-Werror | ||
| // Alternative: Add a catch block to handle exceptions | ||
| import scala.util.control.NonFatal | ||
|
|
||
| def example() = | ||
| try | ||
| println("hello") | ||
| catch | ||
| case NonFatal(e) => println(s"Caught: $e") | ||
| ``` | ||
|
|
||
| ```scala sc:compile sc-opts:-Werror | ||
| // Alternative: Add a finally block for cleanup | ||
| def example() = | ||
| try | ||
| println("hello") | ||
| finally | ||
| println("cleanup") | ||
| ``` | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might need an example? What is transfer function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea, example and description was generated based on
-explaindescription from compiler.So in fact here we have a duplication, the same example in explain and in description (altough outputs are allowed to change any moment)