-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing span to synthesized product mirror
Fixes #18353
- Loading branch information
1 parent
84099a7
commit ea5d7d3
Showing
3 changed files
with
74 additions
and
3 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
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,64 @@ | ||
import scala.compiletime.* | ||
import scala.deriving.* | ||
import scala.quoted.* | ||
|
||
trait Getter[S, A]: | ||
def view: S => A | ||
|
||
trait Lens[S, A] extends Getter[S, A]: | ||
def set: S => A => S | ||
|
||
object Lens { | ||
inline def apply[S, A](_view: S => A)(_set: S => A => S): Lens[S, A] = | ||
new Lens[S, A]: | ||
def view: S => A = _view | ||
def set: S => A => S = _set | ||
|
||
inline given derived[T <: Product, A]: Lens[T, A] = ${ | ||
ProductMacros.genLens[T, A] | ||
} | ||
} | ||
|
||
object ProductMacros { | ||
private def indexOf[T: Type, A: Type](using Quotes): Int = | ||
indexOf0[T, A](0) | ||
|
||
private def indexOf0[T: Type, A: Type](acc: Int)(using Quotes): Int = | ||
Type.of[T] match | ||
case '[EmptyTuple] => -1 | ||
case '[A *: tpes] => acc | ||
case '[tpe *: tpes] => indexOf0[tpes, A](acc + 1) | ||
|
||
def genLens[T <: Product: Type, A: Type](using | ||
q: Quotes | ||
): Expr[Lens[T, A]] = { | ||
import quotes.reflect.* | ||
|
||
Expr | ||
.summon[Mirror.ProductOf[T]] | ||
.map { | ||
case '{ | ||
$m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes } | ||
} => | ||
val i = indexOf[elementTypes, A] | ||
if i < 0 then | ||
report.errorAndAbort(s"has no the field of ${Type.show[A]}") | ||
else | ||
val ii: Expr[Int] = Expr(i) | ||
val view: Expr[T => A] = '{ t => | ||
t.productElement($ii).asInstanceOf[A] | ||
} | ||
val set: Expr[T => A => T] = '{ t => a => | ||
val arr = Tuple.fromProduct(t).toArray | ||
arr($ii) = a.asInstanceOf[Object] | ||
// Check-macros fails here probably | ||
$m.fromTuple(Tuple.fromArray(arr).asInstanceOf[elementTypes]) | ||
} | ||
'{ Lens[T, A]($view)($set) } | ||
} | ||
.getOrElse( | ||
report.errorAndAbort(s"${Type.show[T]} is not a product type") | ||
) | ||
|
||
} | ||
} |
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,7 @@ | ||
def Test = { | ||
|
||
type TupleConfig = (Int, String) | ||
|
||
val tConfig = (1, "string") | ||
val fails = summon[Lens[TupleConfig, Int]].view(tConfig) | ||
} |