Skip to content

Commit 8232b9d

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 e813069 commit 8232b9d

File tree

4 files changed

+41
-58
lines changed

4 files changed

+41
-58
lines changed

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

Lines changed: 13 additions & 4 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

@@ -2767,8 +2774,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27672774
def SomeModule: Symbol = dotc.core.Symbols.defn.SomeClass.companionModule
27682775
def ProductClass: Symbol = dotc.core.Symbols.defn.ProductClass
27692776
def FunctionClass(arity: Int, isImplicit: Boolean = false, isErased: Boolean = false): Symbol =
2770-
if isErased then dotc.core.Symbols.defn.ErasedFunctionClass
2777+
if isErased then
2778+
throw new Exception("Erased function classes are not supported. Use a refined `ErasedFunctionClass`")
27712779
else dotc.core.Symbols.defn.FunctionSymbol(arity, isImplicit)
2780+
def ErasedFunctionClass = dotc.core.Symbols.defn.ErasedFunctionClass
27722781
def TupleClass(arity: Int): Symbol =
27732782
dotc.core.Symbols.defn.TupleType(arity).nn.classSymbol.asClass
27742783
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
@@ -4256,6 +4275,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
42564275
*/
42574276
def FunctionClass(arity: Int, isImplicit: Boolean = false, isErased: Boolean = false): Symbol
42584277

4278+
/** The `ErasedFunction` built-in trait. */
4279+
@experimental
4280+
def ErasedFunctionClass: Symbol
4281+
42594282
/** Function-like object that maps arity to symbols for classes `scala.TupleX`.
42604283
* - 0th element is `NoSymbol`
42614284
* - 1st element is `NoSymbol`

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

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -82,55 +82,7 @@ ContextFunction22
8282
ContextFunction23
8383
ContextFunction24
8484
ContextFunction25
85-
ErasedFunction
86-
ErasedFunction
87-
ErasedFunction
88-
ErasedFunction
89-
ErasedFunction
90-
ErasedFunction
91-
ErasedFunction
92-
ErasedFunction
93-
ErasedFunction
94-
ErasedFunction
95-
ErasedFunction
96-
ErasedFunction
97-
ErasedFunction
98-
ErasedFunction
99-
ErasedFunction
100-
ErasedFunction
101-
ErasedFunction
102-
ErasedFunction
103-
ErasedFunction
104-
ErasedFunction
105-
ErasedFunction
106-
ErasedFunction
107-
ErasedFunction
108-
ErasedFunction
109-
ErasedFunction
110-
ErasedFunction
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
85+
class java.lang.Exception: Erased function classes are not supported. Use a refined `ErasedFunctionClass`
13486
ErasedFunction
13587
Tuple2
13688
Tuple3

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ object Macros {
6262
for (i <- 0 to 25)
6363
printout(defn.FunctionClass(i, isImplicit = true).name)
6464

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

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

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

0 commit comments

Comments
 (0)