Skip to content

Improvements to Typeclass Derivation #5839

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 3 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
* Pre: `sym` must have a position.
*/
def defPath(sym: Symbol, root: Tree)(implicit ctx: Context): List[Tree] = trace.onDebug(s"defpath($sym with position ${sym.span}, ${root.show})") {
require(sym.span.exists)
require(sym.span.exists, sym)
object accum extends TreeAccumulator[List[Tree]] {
def apply(x: List[Tree], tree: Tree)(implicit ctx: Context): List[Tree] = {
if (tree.span.contains(sym.span))
Expand Down
31 changes: 26 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Deriving.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ trait Deriving { this: Typer =>
/** A buffer for synthesized symbols */
private var synthetics = new mutable.ListBuffer[Symbol]

private var derivesGeneric = false

/** the children of `cls` ordered by textual occurrence */
lazy val children: List[Symbol] = cls.children

Expand Down Expand Up @@ -170,7 +172,7 @@ trait Deriving { this: Typer =>
val derivedType = checkClassType(underlyingType, derived.sourcePos, traitReq = false, stablePrefixReq = true)
val nparams = derivedType.classSymbol.typeParams.length
if (derivedType.isRef(defn.GenericClass))
() // do nothing, a Generic instance will be created anyway by `addGeneric`
derivesGeneric = true
else if (nparams == 1) {
val typeClass = derivedType.classSymbol
val firstKindedParams = cls.typeParams.filterNot(_.info.isLambdaSub)
Expand Down Expand Up @@ -210,14 +212,33 @@ trait Deriving { this: Typer =>
addDerivedInstance(defn.GenericType.name, genericCompleter, codePos, reportErrors = false)
}

/** If any of the instances has a companion with a `derived` member
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs/reference/derivation.md should be update to reflect that change. I think it would make sense to keep this under specified and say that Generic instance are only guaranteed to be cached when writing derives Generic.

* that refers to `scala.reflect.Generic`, add an implied instance
* of `Generic`. Note: this is just an optimization to avoid possible
* code duplication. Generic instances are created on the fly if they
* are missing from the companion.
*/
private def maybeAddGeneric(): Unit = {
val genericCls = defn.GenericClass
def refersToGeneric(sym: Symbol): Boolean = {
val companion = sym.info.finalResultType.classSymbol.companionModule
val derivd = companion.info.member(nme.derived)
derivd.hasAltWith(sd => sd.info.existsPart(p => p.typeSymbol == genericCls))
}
if (derivesGeneric || synthetics.exists(refersToGeneric)) {
derive.println(i"add generic infrastructure for $cls")
addGeneric()
addGenericClass()
}
}

/** Create symbols for derived instances and infrastructure,
* append them to `synthetics` buffer,
* and enter them into class scope.
* append them to `synthetics` buffer, and enter them into class scope.
* Also, add generic instances if needed.
*/
def enterDerived(derived: List[untpd.Tree]) = {
derived.foreach(processDerivedInstance(_))
addGeneric()
addGenericClass()
maybeAddGeneric()
}

private def tupleElems(tp: Type): List[Type] = tp match {
Expand Down