Skip to content

Commit

Permalink
Add example to explanation
Browse files Browse the repository at this point in the history
[Cherry-picked eed38ec]
  • Loading branch information
odersky authored and WojciechMazur committed Jun 21, 2024
1 parent 9c0bca3 commit 8937d0b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
14 changes: 12 additions & 2 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2357,8 +2357,18 @@ class ExtractorNotFound(val name: Name)(using Context) extends NotFoundMsg(Extra
def msg(using Context) = i"no pattern match extractor named $name was found"
def explain(using Context) =
i"""An application $name(...) in a pattern can refer to an extractor
|which defines an unapply or unapplySeq method. Case classes and enum cases
|implicitly define extractors with the name of the class or enum case.
|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 $name was found, so the pattern could not be typed."""

class MemberWithSameNameAsStatic()(using Context)
Expand Down
42 changes: 36 additions & 6 deletions tests/neg/i18684.check
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| An application s(...) in a pattern can refer to an extractor
| which defines an unapply or unapplySeq method. Case classes and enum cases
| implicitly define extractors with the name of the class or enum case.
| 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 ------------------------------------------------------------------
Expand All @@ -18,8 +28,18 @@
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| An application i(...) in a pattern can refer to an extractor
| which defines an unapply or unapplySeq method. Case classes and enum cases
| implicitly define extractors with the name of the class or enum case.
| 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 -----------------------------------------------------------------
Expand All @@ -30,8 +50,18 @@
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| An application foo(...) in a pattern can refer to an extractor
| which defines an unapply or unapplySeq method. Case classes and enum cases
| implicitly define extractors with the name of the class or enum case.
| 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 -------------------------------------------------------------
Expand Down

0 comments on commit 8937d0b

Please sign in to comment.