-
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.
Set the correct type when copying reflect Inlined trees (#19409)
Fixes #19191
- Loading branch information
Showing
4 changed files
with
80 additions
and
1 deletion.
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,15 @@ | ||
trait Binding[+A]: | ||
def value: A = ??? | ||
object Binding: | ||
inline def apply[A](inline a: A): Binding[A] = ??? | ||
final case class Constant[+A](override val value: A) extends Binding[A] | ||
|
||
extension [A](inline binding: Binding[A]) | ||
transparent inline def bind: A = null.asInstanceOf[A] | ||
|
||
trait Vars[A] extends BindingSeq[A] | ||
trait BindingSeq[+A]: | ||
def foreachBinding[U](f: A => Binding[U]): Binding[Unit] = ??? | ||
|
||
extension [A](inline bindingSeq: BindingSeq[A]) | ||
transparent inline def foreach[U](inline f: A => U): Unit = ${ Macros.foreach('bindingSeq, 'f) } |
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,54 @@ | ||
import scala.compiletime._ | ||
import scala.deriving._ | ||
import scala.quoted._ | ||
import scala.annotation.meta.field | ||
|
||
object Macros{ | ||
private def bindingFunctionBody[A: quoted.Type, B: quoted.Type](f: quoted.Expr[A => B])(using Quotes) = | ||
import quoted.quotes.reflect.* | ||
f.asTerm match | ||
case inlined @ Inlined( | ||
call, | ||
bindings, | ||
block @ Block( | ||
List( | ||
defDef @ DefDef( | ||
name, | ||
paramss @ List(TermParamClause(List(param @ ValDef(paramName, paramTpt, _)))), | ||
tpt, | ||
Some(rhs) | ||
) | ||
), | ||
closureIdent | ||
) | ||
) => | ||
Inlined | ||
.copy(inlined)( | ||
call, | ||
bindings, | ||
'{ (a: A) => | ||
${ | ||
Block( | ||
List( | ||
ValDef | ||
.copy(param)(paramName, paramTpt, Some('a.asTerm)) | ||
.changeOwner(Symbol.spliceOwner) | ||
), | ||
'{ | ||
Binding(${ rhs.changeOwner(Symbol.spliceOwner).asExprOf[B] }) | ||
}.asTerm.changeOwner(Symbol.spliceOwner) | ||
) | ||
.asExprOf[Binding[B]] | ||
}: Binding[B] | ||
}.asTerm | ||
) | ||
.asExprOf[A => Binding[B]] | ||
case _ => | ||
'{ (a: A) => Binding.Constant($f(a)): Binding[B] } | ||
end match | ||
end bindingFunctionBody | ||
|
||
def foreach[A: quoted.Type, U: quoted.Type](self: quoted.Expr[BindingSeq[A]], f: quoted.Expr[A => U])(using | ||
qctx: Quotes | ||
): quoted.Expr[Unit] = '{ $self.foreachBinding(${ bindingFunctionBody(f) }).bind } | ||
} |
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,6 @@ | ||
def Test = { | ||
val vars: Vars[Int] = ??? | ||
|
||
val works = vars.foreach { v => () } | ||
val fails = for (v <- vars) () | ||
} |