Skip to content

Commit b072494

Browse files
author
Arnaud ESTEVE
committed
attempt at fixing Applications.scala:95 #1589
1 parent 1a52057 commit b072494

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public enum ErrorMessageID {
125125
UnableToEmitSwitchID,
126126
MissingCompanionForStaticID,
127127
PolymorphicMethodMissingTypeInParentID,
128+
NotAValidResultTypeOfUnapplyID
128129
;
129130

130131
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,4 +2074,21 @@ object messages {
20742074
|$rsym does not override any method in $parentSym. Structural refinement does not allow for
20752075
|polymorphic methods."""
20762076
}
2077+
2078+
case class NotAValidResultTypeOfUnapply(sym: Symbol, resultType: Type, args: List[untpd.Tree])(implicit ctx: Context) extends Message(NotAValidResultTypeOfUnapplyID) {
2079+
val kind = "Reference"
2080+
val resultClassName = resultType.classSymbol.name
2081+
val msg = hl"Cannot extract (${sym.name}) from ${sym.owner}. $resultClassName is not a valid extractor return type"
2082+
val explanation =
2083+
hl"""${sym.name} method of ${sym.owner} has been tried as a candidate to extract ${args.map(_.show).mkString("[", ", ", "]")}
2084+
|Its return type: ${s"$resultClassName"} is not a valid return type for an extractor.
2085+
|
2086+
|The return type of an extractor should be chosen as follows:
2087+
| - If it is just a test, return a ${"Boolean"}. For instance ${"case even()"}
2088+
| - If it returns a single sub-value of type ${s"$resultClassName"}, return an ${s"Option[$resultClassName]"}
2089+
| - If you want to return several sub-values ${"T1,...,Tn"}, group them in an optional tuple ${"Option[(T1,...,Tn)]"}.
2090+
""".stripMargin
2091+
2092+
}
2093+
20772094
}

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import language.implicitConversions
3333
import reporting.diagnostic.Message
3434
import reporting.trace
3535
import Constants.{Constant, IntTag, LongTag}
36-
import dotty.tools.dotc.reporting.diagnostic.messages.UnapplyInvalidNumberOfArguments
36+
import dotty.tools.dotc.reporting.diagnostic.messages.{NotAValidResultTypeOfUnapply, UnapplyInvalidNumberOfArguments}
3737

3838
import scala.collection.mutable.ListBuffer
3939

@@ -93,7 +93,8 @@ object Applications {
9393
def getTp = extractorMemberType(unapplyResult, nme.get, pos)
9494

9595
def fail = {
96-
ctx.error(i"$unapplyResult is not a valid result type of an $unapplyName method of an extractor", pos)
96+
// ctx.error(i"$unapplyResult is not a valid result type of an $unapplyName method of an extractor", pos)
97+
ctx.error(NotAValidResultTypeOfUnapply(unapplyFn.symbol, unapplyResult, args), pos)
9798
Nil
9899
}
99100

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package dotty.tools
22
package dotc
33
package reporting
44

5-
import core.Contexts.Context
6-
import diagnostic.messages._
7-
import dotty.tools.dotc.core.Flags
8-
import dotty.tools.dotc.core.Flags.FlagSet
5+
import dotty.tools.dotc.core.Contexts.Context
96
import dotty.tools.dotc.core.Types.WildcardType
107
import dotty.tools.dotc.parsing.Tokens
8+
import dotty.tools.dotc.reporting.diagnostic.messages._
119
import org.junit.Assert._
1210
import org.junit.Test
1311

@@ -1294,4 +1292,26 @@ class ErrorMessagesTests extends ErrorMessagesTest {
12941292
assertEquals("method get", rsym.show)
12951293
assertEquals("class Object", parentSym.show)
12961294
}
1295+
1296+
@Test def notAValidReturnTypeOfUnapply =
1297+
checkMessagesAfter("frontend") {
1298+
"""
1299+
|object Test {
1300+
| object Foo {
1301+
| def unapply(arg: String): String = arg.toUpperCase
1302+
| }
1303+
| val Foo(unextractedValue) = "test"
1304+
|}
1305+
""".stripMargin
1306+
}.expect { (ictx, messages) =>
1307+
implicit val ctx: Context = ictx
1308+
1309+
assertMessageCount(1, messages)
1310+
val NotAValidResultTypeOfUnapply(sym, resultType, args) = messages.head
1311+
assertEquals("method unapply", sym.show)
1312+
assertEquals("String", resultType.show)
1313+
assertEquals(1, args.length)
1314+
assertEquals("unextractedValue", args.head.show)
1315+
}
1316+
12971317
}

0 commit comments

Comments
 (0)