Skip to content

Commit cbb565a

Browse files
committed
Merge pull request #821 from dotty-staging/fix-check-simple-kinded
Check that some types are not higher-kinded.
2 parents 78d7690 + 6cca64f commit cbb565a

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Trees._
1616
import ProtoTypes._
1717
import Constants._
1818
import Scopes._
19+
import ErrorReporting.errorTree
1920
import annotation.unchecked
2021
import util.Positions._
2122
import util.{Stats, SimpleMap}
@@ -347,6 +348,17 @@ trait Checking {
347348
ctx.error(i"""$called is already implemented by super${caller.superClass},
348349
|its constructor cannot be called again""".stripMargin, call.pos)
349350
}
351+
352+
/** Check that `tpt` does not define a higher-kinded type */
353+
def checkSimpleKinded(tpt: Tree)(implicit ctx: Context): Tree =
354+
if (tpt.tpe.isHK && !ctx.compilationUnit.isJava) {
355+
// be more lenient with missing type params in Java,
356+
// needed to make pos/java-interop/t1196 work.
357+
val alias = tpt.tpe.dealias
358+
if (alias.isHK) errorTree(tpt, d"missing type parameter for ${tpt.tpe}")
359+
else tpt.withType(alias)
360+
}
361+
else tpt
350362
}
351363

352364
trait NoChecking extends Checking {
@@ -360,4 +372,5 @@ trait NoChecking extends Checking {
360372
override def checkFeasible(tp: Type, pos: Position, where: => String = "")(implicit ctx: Context): Type = tp
361373
override def checkNoDoubleDefs(cls: Symbol)(implicit ctx: Context): Unit = ()
362374
override def checkParentCall(call: Tree, caller: ClassSymbol)(implicit ctx: Context) = ()
375+
override def checkSimpleKinded(tpt: Tree)(implicit ctx: Context): Tree = tpt
363376
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
370370
if (untpd.isWildcardStarArg(tree))
371371
TypeTree(defn.SeqClass.typeRef.appliedTo(pt :: Nil))
372372
else
373-
typedType(tree.tpt)
373+
checkSimpleKinded(typedType(tree.tpt))
374374
val expr1 =
375375
if (isWildcard) tree.expr withType tpt1.tpe
376376
else typed(tree.expr, tpt1.tpe)
@@ -918,7 +918,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
918918
def typedValDef(vdef: untpd.ValDef, sym: Symbol)(implicit ctx: Context) = track("typedValDef") {
919919
val ValDef(name, tpt, _) = vdef
920920
completeAnnotations(vdef, sym)
921-
val tpt1 = typedType(tpt)
921+
val tpt1 = checkSimpleKinded(typedType(tpt))
922922
val rhs1 = vdef.rhs match {
923923
case rhs @ Ident(nme.WILDCARD) => rhs withType tpt1.tpe
924924
case rhs => typedExpr(rhs, tpt1.tpe)
@@ -932,7 +932,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
932932
val tparams1 = tparams mapconserve (typed(_).asInstanceOf[TypeDef])
933933
val vparamss1 = vparamss nestedMapconserve (typed(_).asInstanceOf[ValDef])
934934
if (sym is Implicit) checkImplicitParamsNotSingletons(vparamss1)
935-
val tpt1 = typedType(tpt)
935+
val tpt1 = checkSimpleKinded(typedType(tpt))
936936
val rhs1 = typedExpr(ddef.rhs, tpt1.tpe)
937937
assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym)
938938
//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
@@ -105,7 +105,7 @@ class tests extends CompilerTest {
105105
@Test def neg_typedapply() = compileFile(negDir, "typedapply", xerrors = 4)
106106
@Test def neg_typedidents() = compileFile(negDir, "typedIdents", xerrors = 2)
107107
@Test def neg_assignments() = compileFile(negDir, "assignments", xerrors = 3)
108-
@Test def neg_typers() = compileFile(negDir, "typers", xerrors = 10)(allowDoubleBindings)
108+
@Test def neg_typers() = compileFile(negDir, "typers", xerrors = 13)(allowDoubleBindings)
109109
@Test def neg_privates() = compileFile(negDir, "privates", xerrors = 2)
110110
@Test def neg_rootImports = compileFile(negDir, "rootImplicits", xerrors = 2)
111111
@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

tests/pos/polyalias.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ object Test {
33

44
type S = scala.Predef.Set
55

6-
val z: S = ???
6+
val z: S[_] = ???
77

88

99
type Pair[T] = (T, T)

0 commit comments

Comments
 (0)