Skip to content

Commit 97dd61a

Browse files
committed
Add Quotes MethodTypeCompanion
This allow makes it possible to create contextual and implicit method types. Fixes scala#18477
1 parent d482108 commit 97dd61a

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
21802180
end MethodOrPolyTypeTest
21812181

21822182
type MethodType = dotc.core.Types.MethodType
2183+
type MethodTypeCompanion = dotc.core.Types.MethodTypeCompanion
21832184

21842185
object MethodTypeTypeTest extends TypeTest[TypeRepr, MethodType]:
21852186
def unapply(x: TypeRepr): Option[MethodType & x.type] = x match
@@ -2190,16 +2191,20 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
21902191
object MethodType extends MethodTypeModule:
21912192
def apply(paramNames: List[String])(paramInfosExp: MethodType => List[TypeRepr], resultTypeExp: MethodType => TypeRepr): MethodType =
21922193
Types.MethodType(paramNames.map(_.toTermName))(paramInfosExp, resultTypeExp)
2194+
def Plain: MethodTypeCompanion = Types.MethodType
2195+
def Contextual: MethodTypeCompanion = Types.ContextualMethodType
2196+
def Implicit: MethodTypeCompanion = Types.ImplicitMethodType
21932197
def unapply(x: MethodType): (List[String], List[TypeRepr], TypeRepr) =
21942198
(x.paramNames.map(_.toString), x.paramTypes, x.resType)
21952199
end MethodType
21962200

21972201
given MethodTypeMethods: MethodTypeMethods with
21982202
extension (self: MethodType)
21992203
def isErased: Boolean = false
2204+
def isContextual: Boolean = self.isContextualMethod
22002205
def isImplicit: Boolean = self.isImplicitMethod
2206+
def companion: MethodTypeCompanion = self.companion
22012207
def param(idx: Int): TypeRepr = self.newParamRef(idx)
2202-
22032208
def erasedParams: List[Boolean] = self.erasedParams
22042209
def hasErasedParams: Boolean = self.hasErasedParams
22052210
end extension

library/src/scala/quoted/Quotes.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3185,6 +3185,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
31853185
/** Methods of the module object `val MethodType` */
31863186
trait MethodTypeModule { this: MethodType.type =>
31873187
def apply(paramNames: List[String])(paramInfosExp: MethodType => List[TypeRepr], resultTypeExp: MethodType => TypeRepr): MethodType
3188+
/** Companion for method type without implicit nor contextual parameter */
3189+
def Plain: MethodTypeCompanion
3190+
/** Companion for method type with contextual parameter */
3191+
def Contextual: MethodTypeCompanion
3192+
/** Companion for method type with implicit parameter */
3193+
def Implicit: MethodTypeCompanion
31883194
def unapply(x: MethodType): (List[String], List[TypeRepr], TypeRepr)
31893195
}
31903196

@@ -3194,8 +3200,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
31943200
/** Extension methods of `MethodType` */
31953201
trait MethodTypeMethods:
31963202
extension (self: MethodType)
3203+
/** Is this the type of using parameter clause `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */
3204+
def isContextual: Boolean
31973205
/** Is this the type of using parameter clause `(implicit X1, ..., Xn)`, `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */
31983206
def isImplicit: Boolean
3207+
/** Companion of this method type. Can be used to construct method types with the same implicitness of parameters */
3208+
def companion: MethodTypeCompanion
31993209
/** Is this the type of erased parameter clause `(erased x1: X1, ..., xn: Xn)` */
32003210
// TODO:deprecate in 3.4 and stabilize `erasedParams` and `hasErasedParams`.
32013211
// @deprecated("Use `hasErasedParams`","3.4")
@@ -3211,6 +3221,14 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
32113221
end extension
32123222
end MethodTypeMethods
32133223

3224+
/** Companion of a method type. It provides a way to instantiate new method types with a given implicitness of arguments. */
3225+
type MethodTypeCompanion
3226+
3227+
trait MethodTypeCompanionMethods {
3228+
/** Create a MethodType with the same implicitness as this companion */
3229+
def apply(paramNames: List[String])(paramInfosExp: MethodType => List[TypeRepr], resultTypeExp: MethodType => TypeRepr): MethodType
3230+
}
3231+
32143232
/** Type of the definition of a method taking a list of type parameters. It's return type may be a MethodType. */
32153233
type PolyType <: MethodOrPoly
32163234

project/MiMaFilters.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ object MiMaFilters {
66
// New API in 3.4.X
77
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule.ValOrDefDefTypeTest"),
88
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule.ValOrDefDefMethods"),
9-
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#defnModule.FunctionClass")
10-
// New API in 3.4.X
9+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#defnModule.FunctionClass"),
10+
11+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#MethodTypeMethods.isContextual"),
12+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#MethodTypeMethods.companion"),
13+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#MethodTypeModule.Plain"),
14+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#MethodTypeModule.Contextual"),
15+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#MethodTypeModule.Implicit"),
1116
)
1217
val TastyCore: Seq[ProblemFilter] = Seq(
1318
ProblemFilters.exclude[DirectMissingMethodProblem]("dotty.tools.tasty.TastyFormat.EXPLICITtpt"),

0 commit comments

Comments
 (0)