Skip to content

Commit c3776b0

Browse files
committed
Add missing cases for flexible types in Quotes
1 parent cb554eb commit c3776b0

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
22302230
def isErased: Boolean = false
22312231
def isImplicit: Boolean = self.isImplicitMethod
22322232
def isContextual: Boolean = self.isContextualMethod
2233-
def methodTypeKind: MethodTypeKind =
2233+
def methodTypeKind: MethodTypeKind =
22342234
self.companion match
22352235
case Types.ContextualMethodType => MethodTypeKind.Contextual
22362236
case Types.ImplicitMethodType => MethodTypeKind.Implicit
@@ -2343,6 +2343,21 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
23432343
def unapply(x: NoPrefix): true = true
23442344
end NoPrefix
23452345

2346+
type FlexibleType = dotc.core.Types.FlexibleType
2347+
2348+
object FlexibleTypeTest extends TypeTest[TypeRepr, FlexibleType]:
2349+
def unapply(x: TypeRepr): Option[FlexibleType & x.type] = x match
2350+
case x: (Types.FlexibleType & x.type) => Some(x)
2351+
case _ => None
2352+
end FlexibleTypeTest
2353+
2354+
object FlexibleType extends FlexibleTypeModule:
2355+
def apply(tp: TypeRepr): FlexibleType =
2356+
// why object FlexibleType.apply(Type) is not found here?
2357+
Types.FlexibleType(tp, tp)
2358+
def unapply(x: FlexibleType): TypeRepr = x.hi
2359+
end FlexibleType
2360+
23462361
type Constant = dotc.core.Constants.Constant
23472362

23482363
object Constant extends ConstantModule

compiler/src/scala/quoted/runtime/impl/printers/Extractors.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ object Extractors {
239239
this += "NoPrefix()"
240240
case MatchCase(pat, rhs) =>
241241
this += "MatchCase(" += pat += ", " += rhs += ")"
242+
case FlexibleType(tp) =>
243+
this += "FlexibleType(" += tp += ")"
242244
}
243245

244246
def visitSignature(sig: Signature): this.type = {

compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,11 @@ object SourceCode {
12471247
this += " => "
12481248
printType(rhs)
12491249

1250+
case FlexibleType(tp) =>
1251+
this += "("
1252+
printType(tp)
1253+
this += ")?"
1254+
12501255
case _ =>
12511256
cannotBeShownAsSource(tpe.show(using Printer.TypeReprStructure))
12521257
}

library/src/scala/quoted/Quotes.scala

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
211211
* +- MatchCase
212212
* +- TypeBounds
213213
* +- NoPrefix
214-
*
214+
*
215215
* +- MethodTypeKind -+- Contextual
216216
* +- Implicit
217217
* +- Plain
@@ -3273,7 +3273,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
32733273
def isImplicit: Boolean
32743274
/** Is this the type of parameter clause like `(using X1, ..., Xn)` or `(using x1: X1, x2: X2, ... )` */
32753275
def isContextual: Boolean
3276-
/** Returns a MethodTypeKind object representing the implicitness of the MethodType parameter clause. */
3276+
/** Returns a MethodTypeKind object representing the implicitness of the MethodType parameter clause. */
32773277
def methodTypeKind: MethodTypeKind
32783278
/** Is this the type of erased parameter clause `(erased x1: X1, ..., xn: Xn)` */
32793279
@deprecated("Use `hasErasedParams` and `erasedParams`", "3.4")
@@ -3428,6 +3428,23 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
34283428
def unapply(x: NoPrefix): true
34293429
}
34303430

3431+
// ----- Flexible Type --------------------------------------------
3432+
3433+
/** Flexible types for explicit nulls */
3434+
type FlexibleType <: TypeRepr
3435+
3436+
/** `TypeTest` that allows testing at runtime in a pattern match if a `TypeRepr` is a `FlexibleType` */
3437+
given FlexibleTypeTypeTest: TypeTest[TypeRepr, FlexibleType]
3438+
3439+
/** Module object of `type FlexibleType` */
3440+
val FlexibleType: FlexibleTypeModule
3441+
3442+
/** Methods of the module object `val FlexibleType` */
3443+
trait FlexibleTypeModule { this: FlexibleType.type =>
3444+
def apply(tp: TypeRepr): FlexibleType
3445+
def unapply(x: FlexibleType): TypeRepr
3446+
}
3447+
34313448
///////////////
34323449
// CONSTANTS //
34333450
///////////////

0 commit comments

Comments
 (0)