-
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.
#19930) In the issue minimisation, `tpd.ref` returns a tpd.This(Ident(Inner$)) tree, where the inner ident is a type ident, which was unexpected. Trying to adjust the `tpd.ref` to return a RefType for that case seems dangerous, so I instead opted for a workaround directly inside the QuotesImpl. At first I wanted to reuse the `tpd.This` tree somehow (or at least to use a correctly retyped `Ident`), but nothing I tried with it worked, so I ended up writing the correct Ref by hand. Fixes #19732
- Loading branch information
Showing
4 changed files
with
58 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 @@ | ||
Map(b -> 23) |
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,42 @@ | ||
// package dummy | ||
|
||
import scala.quoted.* | ||
|
||
trait Defaults[A]: | ||
def defaults: Map[String, Any] | ||
|
||
object Defaults: | ||
inline def derived[A <: Product]: Defaults[A] = ${ defaultsImpl[A] } | ||
|
||
def defaultsImpl[A <: Product: Type](using Quotes): Expr[Defaults[A]] = | ||
'{ | ||
new Defaults[A]: | ||
def defaults: Map[String, Any] = ${ defaultParmasImpl[A] } | ||
} | ||
|
||
def defaultParmasImpl[T](using quotes: Quotes, tpe: Type[T]): Expr[Map[String, Any]] = | ||
import quotes.reflect.* | ||
|
||
TypeRepr.of[T].classSymbol match | ||
case None => '{ Map.empty[String, Any] } | ||
case Some(sym) => | ||
val comp = sym.companionClass | ||
val mod = Ref(sym.companionModule) | ||
val names = | ||
for p <- sym.caseFields if p.flags.is(Flags.HasDefault) | ||
yield p.name | ||
val namesExpr: Expr[List[String]] = | ||
Expr.ofList(names.map(Expr(_))) | ||
|
||
val body = comp.tree.asInstanceOf[ClassDef].body | ||
val idents: List[Ref] = | ||
for | ||
case deff @ DefDef(name, _, _, _) <- body | ||
if name.startsWith("$lessinit$greater$default") | ||
yield mod.select(deff.symbol) | ||
val typeArgs = TypeRepr.of[T].typeArgs | ||
val identsExpr: Expr[List[Any]] = | ||
if typeArgs.isEmpty then Expr.ofList(idents.map(_.asExpr)) | ||
else Expr.ofList(idents.map(_.appliedToTypes(typeArgs).asExpr)) | ||
|
||
'{ $namesExpr.zip($identsExpr).toMap } |
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 @@ | ||
class Outer: | ||
case class Inner(a: String, b: Int = 23) derives Defaults | ||
|
||
object Test: | ||
def main(args: Array[String]): Unit = | ||
val outer = Outer() | ||
println(summon[Defaults[outer.Inner]].defaults) |