Skip to content

Commit c79ffa1

Browse files
Merge pull request #7848 from EpamLifeSciencesTeam/issue-1589-error-mes-implicit-erased-types-can-only-be-ft
Add error message for only functional erased or implicit types
2 parents dcf0254 + c3a72f7 commit c79ffa1

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1431,9 +1431,9 @@ object Parsers {
14311431
case FORSOME => syntaxError(ExistentialTypesNoLongerSupported()); t
14321432
case _ =>
14331433
if (imods.isOneOf(GivenOrImplicit) && !t.isInstanceOf[FunctionWithMods])
1434-
syntaxError("Types with implicit keyword can only be function types `implicit (...) => ...`", implicitKwPos(start))
1434+
syntaxError(ImplicitTypesCanOnlyBeFunctionTypes(), implicitKwPos(start))
14351435
if (imods.is(Erased) && !t.isInstanceOf[FunctionWithMods])
1436-
syntaxError("Types with erased keyword can only be function types `erased (...) => ...`", implicitKwPos(start))
1436+
syntaxError(ErasedTypesCanOnlyBeFunctionTypes(), implicitKwPos(start))
14371437
t
14381438
}
14391439
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
149149
TraitParameterUsedAsParentPrefixID,
150150
UnknownNamedEnclosingClassOrObjectID,
151151
IllegalCyclicTypeReferenceID,
152-
MissingTypeParameterInTypeAppID
152+
MissingTypeParameterInTypeAppID,
153+
ImplicitTypesCanOnlyBeFunctionTypesID,
154+
ErasedTypesCanOnlyBeFunctionTypesID
153155

154156
def errorNumber = ordinal - 2
155157
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -2381,4 +2381,18 @@ object messages {
23812381
val msg: String = i"illegal cyclic type reference: ${where} ${hl(lastChecked.show)} of $sym refers back to the type itself"
23822382
val explanation: String = ""
23832383
}
2384+
2385+
case class ImplicitTypesCanOnlyBeFunctionTypes()(implicit val ctx: Context)
2386+
extends Message(ImplicitTypesCanOnlyBeFunctionTypesID) {
2387+
val kind: String = "Syntax"
2388+
val msg: String = "Types with given keyword can only be function types `given (...) => ...`"
2389+
val explanation: String = ""
2390+
}
2391+
2392+
case class ErasedTypesCanOnlyBeFunctionTypes()(implicit val ctx: Context)
2393+
extends Message(ErasedTypesCanOnlyBeFunctionTypesID) {
2394+
val kind: String = "Syntax"
2395+
val msg: String = "Types with erased keyword can only be function types `erased (...) => ...`"
2396+
val explanation: String = ""
2397+
}
23842398
}

compiler/test/dotty/tools/DottyTest.scala

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ trait DottyTest extends ContextEscapeDetection {
4242
protected def initializeCtx(fc: FreshContext): Unit = {
4343
fc.setSetting(fc.settings.encoding, "UTF8")
4444
fc.setSetting(fc.settings.classpath, TestConfiguration.basicClasspath)
45+
fc.setSetting(fc.settings.YerasedTerms, true)
4546
fc.setProperty(ContextDoc, new ContextDocstrings)
4647
}
4748

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

+26
Original file line numberDiff line numberDiff line change
@@ -1684,4 +1684,30 @@ class ErrorMessagesTests extends ErrorMessagesTest {
16841684
assertEquals("alias", where)
16851685
assertEquals("List[X]", lastChecked.show)
16861686
}
1687+
1688+
@Test def implicitTypesCanOnlyBeFunctionTypesSuccess() =
1689+
checkMessagesAfter(RefChecks.name) ("def foo(f: (given Int) => Int): Int = 1")
1690+
.expectNoErrors
1691+
1692+
@Test def erasedTypesCanOnlyBeFunctionTypesSuccess() =
1693+
checkMessagesAfter(FrontEnd.name) ("def foo(f: (erased Int) => Int): Int = 1")
1694+
.expectNoErrors
1695+
1696+
@Test def implicitTypesCanOnlyBeFunctionTypesFailed() =
1697+
checkMessagesAfter(FrontEnd.name) ("def foo(f: (given Int)): Int = 1")
1698+
.expect { (ictx, messages) =>
1699+
implicit val ctx: Context = ictx
1700+
assertMessageCount(1, messages)
1701+
val ImplicitTypesCanOnlyBeFunctionTypes() :: Nil = messages
1702+
assertEquals("Types with given keyword can only be function types `given (...) => ...`", messages.head.msg)
1703+
}
1704+
1705+
@Test def erasedTypesCanOnlyBeFunctionTypesFailed() =
1706+
checkMessagesAfter(FrontEnd.name) ("def foo(f: (erased Int)): Int = 1")
1707+
.expect { (ictx, messages) =>
1708+
implicit val ctx: Context = ictx
1709+
assertMessageCount(1, messages)
1710+
val ErasedTypesCanOnlyBeFunctionTypes() :: Nil = messages
1711+
assertEquals("Types with erased keyword can only be function types `erased (...) => ...`", messages.head.msg)
1712+
}
16871713
}

0 commit comments

Comments
 (0)