Skip to content

Added apply methods to import selectors in Quotes #21225 #22457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end SimpleSelectorTypeTest

object SimpleSelector extends SimpleSelectorModule:
def apply(name: String): SimpleSelector =
withDefaultPos(untpd.ImportSelector(untpd.Ident(name.toTermName)))
def unapply(x: SimpleSelector): Some[String] = Some(x.name.toString)
end SimpleSelector

Expand All @@ -1713,6 +1715,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end RenameSelectorTypeTest

object RenameSelector extends RenameSelectorModule:
def apply(fromName: String, toName: String): RenameSelector =
withDefaultPos(untpd.ImportSelector(untpd.Ident(fromName.toTermName), untpd.Ident(toName.toTermName)))
def unapply(x: RenameSelector): (String, String) = (x.fromName, x.toName)
end RenameSelector

Expand All @@ -1738,6 +1742,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end OmitSelectorTypeTest

object OmitSelector extends OmitSelectorModule:
def apply(name: String): OmitSelector =
withDefaultPos(untpd.ImportSelector(untpd.Ident(name.toTermName), untpd.Ident(nme.WILDCARD)))
def unapply(x: OmitSelector): Some[String] = Some(x.imported.name.toString)
end OmitSelector

Expand All @@ -1758,6 +1764,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end GivenSelectorTypeTest

object GivenSelector extends GivenSelectorModule:
def apply(bound: Option[TypeTree]): GivenSelector =
withDefaultPos(untpd.ImportSelector(
untpd.Ident(nme.EMPTY),
bound = bound.map(tpt => untpd.TypedSplice(tpt)).getOrElse(EmptyTree)
))
def unapply(x: GivenSelector): Some[Option[TypeTree]] =
Some(GivenSelectorMethods.bound(x))
end GivenSelector
Expand Down
4 changes: 4 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val SimpleSelector` */
trait SimpleSelectorModule { this: SimpleSelector.type =>
@experimental def apply(name: String): SimpleSelector
def unapply(x: SimpleSelector): Some[String]
}

Expand All @@ -2570,6 +2571,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val RenameSelector` */
trait RenameSelectorModule { this: RenameSelector.type =>
@experimental def apply(fromName: String, toName: String): RenameSelector
def unapply(x: RenameSelector): (String, String)
}

Expand Down Expand Up @@ -2597,6 +2599,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val OmitSelector` */
trait OmitSelectorModule { this: OmitSelector.type =>
@experimental def apply(name: String): OmitSelector
def unapply(x: OmitSelector): Some[String]
}

Expand All @@ -2621,6 +2624,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val GivenSelector` */
trait GivenSelectorModule { this: GivenSelector.type =>
@experimental def apply(bound: Option[TypeTree]): GivenSelector
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@experimental def apply(bound: Option[TypeTree]): GivenSelector
@experimental def apply(bound: Option[TypeRepr]): GivenSelector

I would suggest internally wrapping the type in TypeTree with the default span

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be inconsistent with unapply and the .bound accessor method. Is that a problem?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeTree makes more sense to me too

def unapply(x: GivenSelector): Some[Option[TypeTree]]
}

Expand Down
2 changes: 2 additions & 0 deletions tests/run-macros/i21225.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bar codec
foo codec
33 changes: 33 additions & 0 deletions tests/run-macros/i21225/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//> using options -experimental

import scala.quoted.*

trait Codec[-T] { def print(): Unit }
object Codec {
inline def derivedWithDeps[T](deps: Any): Codec[T] = ${derivedWithDepsImpl[T]('deps)}

private def derivedWithDepsImpl[T](deps: Expr[Any])(using q: Quotes)(using Type[T]): Expr[Codec[T]] = {
import q.reflect.*

val givenSelector: Selector = GivenSelector(None)
val theImport = Import(deps.asTerm, List(givenSelector))
Block(List(theImport), '{scala.compiletime.summonInline[Codec[T]]}.asTerm).asExprOf[Codec[T]]
/* import deps.given
* summonInline[Codec[T]]
*/
}

inline def derivedWithDepsWithNamedOmitted[T](deps: Any): Codec[T] = ${derivedWithDepsWithNamedOmittedImpl[T]('deps)}

private def derivedWithDepsWithNamedOmittedImpl[T](deps: Expr[Any])(using q: Quotes)(using Type[T]): Expr[Codec[T]] = {
import q.reflect.*

val givenSelector: Selector = GivenSelector(None)
val omitSelector: Selector = OmitSelector("named")
val theImport = Import(deps.asTerm, List(givenSelector, omitSelector))
Block(List(theImport), '{scala.compiletime.summonInline[Codec[T]]}.asTerm).asExprOf[Codec[T]]
/* import deps.{given, named => _}
* summonInline[Codec[T]]
*/
}
}
14 changes: 14 additions & 0 deletions tests/run-macros/i21225/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//> using options -experimental

import scala.quoted.*

sealed trait Foo
class Bar extends Foo
object CustomCodecs {
given named: Codec[Bar] = new Codec[Bar] { def print(): Unit = println("bar codec")}
given Codec[Foo] = new Codec[Foo] { def print(): Unit = println("foo codec") }
}

@main def Test =
Codec.derivedWithDeps[Bar](CustomCodecs).print()
Codec.derivedWithDepsWithNamedOmitted[Bar](CustomCodecs).print()
4 changes: 4 additions & 0 deletions tests/run-tasty-inspector/stdlibExperimentalDefinitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ val experimentalDefinitionInLibrary = Set(
"scala.quoted.Quotes.reflectModule.MethodTypeMethods.hasErasedParams",
"scala.quoted.Quotes.reflectModule.TermParamClauseMethods.erasedArgs",
"scala.quoted.Quotes.reflectModule.TermParamClauseMethods.hasErasedArgs",
"scala.quoted.Quotes.reflectModule.GivenSelectorModule.apply",
"scala.quoted.Quotes.reflectModule.OmitSelectorModule.apply",
"scala.quoted.Quotes.reflectModule.RenameSelectorModule.apply",
"scala.quoted.Quotes.reflectModule.SimpleSelectorModule.apply",

// New feature: fromNullable for explicit nulls
"scala.Predef$.fromNullable",
Expand Down