Skip to content

Commit c52881f

Browse files
committed
Erased parameters/functions quotes API changes
- `isErased` => `erasedArgs`/`erasedParams` and `hasErasedArgs`/`hasErasedParams` - `FunctionClass` now fails when `isErased = true`. Add `ErasedFunctionClass`.
1 parent 4e4cd8e commit c52881f

File tree

5 files changed

+48
-61
lines changed

5 files changed

+48
-61
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,8 +1581,12 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
15811581
self.nonEmpty && self.head.symbol.is(dotc.core.Flags.Implicit)
15821582
def isGiven: Boolean =
15831583
self.nonEmpty && self.head.symbol.is(dotc.core.Flags.Given)
1584-
def isErased: Boolean =
1585-
self.nonEmpty && self.head.symbol.is(dotc.core.Flags.Erased)
1584+
def isErased: Boolean = false
1585+
1586+
def erasedArgs: List[Boolean] =
1587+
self.map(param => param.tpe.hasAnnotation(dotc.core.Symbols.defn.ErasedParamAnnot))
1588+
def hasErasedArgs: Boolean =
1589+
self.exists(param => param.tpe.hasAnnotation(dotc.core.Symbols.defn.ErasedParamAnnot))
15861590
end TermParamClauseMethods
15871591

15881592
type TypeParamClause = List[tpd.TypeDef]
@@ -2139,9 +2143,12 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
21392143

21402144
given MethodTypeMethods: MethodTypeMethods with
21412145
extension (self: MethodType)
2142-
def isErased: Boolean = self.hasErasedParams
2146+
def isErased: Boolean = false
21432147
def isImplicit: Boolean = self.isImplicitMethod
21442148
def param(idx: Int): TypeRepr = self.newParamRef(idx)
2149+
2150+
def erasedParams: List[Boolean] = self.erasedParams
2151+
def hasErasedParams: Boolean = self.hasErasedParams
21452152
end extension
21462153
end MethodTypeMethods
21472154

@@ -2768,13 +2775,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27682775
def ProductClass: Symbol = dotc.core.Symbols.defn.ProductClass
27692776
def FunctionClass(arity: Int, isImplicit: Boolean = false, isErased: Boolean = false): Symbol =
27702777
if arity < 0 then throw IllegalArgumentException(s"arity: $arity")
2771-
if isErased
2772-
then dotc.core.Symbols.defn.ErasedFunctionClass
2778+
if isErased then
2779+
throw new Exception("Erased function classes are not supported. Use a refined `scala.runtime.ErasedFunction`")
27732780
else dotc.core.Symbols.defn.FunctionSymbol(arity, isImplicit)
27742781
def FunctionClass(arity: Int): Symbol =
27752782
FunctionClass(arity, false, false)
27762783
def FunctionClass(arity: Int, isContextual: Boolean): Symbol =
27772784
FunctionClass(arity, isContextual, false)
2785+
def ErasedFunctionClass = dotc.core.Symbols.defn.ErasedFunctionClass
27782786
def TupleClass(arity: Int): Symbol =
27792787
dotc.core.Symbols.defn.TupleType(arity).nn.classSymbol.asClass
27802788
def isTupleClass(sym: Symbol): Boolean =

library/src/scala/quoted/Quotes.scala

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,16 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
23742374
/** Is this a given parameter clause `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */
23752375
def isGiven: Boolean
23762376
/** Is this a erased parameter clause `(erased x1: X1, ..., xn: Xn)` */
2377+
// TODO:deprecate in 3.4 and stabilize `erasedParams` and `hasErasedParams`.
2378+
// @deprecated("Use `hasErasedArgs`","3.4")
23772379
def isErased: Boolean
2380+
2381+
/** List of `erased` flags for each parameter of the clause */
2382+
@experimental
2383+
def erasedArgs: List[Boolean]
2384+
/** Whether the clause has any erased parameters */
2385+
@experimental
2386+
def hasErasedArgs: Boolean
23782387
end TermParamClauseMethods
23792388

23802389
/** A type parameter clause `[X1, ..., Xn]` */
@@ -2650,7 +2659,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
26502659
*/
26512660
def isContextFunctionType: Boolean
26522661

