Skip to content

Commit 2602b55

Browse files
Add reflect defn.FunctionClass overloads (#16849)
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
2 parents 4c99fde + 9571b42 commit 2602b55

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

+5
Original file line numberDiff line numberDiff line change
@@ -2767,7 +2767,12 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27672767
def SomeModule: Symbol = dotc.core.Symbols.defn.SomeClass.companionModule
27682768
def ProductClass: Symbol = dotc.core.Symbols.defn.ProductClass
27692769
def FunctionClass(arity: Int, isImplicit: Boolean = false, isErased: Boolean = false): Symbol =
2770+
if arity < 0 then throw IllegalArgumentException(s"arity: $arity")
27702771
dotc.core.Symbols.defn.FunctionSymbol(arity, isImplicit, isErased)
2772+
def FunctionClass(arity: Int): Symbol =
2773+
FunctionClass(arity, false, false)
2774+
def FunctionClass(arity: Int, isContextual: Boolean): Symbol =
2775+
FunctionClass(arity, isContextual, false)
27712776
def TupleClass(arity: Int): Symbol =
27722777
dotc.core.Symbols.defn.TupleType(arity).nn.classSymbol.asClass
27732778
def isTupleClass(sym: Symbol): Boolean =

library/src/scala/quoted/Quotes.scala

+19
Original file line numberDiff line numberDiff line change
@@ -4254,8 +4254,27 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
42544254
* - ...
42554255
* - Nth element is `FunctionN`
42564256
*/
4257+
// TODO: deprecate in 3.4 and stabilize FunctionClass(Int)/FunctionClass(Int,Boolean)
4258+
// @deprecated("Use overload of `FunctionClass` with 1 or 2 arguments","3.4")
42574259
def FunctionClass(arity: Int, isImplicit: Boolean = false, isErased: Boolean = false): Symbol
42584260

4261+
/** Class symbol of a function class `scala.FunctionN`.
4262+
*
4263+
* @param arity the arity of the function where `0 <= arity`
4264+
* @return class symbol of `scala.FunctionN` where `N == arity`
4265+
*/
4266+
@experimental
4267+
def FunctionClass(arity: Int): Symbol
4268+
4269+
/** Class symbol of a context function class `scala.FunctionN` or `scala.ContextFunctionN`.
4270+
*
4271+
* @param arity the arity of the function where `0 <= arity`
4272+
* @param isContextual if it is a `scala.ContextFunctionN`
4273+
* @return class symbol of `scala.FunctionN` or `scala.ContextFunctionN` where `N == arity`
4274+
*/
4275+
@experimental
4276+
def FunctionClass(arity: Int, isContextual: Boolean): Symbol
4277+
42594278
/** Function-like object that maps arity to symbols for classes `scala.TupleX`.
42604279
* - 0th element is `NoSymbol`
42614280
* - 1st element is `NoSymbol`

tests/run-custom-args/tasty-inspector/stdlibExperimentalDefinitions.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ val experimentalDefinitionInLibrary = Set(
6666
"scala.annotation.MacroAnnotation",
6767

6868
//// New APIs: Quotes
69-
// Can be stabilized in 3.3.0 (unsure) or later
69+
// Should be stabilized in 3.4.0
70+
"scala.quoted.Quotes.reflectModule.defnModule.FunctionClass",
71+
// Can be stabilized in 3.4.0 (unsure) or later
7072
"scala.quoted.Quotes.reflectModule.CompilationInfoModule.XmacroSettings",
7173
"scala.quoted.Quotes.reflectModule.FlagsModule.JavaAnnotation",
7274
// Cant be stabilized yet.

tests/run-macros/tasty-definitions-1.check

+26
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,57 @@ Function23
5757
Function24
5858
Function25
5959
ContextFunction0
60+
ContextFunction0
61+
ContextFunction1
6062
ContextFunction1
6163
ContextFunction2
64+
ContextFunction2
65+
ContextFunction3
6266
ContextFunction3
6367
ContextFunction4
68+
ContextFunction4
69+
ContextFunction5
6470
ContextFunction5
6571
ContextFunction6
72+
ContextFunction6
6673
ContextFunction7
74+
ContextFunction7
75+
ContextFunction8
6776
ContextFunction8
6877
ContextFunction9
78+
ContextFunction9
6979
ContextFunction10
80+
ContextFunction10
81+
ContextFunction11
7082
ContextFunction11
7183
ContextFunction12
84+
ContextFunction12
7285
ContextFunction13
86+
ContextFunction13
87+
ContextFunction14
7388
ContextFunction14
7489
ContextFunction15
90+
ContextFunction15
91+
ContextFunction16
7592
ContextFunction16
7693
ContextFunction17
94+
ContextFunction17
95+
ContextFunction18
7796
ContextFunction18
7897
ContextFunction19
98+
ContextFunction19
7999
ContextFunction20
100+
ContextFunction20
101+
ContextFunction21
80102
ContextFunction21
81103
ContextFunction22
104+
ContextFunction22
82105
ContextFunction23
106+
ContextFunction23
107+
ContextFunction24
83108
ContextFunction24
84109
ContextFunction25
110+
ContextFunction25
85111
ErasedFunction1
86112
ErasedFunction2
87113
ErasedFunction3

tests/run-macros/tasty-definitions-1/quoted_1.scala

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ object Macros {
6060
printout(defn.FunctionClass(i).name)
6161

6262
for (i <- 0 to 25)
63+
printout(defn.FunctionClass(i, isContextual = true).name)
6364
printout(defn.FunctionClass(i, isImplicit = true).name)
6465

6566
for (i <- 1 to 25)

0 commit comments

Comments
 (0)