Skip to content

Commit 51607a4

Browse files
committed
Allow to derive directly from Generic
In this case only the generic infrastructure will be created. Also, fix a bug related to monomorphic cases and add a new `derive` printer for debugging.
1 parent 0b4074d commit 51607a4

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

compiler/src/dotty/tools/dotc/config/Printers.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ object Printers {
1818
val config: Printer = noPrinter
1919
val cyclicErrors: Printer = noPrinter
2020
val debug = noPrinter
21+
val derive = new Printer
2122
val dottydoc: Printer = noPrinter
2223
val exhaustivity: Printer = noPrinter
2324
val gadts: Printer = noPrinter

compiler/src/dotty/tools/dotc/typer/Deriving.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ProtoTypes._
1111
import util.Positions._
1212
import collection.mutable
1313
import Constants.Constant
14-
import config.Printers.typr
14+
import config.Printers.derive
1515
import Inferencing._
1616
import transform.TypeUtils._
1717
import transform.SymUtils._
@@ -59,9 +59,9 @@ trait Deriving { this: Typer =>
5959
}
6060
instantiate(ctx.fresh.setExploreTyperState().setOwner(caseClass))
6161
case info: MethodType =>
62-
(cls.typeRef, info.paramInfos)
62+
(caseClass.typeRef, info.paramInfos)
6363
case _ =>
64-
(cls.typeRef, Nil)
64+
(caseClass.typeRef, Nil)
6565
}
6666
case _ =>
6767
(sym.termRef, Nil)
@@ -81,10 +81,11 @@ trait Deriving { this: Typer =>
8181
* of abstracting over them.
8282
* Returns NoType if `cls` is neither sealed nor a case class or object.
8383
*/
84-
lazy val shapeWithClassParams: Type =
84+
lazy val shapeWithClassParams: Type = {
8585
if (cls.is(Case)) caseShape(cls)
8686
else if (cls.is(Sealed)) sealedShape
8787
else NoType
88+
}.reporting(res => i"shape of $cls = $res", derive)
8889

8990
private def shapeOfType(tp: Type) = {
9091
val shape0 = shapeWithClassParams
@@ -155,7 +156,9 @@ trait Deriving { this: Typer =>
155156
val underlyingType = underlyingClassRef(originalType)
156157
val derivedType = checkClassType(underlyingType, derived.pos, traitReq = false, stablePrefixReq = true)
157158
val nparams = derivedType.classSymbol.typeParams.length
158-
if (nparams == 1) {
159+
if (derivedType.isRef(defn.GenericClass))
160+
() // do nothing, a Generic instance will be created anyway by `addGeneric`
161+
else if (nparams == 1) {
159162
val typeClass = derivedType.classSymbol
160163
val firstKindedParams = cls.typeParams.filterNot(_.info.isLambdaSub)
161164
val evidenceParamInfos =

tests/run/derive-generic.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Array(Array(B), Array(C, x, y))

tests/run/derive-generic.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import reflect.Generic
2+
3+
object Test extends App {
4+
sealed trait A derives Generic
5+
6+
object A {
7+
case class B() extends A
8+
case class C(x: Int, y: Int) extends A
9+
}
10+
11+
println(implicitly[Generic[A]].common.label.deep)
12+
}
13+

0 commit comments

Comments
 (0)