Skip to content

Commit

Permalink
Add reflect defn.{FunctionClass,ContextFunctionClass}
Browse files Browse the repository at this point in the history
The old `FunctionClass` will need to be deprecated as we will remove
`ErasedFunctionN` and `ErasedContextFunctionN`. We will replace this API
with a simpler version that can return `FunctionN` or `ContextFunctionN`,
the only two stable function classes we have in the compiler/TASTy.

Other new function classes will be encoded with the more general refined
function type encoding, generalization of the `PolyFunction` encoding.
This implies that we won't need to add other kind of function classes to
the reflect API.

Part of the fix for #16847
  • Loading branch information
nicolasstucki committed Feb 13, 2023
1 parent a356581 commit 262a4b9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
9 changes: 9 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2768,7 +2768,16 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def SomeModule: Symbol = dotc.core.Symbols.defn.SomeClass.companionModule
def ProductClass: Symbol = dotc.core.Symbols.defn.ProductClass
def FunctionClass(arity: Int, isImplicit: Boolean = false, isErased: Boolean = false): Symbol =
if arity < 0 then throw IllegalArgumentException(s"arity: $arity")
dotc.core.Symbols.defn.FunctionSymbol(arity, isImplicit, isErased)
def FunctionClass(arity: Int): Symbol =
FunctionClass(arity, false, false)
def FunctionClass(arity: Int, isContextual: Boolean): Symbol =
FunctionClass(arity, isContextual, false)

def ContextFunctionClass(arity: Int): Symbol =
if arity < 0 then throw IllegalArgumentException(s"arity: $arity")
dotc.core.Symbols.defn.FunctionSymbol(arity, true, false)
def TupleClass(arity: Int): Symbol =
dotc.core.Symbols.defn.TupleType(arity).nn.classSymbol.asClass
def isTupleClass(sym: Symbol): Boolean =
Expand Down
19 changes: 19 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4254,8 +4254,27 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
* - ...
* - Nth element is `FunctionN`
*/
// TODO: deprecate in 3.4 and stabilize FunctionClass(Int)/ContextFunctionClass(Int)
// @deprecated("Use overload of `FunctionClass` with 1 or 2 arguments","3.4")
def FunctionClass(arity: Int, isImplicit: Boolean = false, isErased: Boolean = false): Symbol

/** Class symbol of a function class `scala.FunctionN`.
*
* @param arity the arity of the function where `0 <= arity`
* @return class symbol of `scala.FunctionN` where `N == arity`
*/
@experimental
def FunctionClass(arity: Int): Symbol

/** Class symbol of a context function class `scala.ContextFunctionN`.
*
* @param arity the arity of the function where `0 <= arity`
* @param isContextual if it is a `scala.ContextFunctionN`
* @return class symbol of `scala.FunctionN` or `scala.ContextFunctionN` where `N == arity`
*/
@experimental
def FunctionClass(arity: Int, isContextual: Boolean): Symbol

/** Function-like object that maps arity to symbols for classes `scala.TupleX`.
* - 0th element is `NoSymbol`
* - 1st element is `NoSymbol`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ val experimentalDefinitionInLibrary = Set(
"scala.annotation.MacroAnnotation",

//// New APIs: Quotes
// Can be stabilized in 3.3.0 (unsure) or later
// Should be stabilized in 3.4.0
"scala.quoted.Quotes.reflectModule.defnModule.FunctionClass",
// Can be stabilized in 3.4.0 (unsure) or later
"scala.quoted.Quotes.reflectModule.CompilationInfoModule.XmacroSettings",
"scala.quoted.Quotes.reflectModule.FlagsModule.JavaAnnotation",
// Cant be stabilized yet.
Expand Down
26 changes: 26 additions & 0 deletions tests/run-macros/tasty-definitions-1.check
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,57 @@ Function23
Function24
Function25
ContextFunction0
ContextFunction0
ContextFunction1
ContextFunction1
ContextFunction2
ContextFunction2
ContextFunction3
ContextFunction3
ContextFunction4
ContextFunction4
ContextFunction5
ContextFunction5
ContextFunction6
ContextFunction6
ContextFunction7
ContextFunction7
ContextFunction8
ContextFunction8
ContextFunction9
ContextFunction9
ContextFunction10
ContextFunction10
ContextFunction11
ContextFunction11
ContextFunction12
ContextFunction12
ContextFunction13
ContextFunction13
ContextFunction14
ContextFunction14
ContextFunction15
ContextFunction15
ContextFunction16
ContextFunction16
ContextFunction17
ContextFunction17
ContextFunction18
ContextFunction18
ContextFunction19
ContextFunction19
ContextFunction20
ContextFunction20
ContextFunction21
ContextFunction21
ContextFunction22
ContextFunction22
ContextFunction23
ContextFunction23
ContextFunction24
ContextFunction24
ContextFunction25
ContextFunction25
ErasedFunction1
ErasedFunction2
ErasedFunction3
Expand Down
1 change: 1 addition & 0 deletions tests/run-macros/tasty-definitions-1/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ object Macros {
printout(defn.FunctionClass(i).name)

for (i <- 0 to 25)
printout(defn.FunctionClass(i, isContextual = true).name)
printout(defn.FunctionClass(i, isImplicit = true).name)

for (i <- 1 to 25)
Expand Down

0 comments on commit 262a4b9

Please sign in to comment.