Skip to content

Add error reporting for mirror synthesis #15164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions compiler/src/dotty/tools/dotc/transform/SymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ object SymUtils:
* parameter section.
*/
def whyNotGenericProduct(using Context): String =
if (!self.is(CaseClass)) "it is not a case class"
else if (self.is(Abstract)) "it is an abstract class"
else if (self.primaryConstructor.info.paramInfoss.length != 1) "it takes more than one parameter list"
else if (isDerivedValueClass(self)) "it is a value class"
if (!self.is(CaseClass)) i"$self is not a case class"
else if (self.is(Abstract)) i"$self is an abstract class"
else if (self.primaryConstructor.info.paramInfoss.length != 1) i"$self takes more than one parameter list"
else if (isDerivedValueClass(self)) i"$self is a value class"
else ""

def isGenericProduct(using Context): Boolean = whyNotGenericProduct.isEmpty
Expand Down Expand Up @@ -144,9 +144,9 @@ object SymUtils:
*/
def whyNotGenericSum(declScope: Symbol)(using Context): String =
if (!self.is(Sealed))
s"it is not a sealed ${self.kindString}"
s"$self is not a sealed ${self.kindString}"
else if (!self.isOneOf(AbstractOrTrait))
s"it is not an abstract class"
s"$self is not an abstract class"
else {
val children = self.children
val companionMirror = self.useCompanionAsSumMirror
Expand All @@ -157,20 +157,20 @@ object SymUtils:
(self.isContainedIn(sym) && (companionMirror || declScope.isContainedIn(sym)))
|| sym.is(Module) && isAccessible(sym.owner)

if (child == self) "it has anonymous or inaccessible subclasses"
else if (!isAccessible(child.owner)) i"its child $child is not accessible"
if (child == self) i"$self has anonymous or inaccessible subclasses"
else if (!isAccessible(child.owner)) i"$self's child $child is not accessible"
else if (!child.isClass) ""
else {
val s = child.whyNotGenericProduct
if (s.isEmpty) s
else if (child.is(Sealed)) {
val s = child.whyNotGenericSum(if child.useCompanionAsSumMirror then child.linkedClass else ctx.owner)
if (s.isEmpty) s
else i"its child $child is not a generic sum because $s"
} else i"its child $child is not a generic product because $s"
else i"$self's child $child is not a generic sum because $s"
} else i"$self's child $child is not a generic product because $s"
}
}
if (children.isEmpty) "it does not have subclasses"
if (children.isEmpty) i"$self does not have subclasses"
else children.map(problem).find(!_.isEmpty).getOrElse("")
}

Expand Down
20 changes: 10 additions & 10 deletions compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -266,24 +266,26 @@ class ImplicitSearchError(
++ ErrorReporting.matchReductionAddendum(pt)
}

private def formatMsg(shortForm: String)(headline: String = shortForm) = arg match {
private def formatMsg(shortForm: String)(headline: String = shortForm) = arg match
case arg: Trees.SearchFailureIdent[?] =>
shortForm
arg.tpe match
case _: NoMatchingImplicits => headline
case tpe: SearchFailureType =>
i"$headline. ${tpe.explanation}"
case _ => headline
case _ =>
arg.tpe match {
arg.tpe match
case tpe: SearchFailureType =>
val original = arg match
case Inlined(call, _, _) => call
case _ => arg

i"""$headline.
|I found:
|
| ${original.show.replace("\n", "\n ")}
|
|But ${tpe.explanation}."""
}
}
case _ => headline

private def userDefinedErrorString(raw: String, paramNames: List[String], args: List[Type]): String = {
def translate(name: String): Option[String] = {
Expand All @@ -307,12 +309,10 @@ class ImplicitSearchError(
private def location(preposition: String) = if (where.isEmpty) "" else s" $preposition $where"

private def defaultAmbiguousImplicitMsg(ambi: AmbiguousImplicits) =
formatMsg(s"ambiguous given instances: ${ambi.explanation}${location("of")}")(
s"ambiguous given instances of type ${pt.show} found${location("for")}"
)
s"Ambiguous given instances: ${ambi.explanation}${location("of")}"

private def defaultImplicitNotFoundMessage =
ex"no given instance of type $pt was found${location("for")}"
ex"No given instance of type $pt was found${location("for")}"

/** Construct a custom error message given an ambiguous implicit
* candidate `alt` and a user defined message `raw`.
Expand Down
19 changes: 18 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,18 @@ object Implicits:
override def msg(using Context) = _msg
def explanation(using Context) = msg.toString

/** A search failure type for failed synthesis of terms for special types */
class SynthesisFailure(reasons: List[String], val expectedType: Type) extends SearchFailureType:
def argument = EmptyTree

private def formatReasons =
if reasons.length > 1 then
reasons.mkString("\n\t* ", "\n\t* ", "")
else
reasons.mkString

def explanation(using Context) = em"Failed to synthesize an instance of type ${clarify(expectedType)}: ${formatReasons}"

end Implicits

import Implicits._
Expand Down Expand Up @@ -854,7 +866,12 @@ trait Implicits:
if fail.isAmbiguous then failed
else
if synthesizer == null then synthesizer = Synthesizer(this)
synthesizer.uncheckedNN.tryAll(formal, span).orElse(failed)
val (tree, errors) = synthesizer.uncheckedNN.tryAll(formal, span)
if errors.nonEmpty then
SearchFailure(new SynthesisFailure(errors, formal), span).tree
else
tree.orElse(failed)


/** Search an implicit argument and report error if not found */
def implicitArgTree(formal: Type, span: Span)(using Context): Tree = {
Expand Down
Loading