Skip to content

Commit 1d313e9

Browse files
committed
Generalize kind checking
We now check that the rank of the kind of a type argument does not exceed the rank of its bounds. i2771b is an example that shows that checking at rank * only is not enough.
1 parent a39644d commit 1d313e9

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,25 @@ object Checking {
100100
checkValidIfHKApply(ctx.addMode(Mode.AllowLambdaWildcardApply))
101101
}
102102

103-
/** Check that `arg` has a *-type, unless `paramBounds` is higher-kinded.
103+
/** Check that the rank of the kind of `arg` does not exceed the rank of the
104+
* kind of `paramBounds`. E.g. if `paramBounds` has *-kind, `arg` must have
105+
* *-kind as well, and analogously for higher kinds.
104106
* More detailed kind checking is done as part of checkBounds in PostTyper.
105-
* The purpose of checkStarKind is to do a rough test earlier in Typer,
107+
* The purpose of checkKindRank is to do a rough test earlier in Typer,
106108
* in order to prevent scenarios that lead to self application of
107109
* types. Self application needs to be avoided since it can lead to stackoverflows.
108110
* A test case is neg/i2771.scala.
109111
*/
110-
def checkStarKind(arg: Tree, paramBounds: TypeBounds)(implicit ctx: Context): Tree =
111-
if (arg.tpe.isHK && !paramBounds.isHK)
112-
errorTree(arg, em"${arg.tpe} takes type parameters")
113-
else
114-
arg
115-
116-
def checkStarKinds(args: List[Tree], paramBoundss: List[TypeBounds])(implicit ctx: Context): List[Tree] = {
117-
val args1 = args.zipWithConserve(paramBoundss)(checkStarKind)
112+
def checkKindRank(arg: Tree, paramBounds: TypeBounds)(implicit ctx: Context): Tree = {
113+
def kindOK(argType: Type, boundType: Type): Boolean =
114+
!argType.isHK ||
115+
boundType.isHK && kindOK(argType.resultType, boundType.resultType)
116+
if (kindOK(arg.tpe, paramBounds.hi)) arg
117+
else errorTree(arg, em"${arg.tpe} takes type parameters")
118+
}
119+
120+
def checkKindRanks(args: List[Tree], paramBoundss: List[TypeBounds])(implicit ctx: Context): List[Tree] = {
121+
val args1 = args.zipWithConserve(paramBoundss)(checkKindRank)
118122
args1 ++ args.drop(paramBoundss.length)
119123
// add any arguments that do not correspond to a parameter back,
120124
// so the wrong number of parameters is reported afterwards.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import NameOps._
1414
import collection.mutable
1515
import reporting.diagnostic.Message
1616
import reporting.diagnostic.messages._
17-
import Checking.{checkStarKind, checkStarKinds, checkNoPrivateLeaks}
17+
import Checking.{checkKindRank, checkKindRanks, checkNoPrivateLeaks}
1818

1919
trait TypeAssigner {
2020
import tpd._
@@ -358,7 +358,7 @@ trait TypeAssigner {
358358
else if (!paramNames.contains(name))
359359
ctx.error(s"undefined parameter name, required: ${paramNames.mkString(" or ")}", arg.pos)
360360
else
361-
namedArgMap(name) = checkStarKind(arg, paramBoundsByName(name.asTypeName)).tpe
361+
namedArgMap(name) = checkKindRank(arg, paramBoundsByName(name.asTypeName)).tpe
362362

363363
// Holds indexes of non-named typed arguments in paramNames
364364
val gapBuf = new mutable.ListBuffer[Int]
@@ -391,7 +391,7 @@ trait TypeAssigner {
391391
}
392392
}
393393
else {
394-
val argTypes = checkStarKinds(args, pt.paramInfos).tpes
394+
val argTypes = checkKindRanks(args, pt.paramInfos).tpes
395395
if (sameLength(argTypes, paramNames) || ctx.phase.prev.relaxedTyping) pt.instantiate(argTypes)
396396
else wrongNumberOfTypeArgs(fn.tpe, pt.typeParams, args, tree.pos)
397397
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11401140
}
11411141
args.zipWithConserve(tparams)(typedArg(_, _)).asInstanceOf[List[Tree]]
11421142
}
1143-
val args2 = checkStarKinds(args1, tparams.map(_.paramInfo.bounds))
1143+
val args2 = checkKindRanks(args1, tparams.map(_.paramInfo.bounds))
11441144
// check that arguments conform to bounds is done in phase PostTyper
11451145
assignType(cpy.AppliedTypeTree(tree)(tpt1, args2), tpt1, args2)
11461146
}

tests/neg/i2771b.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
trait A { type L[X[_]] }
2+
trait B { type L }
3+
trait C { type M <: A }
4+
trait D { type M >: B }
5+
6+
object Test {
7+
def test(x: C with D): Unit = {
8+
def f(y: x.M)(z: y.L[y.L]) = z // error: y.L takes type parameters
9+
f(new B { type L[F[_[_]]] = F[F] })(1) // error: F takes type parameters
10+
}
11+
}

0 commit comments

Comments
 (0)