Skip to content

Commit ce333b1

Browse files
committed
Change 'overloaded/recursive method/value needs type' to Message (see scala#2026)
1 parent db24246 commit ce333b1

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public enum ErrorMessageID {
5151
ExpectedTokenButFoundID,
5252
MixedLeftAndRightAssociativeOpsID,
5353
CantInstantiateAbstractClassOrTraitID,
54+
OverloadedOrRecursiveMethodNeedsResultTypeID,
55+
RecursiveValueNeedsResultTypeID,
56+
CyclicReferenceInvolvingImplicitID,
5457
;
5558

5659
public int errorNumber() {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,4 +1146,32 @@ object messages {
11461146
|""".stripMargin
11471147
}
11481148

1149+
case class OverloadedOrRecursiveMethodNeedsResultType(tree: Names.TermName)(implicit ctx: Context)
1150+
extends Message(OverloadedOrRecursiveMethodNeedsResultTypeID) {
1151+
val kind = "Syntax"
1152+
val msg = hl"""overloaded or recursive method ${tree} needs result type"""
1153+
val explanation =
1154+
hl"""""".stripMargin
1155+
}
1156+
1157+
case class RecursiveValueNeedsResultType(tree: Names.TermName)(implicit ctx: Context)
1158+
extends Message(RecursiveValueNeedsResultTypeID) {
1159+
val kind = "Syntax"
1160+
val msg = hl"""recursive value ${tree.name} needs type"""
1161+
val explanation =
1162+
hl"""""".stripMargin
1163+
}
1164+
1165+
case class CyclicReferenceInvolvingImplicit(cycleSym: Symbol)(implicit ctx: Context)
1166+
extends Message(CyclicReferenceInvolvingImplicitID) {
1167+
val kind = "Syntax"
1168+
val msg = hl"""cyclic reference involving implicit $cycleSym"""
1169+
val explanation =
1170+
hl"""|This happens when the right hand-side of $cycleSym's definition involves an implicit search.
1171+
|To avoid the error, give $cycleSym an explicit type.
1172+
|""".stripMargin
1173+
}
1174+
1175+
1176+
11491177
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,20 @@ object ErrorReporting {
2828

2929
def cyclicErrorMsg(ex: CyclicReference)(implicit ctx: Context) = {
3030
val cycleSym = ex.denot.symbol
31-
def errorMsg(msg: String, cx: Context): String =
31+
def errorMsg(msg: String, cx: Context): Message =
3232
if (cx.mode is Mode.InferringReturnType) {
3333
cx.tree match {
3434
case tree: untpd.DefDef if !tree.tpt.typeOpt.exists =>
35-
em"overloaded or recursive method ${tree.name} needs result type"
35+
OverloadedOrRecursiveMethodNeedsResultType(tree.name)
3636
case tree: untpd.ValDef if !tree.tpt.typeOpt.exists =>
37-
em"recursive value ${tree.name} needs type"
37+
RecursiveValueNeedsResultType(tree.name)
3838
case _ =>
3939
errorMsg(msg, cx.outer)
4040
}
4141
} else msg
4242

4343
if (cycleSym.is(Implicit, butNot = Method) && cycleSym.owner.isTerm)
44-
em"""cyclic reference involving implicit $cycleSym
45-
|This happens when the right hand-side of $cycleSym's definition involves an implicit search.
46-
|To avoid the error, give $cycleSym an explicit type."""
44+
CyclicReferenceInvolvingImplicit(cycleSym)
4745
else
4846
errorMsg(ex.show, ctx)
4947
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,23 @@ class ErrorMessagesTests extends ErrorMessagesTest {
198198
assertTrue("expected trait", isTrait)
199199
}
200200

201+
@Test def cantInstantiateTrait =
202+
checkMessagesAfter("refchecks") {
203+
"""
204+
|object Scope {
205+
| trait Concept
206+
| new Concept()
207+
|}
208+
""".stripMargin
209+
}
210+
.expect { (ictx, messages) =>
211+
implicit val ctx: Context = ictx
212+
val defn = ictx.definitions
213+
214+
assertMessageCount(1, messages)
215+
val CantInstantiateAbstractClassOrTrait(cls, isTrait) :: Nil = messages
216+
assertEquals("Concept", cls.name.show)
217+
assertTrue("expected trait", isTrait)
218+
}
219+
201220
}

0 commit comments

Comments
 (0)