2653-
/** Is this type an erased function type?
2662+
/** Is this type a function type with erased parameters?
26542663
*
26552664
* @see `isFunctionType`
26562665
*/
@@ -3145,7 +3154,17 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
31453154
extension (self: MethodType)
31463155
/** Is this the type of using parameter clause `(implicit X1, ..., Xn)`, `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */
31473156
def isImplicit: Boolean
3157+
/** Is this the type of erased parameter clause `(erased x1: X1, ..., xn: Xn)` */
3158+
// TODO:deprecate in 3.4 and stabilize `erasedParams` and `hasErasedParams`.
3159+
// @deprecated("Use `hasErasedParams`","3.4")
31483160
def isErased: Boolean
3161+
3162+
/** List of `erased` flags for each parameters of the clause */
3163+
@experimental
3164+
def erasedParams: List[Boolean]
3165+
/** Whether the clause has any erased parameters */
3166+
@experimental
3167+
def hasErasedParams: Boolean
31493168
def param(idx: Int): TypeRepr
31503169
end extension
31513170
end MethodTypeMethods
@@ -4275,6 +4294,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
42754294
@experimental
42764295
def FunctionClass(arity: Int, isContextual: Boolean): Symbol
42774296

4297+
/** The `scala.runtime.ErasedFunction` built-in trait. */
4298+
@experimental
4299+
def ErasedFunctionClass: Symbol
4300+
42784301
/** Function-like object that maps arity to symbols for classes `scala.TupleX`.
42794302
* - 0th element is `NoSymbol`
42804303
* - 1st element is `NoSymbol`

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ val experimentalDefinitionInLibrary = Set(
8484

8585
// New feature: functions with erased parameters.
8686
// Need erasedDefinitions enabled.
87-
"scala.compiletime.ErasedFunction"
87+
"scala.runtime.ErasedFunction",
88+
"scala.quoted.Quotes.reflectModule.MethodTypeMethods.erasedParams",
89+
"scala.quoted.Quotes.reflectModule.MethodTypeMethods.hasErasedParams",
90+
"scala.quoted.Quotes.reflectModule.TermParamClauseMethods.erasedArgs",
91+
"scala.quoted.Quotes.reflectModule.TermParamClauseMethods.hasErasedArgs",
92+
"scala.quoted.Quotes.reflectModule.defnModule.ErasedFunctionClass"
8893
)
8994

9095

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

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -108,56 +108,8 @@ ContextFunction24
108108
ContextFunction24
109109
ContextFunction25
110110
ContextFunction25
111-
ErasedFunction1
112-
ErasedFunction2
113-
ErasedFunction3
114-
ErasedFunction4
115-
ErasedFunction5
116-
ErasedFunction6
117-
ErasedFunction7
118-
ErasedFunction8
119-
ErasedFunction9
120-
ErasedFunction10
121-
ErasedFunction11
122-
ErasedFunction12
123-
ErasedFunction13
124-
ErasedFunction14
125-
ErasedFunction15
126-
ErasedFunction16
127-
ErasedFunction17
128-
ErasedFunction18
129-
ErasedFunction19
130-
ErasedFunction20
131-
ErasedFunction21
132-
ErasedFunction22
133-
ErasedFunction23
134-
ErasedFunction24
135-
ErasedFunction25
136-
ErasedContextFunction1
137-
ErasedContextFunction2
138-
ErasedContextFunction3
139-
ErasedContextFunction4
140-
ErasedContextFunction5
141-
ErasedContextFunction6
142-
ErasedContextFunction7
143-
ErasedContextFunction8
144-
ErasedContextFunction9
145-
ErasedContextFunction10
146-
ErasedContextFunction11
147-
ErasedContextFunction12
148-
ErasedContextFunction13
149-
ErasedContextFunction14
150-
ErasedContextFunction15
151-
ErasedContextFunction16
152-
ErasedContextFunction17
153-
ErasedContextFunction18
154-
ErasedContextFunction19
155-
ErasedContextFunction20
156-
ErasedContextFunction21
157-
ErasedContextFunction22
158-
ErasedContextFunction23
159-
ErasedContextFunction24
160-
ErasedContextFunction25
111+
class java.lang.Exception: Erased function classes are not supported. Use a refined `ErasedFunctionClass`
112+
ErasedFunction
161113
Tuple2
162114
Tuple3
163115
Tuple4

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ object Macros {
6363
printout(defn.FunctionClass(i, isContextual = true).name)
6464
printout(defn.FunctionClass(i, isImplicit = true).name)
6565

66-
for (i <- 1 to 25)
67-
printout(defn.FunctionClass(i, isErased = true).name)
66+
// should fail
67+
printout(defn.FunctionClass(1, isErased = true).name)
6868

69-
for (i <- 1 to 25)
70-
printout(defn.FunctionClass(i, isImplicit = true, isErased = true).name)
69+
printout(defn.ErasedFunctionClass.name)
7170

7271
for (i <- 2 to 22)
7372
printout(defn.TupleClass(i).name)

0 commit comments

Comments
 (0)