-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better error message when a pattern match extractor is not found. (#1…
- Loading branch information
Showing
13 changed files
with
169 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,82 @@ | ||
-- [E189] Not Found Error: tests/neg/i18684.scala:3:6 ------------------------------------------------------------------ | ||
3 | val s(): String = "hello, world" // error | ||
| ^ | ||
| no pattern match extractor named s was found | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| An application s(...) in a pattern can refer to an extractor | ||
| which defines an unapply or unapplySeq method. Example: | ||
| | ||
| object split: | ||
| def unapply(x: String) = | ||
| val (leading, trailing) = x.splitAt(x.length / 2) | ||
| Some((leading, trailing)) | ||
| | ||
| val split(fst, snd) = "HiHo" | ||
| | ||
| The extractor pattern `split(fst, snd)` defines `fst` as the first half "Hi" and | ||
| `snd` as the second half "Ho" of the right hand side "HiHo". Case classes and | ||
| enum cases implicitly define extractors with the name of the class or enum case. | ||
| Here, no extractor named s was found, so the pattern could not be typed. | ||
--------------------------------------------------------------------------------------------------------------------- | ||
-- [E189] Not Found Error: tests/neg/i18684.scala:5:6 ------------------------------------------------------------------ | ||
5 | val i() = 22 // error | ||
| ^ | ||
| no pattern match extractor named i was found | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| An application i(...) in a pattern can refer to an extractor | ||
| which defines an unapply or unapplySeq method. Example: | ||
| | ||
| object split: | ||
| def unapply(x: String) = | ||
| val (leading, trailing) = x.splitAt(x.length / 2) | ||
| Some((leading, trailing)) | ||
| | ||
| val split(fst, snd) = "HiHo" | ||
| | ||
| The extractor pattern `split(fst, snd)` defines `fst` as the first half "Hi" and | ||
| `snd` as the second half "Ho" of the right hand side "HiHo". Case classes and | ||
| enum cases implicitly define extractors with the name of the class or enum case. | ||
| Here, no extractor named i was found, so the pattern could not be typed. | ||
--------------------------------------------------------------------------------------------------------------------- | ||
-- [E189] Not Found Error: tests/neg/i18684.scala:10:8 ----------------------------------------------------------------- | ||
10 | val foo() = "33" // error | ||
| ^^^ | ||
| no pattern match extractor named foo was found | ||
|-------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| An application foo(...) in a pattern can refer to an extractor | ||
| which defines an unapply or unapplySeq method. Example: | ||
| | ||
| object split: | ||
| def unapply(x: String) = | ||
| val (leading, trailing) = x.splitAt(x.length / 2) | ||
| Some((leading, trailing)) | ||
| | ||
| val split(fst, snd) = "HiHo" | ||
| | ||
| The extractor pattern `split(fst, snd)` defines `fst` as the first half "Hi" and | ||
| `snd` as the second half "Ho" of the right hand side "HiHo". Case classes and | ||
| enum cases implicitly define extractors with the name of the class or enum case. | ||
| Here, no extractor named foo was found, so the pattern could not be typed. | ||
-------------------------------------------------------------------------------------------------------------------- | ||
-- [E127] Pattern Match Error: tests/neg/i18684.scala:12:6 ------------------------------------------------------------- | ||
12 | val inner(x) = 3 // error | ||
| ^^^^^ | ||
| Test.inner cannot be used as an extractor in a pattern because it lacks an unapply or unapplySeq method | ||
|-------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| An unapply method should be defined in an object as follow: | ||
| - If it is just a test, return a Boolean. For example case even() | ||
| - If it returns a single sub-value of type T, return an Option[T] | ||
| - If it returns several sub-values T1,...,Tn, group them in an optional tuple Option[(T1,...,Tn)] | ||
| | ||
| Sometimes, the number of sub-values isn't fixed and we would like to return a sequence. | ||
| For this reason, you can also define patterns through unapplySeq which returns Option[Seq[T]]. | ||
| This mechanism is used for instance in pattern case List(x1, ..., xn) | ||
-------------------------------------------------------------------------------------------------------------------- |
This file contains 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,12 @@ | ||
//> using options -explain | ||
object Test: | ||
val s(): String = "hello, world" // error | ||
|
||
val i() = 22 // error | ||
|
||
def foo(): String = "22" | ||
|
||
object inner: | ||
val foo() = "33" // error | ||
|
||
val inner(x) = 3 // error |
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
-- [E006] Not Found Error: tests/neg/i5101.scala:11:11 ----------------------------------------------------------------- | ||
-- [E189] Not Found Error: tests/neg/i5101.scala:11:11 ----------------------------------------------------------------- | ||
11 | case A0(_) => // error | ||
| ^^ | ||
| Not found: A0 | ||
| no pattern match extractor named A0 was found | ||
| | ||
| longer explanation available when compiling with `-explain` |
This file contains 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 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