Skip to content

Commit 37dcf9b

Browse files
author
Nadezhda Balashova
committed
Add error message for only functional erased or implicit types
1 parent 842e4c4 commit 37dcf9b

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,9 +1423,9 @@ object Parsers {
14231423
case FORSOME => syntaxError(ExistentialTypesNoLongerSupported()); t
14241424
case _ =>
14251425
if (imods.isOneOf(GivenOrImplicit) && !t.isInstanceOf[FunctionWithMods])
1426-
syntaxError("Types with implicit keyword can only be function types `implicit (...) => ...`", implicitKwPos(start))
1426+
syntaxError(ImplicitTypesCanOnlyBeFunctionTypes(), implicitKwPos(start))
14271427
if (imods.is(Erased) && !t.isInstanceOf[FunctionWithMods])
1428-
syntaxError("Types with erased keyword can only be function types `erased (...) => ...`", implicitKwPos(start))
1428+
syntaxError(ErasedTypesCanOnlyBeFunctionTypes(), implicitKwPos(start))
14291429
t
14301430
}
14311431
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
148148
IllegalSuperAccessorID,
149149
TraitParameterUsedAsParentPrefixID,
150150
UnknownNamedEnclosingClassOrObjectID,
151-
IllegalCyclicTypeReferenceID
151+
IllegalCyclicTypeReferenceID,
152+
ImplicitTypesCanOnlyBeFunctionTypesID,
153+
ErasedTypesCanOnlyBeFunctionTypesID
152154

153155
def errorNumber = ordinal - 2
154156
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,4 +2372,18 @@ object messages {
23722372
val msg: String = i"illegal cyclic type reference: ${where} ${hl(lastChecked.show)} of $sym refers back to the type itself"
23732373
val explanation: String = ""
23742374
}
2375+
2376+
case class ImplicitTypesCanOnlyBeFunctionTypes()(implicit val ctx: Context)
2377+
extends Message(ImplicitTypesCanOnlyBeFunctionTypesID) {
2378+
val kind: String = "Syntax"
2379+
val msg: String = "Types with given keyword can only be function types `given (...) => ...`"
2380+
val explanation: String = ""
2381+
}
2382+
2383+
case class ErasedTypesCanOnlyBeFunctionTypes()(implicit val ctx: Context)
2384+
extends Message(ErasedTypesCanOnlyBeFunctionTypesID) {
2385+
val kind: String = "Syntax"
2386+
val msg: String = "Types with erased keyword can only be function types `erased (...) => ...`"
2387+
val explanation: String = ""
2388+
}
23752389
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,4 +1669,30 @@ class ErrorMessagesTests extends ErrorMessagesTest {
16691669
assertEquals("alias", where)
16701670
assertEquals("List[X]", lastChecked.show)
16711671
}
1672+
1673+
@Test def implicitTypesCanOnlyBeFunctionTypesSuccess() =
1674+
checkMessagesAfter(RefChecks.name) ("def foo(f: (given Int) => Int): Int = 1")
1675+
.expectNoErrors
1676+
1677+
@Test def erasedTypesCanOnlyBeFunctionTypesSuccess() =
1678+
checkMessagesAfter(FrontEnd.name) ("def foo(f: (erased Int) => Int): Int = 1")
1679+
.expectNoErrors
1680+
1681+
@Test def implicitTypesCanOnlyBeFunctionTypesFailed() =
1682+
checkMessagesAfter(FrontEnd.name) ("def foo(f: (given Int)): Int = 1")
1683+
.expect { (ictx, messages) =>
1684+
implicit val ctx: Context = ictx
1685+
assertMessageCount(1, messages)
1686+
val ImplicitTypesCanOnlyBeFunctionTypes() :: Nil = messages
1687+
assertEquals("Types with given keyword can only be function types `given (...) => ...`", messages.head.msg)
1688+
}
1689+
1690+
@Test def erasedTypesCanOnlyBeFunctionTypesFailed() =
1691+
checkMessagesAfter(FrontEnd.name) ("def foo(f: (erased Int)): Int = 1")
1692+
.expect { (ictx, messages) =>
1693+
implicit val ctx: Context = ictx
1694+
assertMessageCount(1, messages)
1695+
val ErasedTypesCanOnlyBeFunctionTypes() :: Nil = messages
1696+
assertEquals("Types with erased keyword can only be function types `erased (...) => ...`", messages.head.msg)
1697+
}
16721698
}

0 commit comments

Comments
 (0)