-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev: Add better compile-time diagnostics
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
main/src/io/github/iltotore/iron/internal/DecodingFailure.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,9 @@ | ||
package io.github.iltotore.iron.internal | ||
|
||
import scala.quoted.* | ||
|
||
enum DecodingFailure: | ||
case Unknown | ||
case NotInlined(term: Term) | ||
case HasRuntimeBindings(bindings: List[Quotes.reflectModule.Definition])(using Quotes) | ||
case HasStatements(statements: List[quotes.reflect.Statement])(using Quotes) |
32 changes: 32 additions & 0 deletions
32
main/src/io/github/iltotore/iron/internal/ExprDecoder.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,32 @@ | ||
package io.github.iltotore.iron.internal | ||
|
||
import io.github.iltotore.iron.compileTime.NumConstant | ||
import scala.quoted.* | ||
|
||
trait ExprDecoder[T]: | ||
|
||
def decode(expr: Expr[T])(using Quotes): Either[DecodingFailure, T] | ||
|
||
object ExprDecoder: | ||
|
||
given [T](using fromExpr: FromExpr[T]): ExprDecoder[T] with | ||
|
||
override def decode(expr: Expr[T])(using Quotes): Either[DecodingFailure, T] = | ||
fromExpr.unapply(expr).toRight(DecodingFailure.Unknown) | ||
|
||
private class PrimitiveExprDecoder[T <: NumConstant | Byte | Short | Boolean | String] extends ExprDecoder[T]: | ||
|
||
override def decode(expr: Expr[T])(using Quotes): Either[DecodingFailure, T] = | ||
import quotes.reflect.* | ||
|
||
def rec(tree: Term): Either[DecodingFailure, T] = tree match | ||
case Block(stats, e) => if stats.isEmpty then rec(e) else Left(DecodingFailure.HasStatements(stats)) | ||
case Inlined(_, bindings, e) => if bindings.isEmpty then rec(e) else Left(DecodingFailure.HasRuntimeBindings(bindings)) | ||
case Typed(e, _) => rec(e) | ||
case Ident(name) => Left(DecodingFailure.NotInlined(Symbol())) | ||
case _ => | ||
tree.tpe.widenTermRefByName match | ||
case ConstantType(c) => Right(c.value.asInstanceOf[T]) | ||
case _ => Left(DecodingFailure.Unknown) | ||
|
||
rec(expr.asTerm) |