Skip to content

Commit abb8146

Browse files
committed
Utility functions to test whether a type is a TypeBounds
1 parent 9e16a48 commit abb8146

File tree

10 files changed

+17
-16
lines changed

10 files changed

+17
-16
lines changed

compiler/src/dotty/tools/dotc/core/OrderingConstraint.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
166166
entries != null && isBounds(entries(pnum)) && (typeVar(entries, pnum) eq tvar)
167167
}
168168

169-
private def isBounds(tp: Type) = tp.isInstanceOf[TypeBounds]
170-
171169
// ---------- Dependency handling ----------------------------------------------
172170

173171
def lower(param: TypeParamRef): List[TypeParamRef] = lowerLens(this, param.binder, param.paramNum)

compiler/src/dotty/tools/dotc/core/TypeApplications.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ object TypeApplications {
106106
private[this] var available = (0 until args.length).toSet
107107
var allReplaced: Boolean = true
108108
def hasWildcardArg(p: TypeParamRef): Boolean =
109-
p.binder == tycon && args(p.paramNum).isInstanceOf[TypeBounds]
109+
p.binder == tycon && isBounds(args(p.paramNum))
110110
def canReduceWildcard(p: TypeParamRef): Boolean =
111111
!ctx.mode.is(Mode.AllowLambdaWildcardApply) || available.contains(p.paramNum)
112112
def atNestedLevel(op: => Type): Type = {
@@ -356,7 +356,7 @@ class TypeApplications(val self: Type) extends AnyVal {
356356
else dealiased match {
357357
case dealiased: HKTypeLambda =>
358358
def tryReduce =
359-
if (!args.exists(_.isInstanceOf[TypeBounds])) {
359+
if (!args.exists(isBounds)) {
360360
val followAlias = Config.simplifyApplications && {
361361
dealiased.resType match {
362362
case AppliedType(tyconBody, dealiasedArgs) =>

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
15191519
if (common.exists) common
15201520
else if (v > 0) glb(arg1.hiBound, arg2.hiBound)
15211521
else if (v < 0) lub(arg1.loBound, arg2.loBound)
1522-
else if (arg1.isInstanceOf[TypeBounds] || arg2.isInstanceOf[TypeBounds])
1522+
else if (isBounds(arg1) || isBounds(arg2))
15231523
TypeBounds(lub(arg1.loBound, arg2.loBound),
15241524
glb(arg1.hiBound, arg2.hiBound))
15251525
else if (homogenizeArgs && !frozenConstraint && isSameType(arg1, arg2)) arg1

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,7 +2067,7 @@ object Types {
20672067
var tp1 = argForParam(base.tp1)
20682068
var tp2 = argForParam(base.tp2)
20692069
val variance = tparam.paramVariance
2070-
if (tp1.isInstanceOf[TypeBounds] || tp2.isInstanceOf[TypeBounds] || variance == 0) {
2070+
if (isBounds(tp1) || isBounds(tp2) || variance == 0) {
20712071
// compute argument as a type bounds instead of a point type
20722072
tp1 = tp1.bounds
20732073
tp2 = tp2.bounds
@@ -3479,6 +3479,8 @@ object Types {
34793479
if (tparams.isEmpty) HKTypeLambda.any(args.length).typeParams else tparams
34803480
}
34813481

3482+
def hasWildcardArg(implicit ctx: Context): Boolean = args.exists(isBounds)
3483+
34823484
def derivedAppliedType(tycon: Type, args: List[Type])(implicit ctx: Context): Type =
34833485
if ((tycon eq this.tycon) && (args eq this.args)) this
34843486
else tycon.appliedTo(args)
@@ -3619,7 +3621,7 @@ object Types {
36193621
/** A skolem type reference with underlying type `info`.
36203622
* Note: `info` is a var, since this allows one to create a number of skolem types
36213623
* and fill in their infos with types that refers to the skolem types recursively.
3622-
* This is used in `captureWildcards` in Typer`.
3624+
* This is used in `captureWildcards` in `Typer`.
36233625
*/
36243626
case class SkolemType(var info: Type) extends UncachedProxyType with ValueType with SingletonType {
36253627
override def underlying(implicit ctx: Context): Type = info
@@ -5091,4 +5093,6 @@ object Types {
50915093
private val keepAlways: AnnotatedType => Context => Boolean = _ => _ => true
50925094
private val keepNever: AnnotatedType => Context => Boolean = _ => _ => false
50935095
private val keepIfRefining: AnnotatedType => Context => Boolean = tp => ctx => tp.isRefining(ctx)
5096+
5097+
val isBounds: Type => Boolean = _.isInstanceOf[TypeBounds]
50945098
}

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
476476
typeDefText(tparamsTxt, toText(rhs))
477477
case LambdaTypeTree(tparams, body) =>
478478
recur(body, tparamsText(tparams))
479-
case rhs: TypeTree if rhs.typeOpt.isInstanceOf[TypeBounds] =>
479+
case rhs: TypeTree if isBounds(rhs.typeOpt) =>
480480
typeDefText(tparamsTxt, toText(rhs))
481481
case rhs =>
482482
typeDefText(tparamsTxt, optText(rhs)(" = " ~ _))

compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ object TypeTestsCasts {
8181
/** Approximate type parameters depending on variance */
8282
def stripTypeParam(tp: Type)(implicit ctx: Context) = new ApproximatingTypeMap {
8383
def apply(tp: Type): Type = tp match {
84-
case tp: TypeRef if tp.underlying.isInstanceOf[TypeBounds] =>
84+
case tp: TypeRef if isBounds(tp.underlying) =>
8585
val lo = apply(tp.info.loBound)
8686
val hi = apply(tp.info.hiBound)
8787
range(lo, hi)

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,15 +624,15 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
624624
if (maximize) lo else hi
625625

626626
def apply(tp: Type): Type = tp match {
627-
case tp: TypeRef if tp.underlying.isInstanceOf[TypeBounds] =>
627+
case tp: TypeRef if isBounds(tp.underlying) =>
628628
val lo = this(tp.info.loBound)
629629
val hi = this(tp.info.hiBound)
630630
// See tests/patmat/gadt.scala tests/patmat/exhausting.scala tests/patmat/t9657.scala
631631
val exposed = expose(lo, hi)
632632
debug.println(s"$tp exposed to =====> $exposed")
633633
exposed
634634

635-
case AppliedType(tycon: TypeRef, args) if tycon.underlying.isInstanceOf[TypeBounds] =>
635+
case AppliedType(tycon: TypeRef, args) if isBounds(tycon.underlying) =>
636636
val args2 = args.map(this)
637637
val lo = this(tycon.info.loBound).applyIfParameterized(args2)
638638
val hi = this(tycon.info.hiBound).applyIfParameterized(args2)

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
10751075
}
10761076

10771077
var argTypes = unapplyArgs(unapplyApp.tpe, unapplyFn, args, tree.sourcePos)
1078-
for (argType <- argTypes) assert(!argType.isInstanceOf[TypeBounds], unapplyApp.tpe.show)
1078+
for (argType <- argTypes) assert(!isBounds(argType), unapplyApp.tpe.show)
10791079
val bunchedArgs =
10801080
if (argTypes.nonEmpty && argTypes.last.isRepeatedParam)
10811081
args.lastOption match {

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ object Checking {
8080
if (boundsCheck) checkBounds(orderedArgs, bounds, instantiate)
8181

8282
def checkWildcardApply(tp: Type): Unit = tp match {
83-
case tp @ AppliedType(tycon, args) =>
84-
if (tycon.isLambdaSub && args.exists(_.isInstanceOf[TypeBounds]))
83+
case tp @ AppliedType(tycon, _) =>
84+
if (tycon.isLambdaSub && tp.hasWildcardArg)
8585
ctx.errorOrMigrationWarning(
8686
ex"unreducible application of higher-kinded type $tycon to wildcard arguments",
8787
tree.sourcePos)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ object Typer {
8080
*/
8181
private[typer] val HiddenSearchFailure = new Property.Key[SearchFailure]
8282

83-
val isBounds: Type => Boolean = _.isInstanceOf[TypeBounds]
8483
}
8584

8685
class Typer extends Namer
@@ -2708,7 +2707,7 @@ class Typer extends Namer
27082707
case tp: RecType => tp.derivedRecType(captureWildcards(tp.parent))
27092708
case tp: LazyRef => captureWildcards(tp.ref)
27102709
case tp: AnnotatedType => tp.derivedAnnotatedType(captureWildcards(tp.parent), tp.annot)
2711-
case tp @ AppliedType(tycon, args) if args.exists(isBounds) =>
2710+
case tp @ AppliedType(tycon, args) if tp.hasWildcardArg =>
27122711
tycon.typeParams match {
27132712
case tparams @ ((_: Symbol) :: _) =>
27142713
val args1 = args.map {

0 commit comments

Comments
 (0)