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 3 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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/SymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ object SymUtils:
if (!self.is(Sealed))
s"it is not a sealed ${self.kindString}"
else if (!self.isOneOf(AbstractOrTrait))
s"it is not an abstract class"
"it is not an abstract class"
else {
val children = self.children
val companionMirror = self.useCompanionAsSumMirror
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