Skip to content

Commit dd17ab1

Browse files
committed
fix:better error msg for cyclic error for constructors
1 parent 6e5be23 commit dd17ab1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

compiler/src/dotty/tools/dotc/core/TypeErrors.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ class CyclicReference private (val denot: SymDenotation)(using Context) extends
140140
// cycleSym.flags would try completing denot and would fail, but here we can use flagsUNSAFE to detect flags
141141
// set by the parser.
142142
val unsafeFlags = cycleSym.flagsUNSAFE
143-
val isMethod = unsafeFlags.is(Method)
143+
val isMethod = unsafeFlags.is(Method) // sometimes,isMethod and isConstructor can both be true!
144144
val isVal = !isMethod && cycleSym.isTerm
145+
val isConstructor = cycleSym.isConstructor
146+
147+
// println("isMethod?"+isMethod+",isConstr:"+isConstructor)
145148

146149
/* This CyclicReference might have arisen from asking for `m`'s type while trying to infer it.
147150
* To try to diagnose this, walk the context chain searching for context in
@@ -154,6 +157,8 @@ class CyclicReference private (val denot: SymDenotation)(using Context) extends
154157
case tree: untpd.ValOrDefDef if !tree.tpt.typeOpt.exists =>
155158
if (inImplicitSearch)
156159
TermMemberNeedsResultTypeForImplicitSearch(cycleSym)
160+
else if (isConstructor)
161+
CyclicMsgUnknownBug(cycleSym)
157162
else if (isMethod)
158163
OverloadedOrRecursiveMethodNeedsResultType(cycleSym)
159164
else if (isVal)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,14 @@ extends CyclicMsg(OverloadedOrRecursiveMethodNeedsResultTypeID) {
12311231
|"""
12321232
}
12331233

1234+
class CyclicMsgUnknownBug(cycleSym: Symbol)(using Context)
1235+
extends CyclicMsg(OverloadedOrRecursiveMethodNeedsResultTypeID) {
1236+
def msg(using Context) = i"""Compiler bug: unknown cyclic error for $cycleSym. please report"""
1237+
def explain(using Context) =
1238+
i"""|For temporary fix, add type annotations to areas involving this constructor
1239+
"""
1240+
}
1241+
12341242
class RecursiveValueNeedsResultType(cycleSym: Symbol)(using Context)
12351243
extends CyclicMsg(RecursiveValueNeedsResultTypeID) {
12361244
def msg(using Context) = i"""Recursive $cycleSym needs type"""

0 commit comments

Comments
 (0)