Skip to content

Commit 1f175a9

Browse files
committed
Flesh out test
New data type: Either Test structures combining several types.
1 parent 58746bf commit 1f175a9

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -804,18 +804,13 @@ class Namer { typer: Typer =>
804804
def register(child: Symbol, parent: Type) = {
805805
val cls = parent.classSymbol
806806
if (cls.is(Sealed)) {
807-
if ((child.isInaccessibleChildOf(cls) || child.isAnonymousClass) && !sym.hasAnonymousChild) {
807+
if ((child.isInaccessibleChildOf(cls) || child.isAnonymousClass) && !sym.hasAnonymousChild)
808808
cls.addAnnotation(Annotation.Child(cls))
809-
}
810-
else {
811-
if (cls.is(ChildrenQueried))
812-
ctx.error(em"""children of ${cls} were already queried before $sym was discovered.
813-
|As a remedy, you could move $sym on the same nesting level as $cls.""")
814-
else {
815-
//println(i"child $child of $cls")
816-
cls.addAnnotation(Annotation.Child(child))
817-
}
818-
}
809+
else if (!cls.is(ChildrenQueried))
810+
cls.addAnnotation(Annotation.Child(child))
811+
else
812+
ctx.error(em"""children of ${cls} were already queried before $sym was discovered.
813+
|As a remedy, you could move $sym on the same nesting level as $cls.""")
819814
}
820815
}
821816

tests/run/typeclass-derivation2.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ ListBuffer(1, 2)
66
Pair(1,2)
77
Cons(hd = 11, tl = Cons(hd = 22, tl = Cons(hd = 33, tl = Nil())))
88
Cons(hd = Cons(hd = 11, tl = Cons(hd = 22, tl = Cons(hd = 33, tl = Nil()))), tl = Cons(hd = Cons(hd = 11, tl = Cons(hd = 22, tl = Nil())), tl = Nil()))
9+
Cons(hd = Left(x = 1), tl = Cons(hd = Right(x = Pair(x = 2, y = 3)), tl = Nil()))
10+
Cons(hd = Left(x = 1), tl = Cons(hd = Right(x = Pair(x = 2, y = 3)), tl = Nil()))

tests/run/typeclass-derivation2.scala

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,38 @@ object Pair extends Deriving {
166166
// two clauses that could be generated from a `derives` clause
167167
implicit def PairEq[T: Eq]: Eq[Pair[T]] = Eq.derived
168168
implicit def PairPickler[T: Pickler]: Pickler[Pair[T]] = Pickler.derived
169+
implicit def PairShow[T: Show]: Show[Pair[T]] = Show.derived
170+
}
171+
172+
sealed trait Either[+L, +R] extends Product derives Eq, Pickler
173+
case class Left[L](x: L) extends Either[L, Nothing]
174+
case class Right[R](x: R) extends Either[Nothing, R]
175+
176+
object Either extends Deriving {
177+
import Deriving._
178+
179+
type Shape[L, R] = Shape.Cases[(
180+
Shape.Case[Left[L], L *: Unit],
181+
Shape.Case[Right[R], R *: Unit]
182+
)]
183+
184+
implicit def eitherShape[L, R]: Shaped[Either[L, R], Shape[L, R]] = new {
185+
def reflect(e: Either[L, R]): Mirror = e match {
186+
case e: Left[L] => mirror(0, e)
187+
case e: Right[R] => mirror(1, e)
188+
}
189+
def reify(c: Mirror): Either[L, R] = c.ordinal match {
190+
case 0 => Left[L](c(0).asInstanceOf)
191+
case 1 => Right[R](c(0).asInstanceOf)
192+
}
193+
def deriving = Either
194+
}
195+
196+
protected val caseLabels = Array("Left\000x", "Right\000x")
197+
198+
implicit def EitherEq[L: Eq, R: Eq]: Eq[Either[L, R]] = Eq.derived
199+
implicit def EitherPickler[L: Pickler, R: Pickler]: Pickler[Either[L, R]] = Pickler.derived
200+
implicit def EitherShow[L: Show, R: Show]: Show[Either[L, R]] = Show.derived
169201
}
170202

171203
// A typeclass
@@ -174,7 +206,7 @@ trait Eq[T] {
174206
}
175207

176208
object Eq {
177-
import scala.typelevel._
209+
import _root_.scala.typelevel._
178210
import Deriving._
179211

180212
inline def tryEql[T](x: T, y: T) = implicit match {
@@ -423,4 +455,25 @@ object Test extends App {
423455
println(implicitly[Show[T]].show(x))
424456
showPrintln(xs)
425457
showPrintln(xss)
458+
459+
val zs = Lst.Cons(Left(1), Lst.Cons(Right(Pair(2, 3)), Lst.Nil))
460+
showPrintln(zs)
461+
462+
def pickle[T: Pickler](buf: mutable.ListBuffer[Int], x: T): Unit =
463+
implicitly[Pickler[T]].pickle(buf, x)
464+
465+
def unpickle[T: Pickler](buf: mutable.ListBuffer[Int]): T =
466+
implicitly[Pickler[T]].unpickle(buf)
467+
468+
def copy[T: Pickler](x: T): T = {
469+
val buf = new mutable.ListBuffer[Int]
470+
pickle(buf, x)
471+
unpickle[T](buf)
472+
}
473+
474+
def eql[T: Eq](x: T, y: T) = implicitly[Eq[T]].eql(x, y)
475+
476+
val zs1 = copy(zs)
477+
showPrintln(zs1)
478+
assert(eql(zs, zs1))
426479
}

0 commit comments

Comments
 (0)