Skip to content

Commit

Permalink
Add defn.NamedTupleModule to quotes reflect API
Browse files Browse the repository at this point in the history
  • Loading branch information
jchyb committed Dec 3, 2024
1 parent d40009a commit 4079629
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def PolyFunctionClass = dotc.core.Symbols.defn.PolyFunctionClass
def TupleClass(arity: Int): Symbol =
dotc.core.Symbols.defn.TupleType(arity).nn.classSymbol.asClass
def NamedTupleModule: Symbol = dotc.core.Symbols.defn.NamedTupleModule
def isTupleClass(sym: Symbol): Boolean =
dotc.core.Symbols.defn.isTupleClass(sym)
def ScalaPrimitiveValueClasses: List[Symbol] =
Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4513,6 +4513,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*/
def TupleClass(arity: Int): Symbol

/** The module symbol of module `scala.NamedTuple`. */
@experimental def NamedTupleModule: Symbol

/** Returns `true` if `sym` is a `Tuple1`, `Tuple2`, ... `Tuple22` */
def isTupleClass(sym: Symbol): Boolean

Expand Down
2 changes: 2 additions & 0 deletions tests/run-macros/named-tuple.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
test
28 changes: 28 additions & 0 deletions tests/run-macros/named-tuple/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import language.experimental.namedTuples

import scala.quoted._
object Macro {
transparent inline def macr(): NamedTuple.AnyNamedTuple = ${ macrImpl() }

def macrImpl()(using Quotes) = {
import quotes.reflect._

val tupleNames = TypeTree.of[scala.Tuple2["stringValue", "intValue"]]
val tupleTypes = TypeTree.of[scala.Tuple2[String, Int]]
val tupleValues = '{("test", 10)}.asTerm

Apply(
TypeApply(
Apply(
TypeApply(
Select.unique(Ref(defn.NamedTupleModule), "build"),
List(tupleNames)
),
List()
),
List(tupleTypes)
),
List(tupleValues)
).asExprOf[NamedTuple.AnyNamedTuple]
}
}
6 changes: 6 additions & 0 deletions tests/run-macros/named-tuple/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import language.experimental.namedTuples

@main def Test =
val namedTple = Macro.macr()
println(namedTple.intValue)
println(namedTple.stringValue)
4 changes: 4 additions & 0 deletions tests/run-tasty-inspector/stdlibExperimentalDefinitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ val experimentalDefinitionInLibrary = Set(
"scala.quoted.Quotes.reflectModule.TermParamClauseMethods.erasedArgs",
"scala.quoted.Quotes.reflectModule.TermParamClauseMethods.hasErasedArgs",

// New feature: named tuples
// Need namedTuples enabled.
"scala.quoted.Quotes.reflectModule.defnModule.NamedTupleModule",

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

Expand Down

0 comments on commit 4079629

Please sign in to comment.