-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flat check: tests + still improving error message
- Loading branch information
Showing
12 changed files
with
136 additions
and
124 deletions.
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
56 changes: 34 additions & 22 deletions
56
kyo-core/shared/src/main/scala-2/internal/FlatImplicits.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 |
---|---|---|
@@ -1,42 +1,54 @@ | ||
package kyo.internal | ||
|
||
import kyo.Flat | ||
import scala.language.experimental.macros | ||
import scala.reflect.macros.blackbox._ | ||
import scala.reflect.macros.blackbox.Context | ||
import kyo._ | ||
|
||
trait FlatImplicits0 { | ||
implicit def infer[T]: Flat[T] = macro FlatMacros.inferMacro[T] | ||
implicit def infer[T]: Flat[T] = macro FlatImplicits.inferMacro[T] | ||
} | ||
|
||
trait FlatImplicits1 extends FlatImplicits0 { | ||
implicit def derived[T](implicit f: Flat[T]): Flat[T] = | ||
Flat.unsafe.derived[T] | ||
implicit def product[T <: Product]: Flat[T] = Flat.unsafe.checked[T] | ||
} | ||
|
||
trait FlatImplicits2 extends FlatImplicits1 { | ||
implicit def product[T <: Product]: Flat[T] = | ||
Flat.unsafe.checked[T] | ||
trait FlatImplicits extends FlatImplicits1 { | ||
implicit def anyVal[T <: AnyVal]: Flat[T] = Flat.unsafe.checked[T] | ||
} | ||
|
||
trait FlatImplicits extends FlatImplicits2 { | ||
implicit def anyVal[T <: AnyVal]: Flat[T] = | ||
Flat.unsafe.checked[T] | ||
} | ||
|
||
object FlatMacros { | ||
def inferMacro[T: c.WeakTypeTag](c: Context): c.Expr[Flat[T]] = { | ||
object FlatImplicits { | ||
def inferMacro[T](c: Context)(implicit t: c.WeakTypeTag[T]): c.Expr[Flat[T]] = { | ||
import c.universe._ | ||
|
||
val t = weakTypeOf[T] | ||
println(t) | ||
|
||
object Kyo { | ||
def unapply(tpe: Type): Option[(Type, Type)] = | ||
tpe match { | ||
case TypeRef(_, sym, List(t, u)) if sym.asType.fullName == "kyo.>" => | ||
Some((t, u)) | ||
case _ => | ||
None | ||
} | ||
} | ||
|
||
def canDerive(t: Type): Boolean = { | ||
val flatType = appliedType(typeOf[Flat[_]].typeConstructor, List(t)) | ||
c.inferImplicitValue(flatType, silent = true) != EmptyTree | ||
} | ||
|
||
c.abort(c.enclosingPosition, "AAA " + t) | ||
def ok = c.Expr[Flat[T]](q"kyo.Flat.unsafe.checked[$t]") | ||
|
||
val isConcrete = t.typeSymbol.isClass | ||
def fail(msg: String) = c.abort(c.enclosingPosition, msg) | ||
|
||
if (isConcrete || t.toString.startsWith("kyo.concurrent.fibers.Fiber")) { | ||
c.Expr[Flat[T]](q"Flat.unsafe.checked[$t]") | ||
} else { | ||
c.abort(c.enclosingPosition, "not pure: " + t.toString) | ||
t match { | ||
case Kyo(Kyo(_, _), _) => | ||
fail("Nested Kyo computations are not allowed.") | ||
case Kyo(nt, _) if !nt.typeSymbol.isClass && !canDerive(nt) => | ||
fail(s"Cannot prove ${nt.toString} isn't nested. Provide an implicit evidence.") | ||
case _ => | ||
println(t) | ||
ok | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.