Skip to content

Commit 726d22f

Browse files
committed
Check that some types are not higher-kinded.
Invalidates scala#813. Review by @DarkDimius.
1 parent a8c8bda commit 726d22f

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ trait Checking {
347347
ctx.error(i"""$called is already implemented by super${caller.superClass},
348348
|its constructor cannot be called again""".stripMargin, call.pos)
349349
}
350+
351+
/** Check that `tpt` does not define a higher-kinded type */
352+
def checkSimpleKinded(tpt: Tree)(implicit ctx: Context) =
353+
if (tpt.tpe.isHK) ctx.error(s"missing type parameter", tpt.pos)
350354
}
351355

352356
trait NoChecking extends Checking {
@@ -360,4 +364,5 @@ trait NoChecking extends Checking {
360364
override def checkFeasible(tp: Type, pos: Position, where: => String = "")(implicit ctx: Context): Type = tp
361365
override def checkNoDoubleDefs(cls: Symbol)(implicit ctx: Context): Unit = ()
362366
override def checkParentCall(call: Tree, caller: ClassSymbol)(implicit ctx: Context) = ()
367+
override def checkSimpleKinded(tpt: Tree)(implicit ctx: Context) = ()
363368
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
370370
TypeTree(defn.SeqClass.typeRef.appliedTo(pt :: Nil))
371371
else
372372
typedType(tree.tpt)
373+
checkSimpleKinded(tpt1)
373374
val expr1 =
374375
if (isWildcard) tree.expr withType tpt1.tpe
375376
else typed(tree.expr, tpt1.tpe)
@@ -918,6 +919,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
918919
val ValDef(name, tpt, _) = vdef
919920
completeAnnotations(vdef, sym)
920921
val tpt1 = typedType(tpt)
922+
checkSimpleKinded(tpt1)
921923
val rhs1 = vdef.rhs match {
922924
case rhs @ Ident(nme.WILDCARD) => rhs withType tpt1.tpe
923925
case rhs => typedExpr(rhs, tpt1.tpe)
@@ -932,6 +934,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
932934
val vparamss1 = vparamss nestedMapconserve (typed(_).asInstanceOf[ValDef])
933935
if (sym is Implicit) checkImplicitParamsNotSingletons(vparamss1)
934936
val tpt1 = typedType(tpt)
937+
checkSimpleKinded(tpt1)
935938
val rhs1 = typedExpr(ddef.rhs, tpt1.tpe)
936939
assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym)
937940
//todo: make sure dependent method types do not depend on implicits or by-name params

test/dotc/tests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class tests extends CompilerTest {
104104
@Test def neg_typedapply() = compileFile(negDir, "typedapply", xerrors = 4)
105105
@Test def neg_typedidents() = compileFile(negDir, "typedIdents", xerrors = 2)
106106
@Test def neg_assignments() = compileFile(negDir, "assignments", xerrors = 3)
107-
@Test def neg_typers() = compileFile(negDir, "typers", xerrors = 10)(allowDoubleBindings)
107+
@Test def neg_typers() = compileFile(negDir, "typers", xerrors = 13)(allowDoubleBindings)
108108
@Test def neg_privates() = compileFile(negDir, "privates", xerrors = 2)
109109
@Test def neg_rootImports = compileFile(negDir, "rootImplicits", xerrors = 2)
110110
@Test def neg_templateParents() = compileFile(negDir, "templateParents", xerrors = 3)

tests/neg/typers.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ object typers {
2929
def g[T](x: T): T = x // OK!
3030
}
3131

32+
type L[X] = scala.collection.immutable.List[X]
33+
type M[X, Y] <: scala.collection.immutable.Map[X, Y]
3234

33-
35+
object hk {
36+
def f(x: L) // error: missing type parameter
37+
: M = // error: missing type parameter
38+
??? : M // error: missing type parameter
39+
}
3440

3541
object returns {
3642

0 commit comments

Comments
 (0)