File tree 4 files changed +52
-1
lines changed
test/dotty/tools/dotc/reporting
4 files changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,7 @@ public enum ErrorMessageID {
112
112
UndefinedNamedTypeParameterID ,
113
113
IllegalStartOfStatementID ,
114
114
TraitIsExpectedID ,
115
+ InvalidUnapplyReturnTypeID ,
115
116
;
116
117
117
118
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -1916,4 +1916,32 @@ object messages {
1916
1916
| """
1917
1917
}
1918
1918
}
1919
+
1920
+ case class InvalidUnapplyReturnType (unapplyResult : Type , unapplyName : Symbol # ThisName )(implicit ctx : Context )
1921
+ extends Message (InvalidUnapplyReturnTypeID ) {
1922
+ val kind = " Type Mismatch"
1923
+ val msg = hl """ | ${Red (i " $unapplyResult" )} is not a valid result type of an $unapplyName method of an ${Magenta (" extractor" )}. """
1924
+ val explanation =
1925
+ hl """
1926
+ |To be used as an ${Magenta (" extractor" )}, an unapply method has to return an ${Green (" Option[T]" )} or a ${Green (" Boolean" )}:
1927
+ |
1928
+ |class A(val i: Int)
1929
+ |
1930
+ |object B {
1931
+ | def unapply(a: A): ${Green (" Option[Int]" )} = Some(a.i)
1932
+ |}
1933
+ |
1934
+ |object C {
1935
+ | def unapply(a: A): ${Green (" Boolean" )} = a.i == 2
1936
+ |}
1937
+ |
1938
+ |object Test {
1939
+ | def test(a: A) = a match {
1940
+ | ${Magenta (" case B(1)" )} => 1
1941
+ | ${Magenta (" case a @ C()" )} => 2
1942
+ | }
1943
+ |}
1944
+ """ .stripMargin
1945
+ }
1946
+
1919
1947
}
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ import language.implicitConversions
33
33
import reporting .diagnostic .Message
34
34
import reporting .trace
35
35
import Constants .{Constant , IntTag , LongTag }
36
+ import dotty .tools .dotc .reporting .diagnostic .messages .InvalidUnapplyReturnType
36
37
37
38
import scala .collection .mutable .ListBuffer
38
39
@@ -92,7 +93,7 @@ object Applications {
92
93
def getTp = extractorMemberType(unapplyResult, nme.get, pos)
93
94
94
95
def fail = {
95
- ctx.error(i " $ unapplyResult is not a valid result type of an $unapplyName method of an extractor " , pos )
96
+ ctx.error(InvalidUnapplyReturnType ( unapplyResult, unapplyName) )
96
97
Nil
97
98
}
98
99
Original file line number Diff line number Diff line change @@ -1172,4 +1172,25 @@ class ErrorMessagesTests extends ErrorMessagesTest {
1172
1172
val TraitIsExpected (symbol) :: Nil = messages
1173
1173
assertEquals(" class B" , symbol.show)
1174
1174
}
1175
+
1176
+ @ Test def invalidUnapplyReturnType =
1177
+ checkMessagesAfter(" frontend" ) {
1178
+ """
1179
+ |class A(val i: Int)
1180
+ |
1181
+ |object A {
1182
+ | def unapply(a: A): Int = a.i
1183
+ | def test(a: A) = a match {
1184
+ | case A() => 1
1185
+ | }
1186
+ |}
1187
+ """ .stripMargin
1188
+ }.expect { (ictx, messages) =>
1189
+ implicit val ctx : Context = ictx
1190
+ assertMessageCount(1 , messages)
1191
+ val InvalidUnapplyReturnType (unapplyResult, unapplyName) :: Nil = messages
1192
+ assertEquals(" Int" , unapplyResult.show)
1193
+ assertEquals(" unapply" , unapplyName.show)
1194
+ }
1195
+
1175
1196
}
You can’t perform that action at this time.
0 commit comments