Skip to content

Commit e632c3f

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 bc8da45 commit e632c3f

File tree

4 files changed

+41
-58
lines changed

4 files changed

+41
-58
lines changed

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

+13-4
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,12 +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 then dotc.core.Symbols.defn.ErasedFunctionClass
2778+
if isErased then
2779+
throw new Exception("Erased function classes are not supported. Use a refined `ErasedFunctionClass`")
27722780
else dotc.core.Symbols.defn.FunctionSymbol(arity, isImplicit)
27732781
def FunctionClass(arity: Int): Symbol =
27742782
FunctionClass(arity, false, false)
27752783
def FunctionClass(arity: Int, isContextual: Boolean): Symbol =
27762784
FunctionClass(arity, isContextual, false)
2785+
def ErasedFunctionClass = dotc.core.Symbols.defn.ErasedFunctionClass
27772786
def TupleClass(arity: Int): Symbol =
27782787
dotc.core.Symbols.defn.TupleType(arity).nn.classSymbol.asClass
27792788
def isTupleClass(sym: Symbol): Boolean =

library/src/scala/quoted/Quotes.scala

+24-1
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 `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-macros/tasty-definitions-1.check

+1-49
Original file line numberDiff line numberDiff line change
@@ -108,55 +108,7 @@ ContextFunction24
108108
ContextFunction24
109109
ContextFunction25
110110
ContextFunction25
111-
ErasedFunction
112-
ErasedFunction
113-
ErasedFunction
114-
ErasedFunction
115-
ErasedFunction
116-
ErasedFunction
117-
ErasedFunction
118-
ErasedFunction
119-
ErasedFunction
120-
ErasedFunction
121-
ErasedFunction
122-
ErasedFunction
123-
ErasedFunction
124-
ErasedFunction
125-
ErasedFunction
126-
ErasedFunction
127-
ErasedFunction
128-
ErasedFunction
129-
ErasedFunction
130-
ErasedFunction
131-
ErasedFunction
132-
ErasedFunction
133-
ErasedFunction
134-
ErasedFunction
135-
ErasedFunction
136-
ErasedFunction
137-
ErasedFunction
138-
ErasedFunction
139-
ErasedFunction
140-
ErasedFunction
141-
ErasedFunction
142-
ErasedFunction
143-
ErasedFunction
144-
ErasedFunction
145-
ErasedFunction
146-
ErasedFunction
147-
ErasedFunction
148-
ErasedFunction
149-
ErasedFunction
150-
ErasedFunction
151-
ErasedFunction
152-
ErasedFunction
153-
ErasedFunction
154-
ErasedFunction
155-
ErasedFunction
156-
ErasedFunction
157-
ErasedFunction
158-
ErasedFunction
159-
ErasedFunction
111+
class java.lang.Exception: Erased function classes are not supported. Use a refined `ErasedFunctionClass`
160112
ErasedFunction
161113
Tuple2
162114
Tuple3

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

+3-4
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)