diff --git a/compiler/src/dotty/tools/dotc/core/TypeErrors.scala b/compiler/src/dotty/tools/dotc/core/TypeErrors.scala index 24a207da6836..7043e944f180 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeErrors.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeErrors.scala @@ -142,6 +142,9 @@ class CyclicReference private (val denot: SymDenotation)(using Context) extends val unsafeFlags = cycleSym.flagsUNSAFE val isMethod = unsafeFlags.is(Method) val isVal = !isMethod && cycleSym.isTerm + val isConstructor = cycleSym.isConstructor + + // println("isMethod?"+isMethod+",isConstr:"+isConstructor) /* This CyclicReference might have arisen from asking for `m`'s type while trying to infer it. * 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 case tree: untpd.ValOrDefDef if !tree.tpt.typeOpt.exists => if (inImplicitSearch) TermMemberNeedsResultTypeForImplicitSearch(cycleSym) + else if (isConstructor) + CyclicMsgUnknownBug(cycleSym) else if (isMethod) OverloadedOrRecursiveMethodNeedsResultType(cycleSym) else if (isVal) diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index 423c1cdef264..aca5d5be1d79 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -1231,6 +1231,14 @@ extends CyclicMsg(OverloadedOrRecursiveMethodNeedsResultTypeID) { |""" } +class CyclicMsgUnknownBug(cycleSym: Symbol)(using Context) +extends CyclicMsg(OverloadedOrRecursiveMethodNeedsResultTypeID) { + def msg(using Context) = i"""Compiler bug: unknown cyclic error for $cycleSym. please report""" + def explain(using Context) = + i"""|For temporary fix, add type annotations to areas involving this constructor + """ +} + class RecursiveValueNeedsResultType(cycleSym: Symbol)(using Context) extends CyclicMsg(RecursiveValueNeedsResultTypeID) { def msg(using Context) = i"""Recursive $cycleSym needs type""" diff --git a/compiler/test/dotty/tools/dotc/reporting/CyclicErrorMessages.scala b/compiler/test/dotty/tools/dotc/reporting/CyclicErrorMessages.scala new file mode 100644 index 000000000000..65cd3abe981e --- /dev/null +++ b/compiler/test/dotty/tools/dotc/reporting/CyclicErrorMessages.scala @@ -0,0 +1,38 @@ +package dotty.tools +package dotc +package reporting + +import dotty.tools.dotc.core.Contexts.Context +import org.junit.Assert._ +import org.junit.Test + +/*better error message for cyclic errors when using exports as in #17076 */ +class CyclicErrorMessages extends ErrorMessagesTest { + @Test def cyclicErrMsg = + checkMessagesAfter("typer") { + """object A { + | def bar(x: B.Foo[Int]) = x + |} + | + |object B { + | import C.* + | case class Foo[T <: Int](x: Any) + | def foo = Foo(0) + |} + | + |object C extends D.Foo[Int](0) + | + |object D { + | export B.foo + | type Foo[T <: Int] = B.Foo[T] + |} + """.stripMargin + }.expect { (itcx, messages) => + given Context = itcx + + assertMessageCount(2, messages) + val msg = messages.head.message + + assertTrue(msg.contains("Compiler bug: unknown cyclic error")) + } +}