Skip to content
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

Add defn.NamedTupleModule to quotes reflect API #22096

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading