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
Integrate splain – implicit resolution chains and type diffs in error messages #7785
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ import scala.tools.nsc.Reporting.WarningCategory | |
* | ||
* @author Martin Odersky | ||
*/ | ||
trait Implicits { | ||
trait Implicits extends splain.SplainData { | ||
self: Analyzer => | ||
|
||
import global._ | ||
|
@@ -105,12 +105,14 @@ trait Implicits { | |
if (shouldPrint) | ||
typingStack.printTyping(tree, "typing implicit: %s %s".format(tree, context.undetparamsString)) | ||
val implicitSearchContext = context.makeImplicit(reportAmbiguous) | ||
ImplicitErrors.startSearch(pt) | ||
val dpt = if (isView) pt else dropByName(pt) | ||
val isByName = dpt ne pt | ||
val search = new ImplicitSearch(tree, dpt, isView, implicitSearchContext, pos, isByName) | ||
pluginsNotifyImplicitSearch(search) | ||
val result = search.bestImplicit | ||
pluginsNotifyImplicitSearchResult(result) | ||
ImplicitErrors.finishSearch(result.isSuccess, pt) | ||
|
||
if (result.isFailure && saveAmbiguousDivergent && implicitSearchContext.reporter.hasErrors) | ||
implicitSearchContext.reporter.propagateImplicitTypeErrorsTo(context.reporter) | ||
|
@@ -146,7 +148,7 @@ trait Implicits { | |
if (result.isFailure && !silent) { | ||
val err = context.reporter.firstError | ||
val errPos = err.map(_.errPos).getOrElse(pos) | ||
val errMsg = err.map(_.errMsg).getOrElse("implicit search has failed. to find out the reason, turn on -Xlog-implicits") | ||
val errMsg = err.map(_.errMsg).getOrElse("implicit search has failed. to find out the reason, turn on -Vimplicits") | ||
onError(errPos, errMsg) | ||
} | ||
result.tree | ||
|
@@ -443,7 +445,7 @@ trait Implicits { | |
def pos = if (pos0 != NoPosition) pos0 else tree.pos | ||
|
||
@inline final def failure(what: Any, reason: => String, pos: Position = this.pos): SearchResult = { | ||
if (settings.XlogImplicits) | ||
if (settings.debug) | ||
reporter.echo(pos, s"$what is not a valid implicit value for $pt because:\n$reason") | ||
SearchFailure | ||
} | ||
|
@@ -906,7 +908,9 @@ trait Implicits { | |
// bounds check on the expandee tree | ||
itree3.attachments.get[MacroExpansionAttachment] match { | ||
case Some(MacroExpansionAttachment(exp @ TypeApply(fun, targs), _)) => | ||
checkBounds(exp, NoPrefix, NoSymbol, fun.symbol.typeParams, targs.map(_.tpe), "inferred ") | ||
val targTpes = mapList(targs)(_.tpe) | ||
val withinBounds = checkBounds(exp, NoPrefix, NoSymbol, fun.symbol.typeParams, targTpes, "inferred ") | ||
if (!withinBounds) splainPushNonconformantBonds(pt, tree, targTpes, undetParams, None) | ||
case _ => () | ||
} | ||
|
||
|
@@ -953,6 +957,7 @@ trait Implicits { | |
|
||
context.reporter.firstError match { | ||
case Some(err) => | ||
splainPushImplicitSearchFailure(itree3, pt, err) | ||
fail("typing TypeApply reported errors for the implicit tree: " + err.errMsg) | ||
case None => | ||
val result = new SearchResult(unsuppressMacroExpansion(itree3), subst, context.undetparams) | ||
|
@@ -1492,17 +1497,17 @@ trait Implicits { | |
// so that if we find one, we could convert it to whatever universe we need by the means of the `in` method | ||
// if no tag is found in scope, we end up here, where we ask someone to materialize the tag for us | ||
// however, since the original search was about a tag with no particular prefix, we cannot proceed | ||
// this situation happens very often, so emitting an error message here (even if only for -Xlog-implicits) would be too much | ||
// this situation happens very often, so emitting an error message here (even if only for -Vimplicits) would be too much | ||
//return failure(tp, "tag error: unsupported prefix type %s (%s)".format(pre, pre.kind)) | ||
return SearchFailure | ||
} | ||
) | ||
// todo. migrate hardcoded materialization in Implicits to corresponding implicit macros | ||
val materializer = atPos(pos.focus)(gen.mkMethodCall(TagMaterializers(tagClass), List(tp), if (prefix != EmptyTree) List(prefix) else List())) | ||
if (settings.XlogImplicits) reporter.echo(pos, "materializing requested %s.%s[%s] using %s".format(pre, tagClass.name, tp, materializer)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
if (settings.debug) reporter.echo(pos, "materializing requested %s.%s[%s] using %s".format(pre, tagClass.name, tp, materializer)) | ||
if (context.macrosEnabled) success(materializer) | ||
// don't call `failure` here. if macros are disabled, we just fail silently | ||
// otherwise -Xlog-implicits will spam the long with zillions of "macros are disabled" | ||
// otherwise -Vimplicits/-Vdebug will spam the long with zillions of "macros are disabled" | ||
// this is ugly but temporary, since all this code will be removed once I fix implicit macros | ||
else SearchFailure | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/compiler/scala/tools/nsc/typechecker/splain/SplainData.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.tools.nsc | ||
package typechecker | ||
package splain | ||
|
||
import scala.util.matching.Regex | ||
|
||
trait SplainData { | ||
self: Analyzer => | ||
|
||
import global._ | ||
|
||
sealed trait ImplicitErrorSpecifics | ||
|
||
object ImplicitErrorSpecifics { | ||
case class NotFound(param: Symbol) extends ImplicitErrorSpecifics | ||
|
||
case class NonconformantBounds( | ||
targs: List[Type], tparams: List[Symbol], originalError: Option[AbsTypeError], | ||
) extends ImplicitErrorSpecifics | ||
} | ||
|
||
object ImplicitErrors { | ||
var stack: List[Type] = Nil | ||
var errors: List[ImplicitError] = Nil | ||
|
||
def push(error: ImplicitError): Unit = errors ::= error | ||
def nesting: Int = stack.length - 1 | ||
def nested: Boolean = stack.nonEmpty | ||
def removeErrorsFor(tpe: Type): Unit = errors = errors.dropWhile(_.tpe == tpe) | ||
|
||
def startSearch(expectedType: Type): Unit = { | ||
dwijnand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (settings.Vimplicits) { | ||
if (!nested) errors = List() | ||
stack = expectedType :: stack | ||
} | ||
} | ||
|
||
def finishSearch(success: Boolean, expectedType: Type): Unit = { | ||
dwijnand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (settings.Vimplicits) { | ||
if (success) removeErrorsFor(expectedType) | ||
stack = stack.drop(1) | ||
} | ||
} | ||
} | ||
|
||
case class ImplicitError(tpe: Type, candidate: Tree, nesting: Int, specifics: ImplicitErrorSpecifics) { | ||
import ImplicitError._ | ||
|
||
override def equals(other: Any) = other match { | ||
case o: ImplicitError => o.tpe.toString == tpe.toString && candidateName(this) == candidateName(o) | ||
case _ => false | ||
} | ||
|
||
override def hashCode = (tpe.toString.##, ImplicitError.candidateName(this).##).## | ||
override def toString = s"ImplicitError(${shortName(tpe.toString)}, ${shortName(candidate.toString)}), $nesting, $specifics)" | ||
} | ||
|
||
object ImplicitError { | ||
def unapplyCandidate(e: ImplicitError): Tree = | ||
e.candidate match { | ||
case TypeApply(fun, _) => fun | ||
case a => a | ||
} | ||
|
||
def candidateName(e: ImplicitError): String = | ||
unapplyCandidate(e) match { | ||
case Select(_, name) => name.toString | ||
case Ident(name) => name.toString | ||
case a => a.toString | ||
} | ||
|
||
val candidateRegex: Regex = """.*\.this\.(.*)""".r | ||
|
||
def cleanCandidate(e: ImplicitError): String = | ||
unapplyCandidate(e).toString match { | ||
case candidateRegex(suf) => suf | ||
case a => a | ||
} | ||
|
||
def shortName(ident: String): String = ident.substring(ident.lastIndexOf(".") + 1) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/compiler/scala/tools/nsc/typechecker/splain/SplainDiagnostics.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.tools.nsc | ||
package typechecker | ||
package splain | ||
|
||
trait SplainDiagnostics extends splain.SplainFormatting { | ||
self: Analyzer => | ||
|
||
import global._ | ||
|
||
def splainFoundReqMsg(found: Type, req: Type): String = { | ||
if (settings.VtypeDiffs) ";\n" + showFormattedL(formatDiff(found, req, top = true), break = true).indent.joinLines | ||
else "" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This were the old style traces which are now handled by splain, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I was hoping but not what the tests seem to show. We lose these bits of information: