Skip to content

Commit fd44a17

Browse files
committed
Disallow hk type parameters in lower bounds.
Also: various cleanups to comments.
1 parent 1c77b03 commit fd44a17

File tree

7 files changed

+28
-8
lines changed

7 files changed

+28
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
550550
*
551551
* (2) Try to eta expand the constructor of `other`.
552552
*
553-
* (3a) In mode `TypeVarsMissConetxt` replace the projection's hk constructor parameter
553+
* (3a) In mode `TypevarsMissConetxt` replace the projection's hk constructor parameter
554554
* by the eta expansion of step (2) reapplied to the projection's arguments.
555555
* (3b) In normal mode, try to unify the projection's hk constructor parameter with
556556
* the eta expansion of step(2)
@@ -573,7 +573,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
573573
else
574574
(tryInstantiate(param, EtaExpansion(tycon)), projection)
575575
ok &&
576-
(if (inOrder) isSubType(projection1, other) else isSubType(other, projection1)) // ### move out?
576+
(if (inOrder) isSubType(projection1, other) else isSubType(other, projection1))
577577
case _ =>
578578
false
579579
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,8 @@ trait Applications extends Compatibility { self: Typer =>
613613
assignType(cpy.TypeApply(tree)(typedFn, typedArgs), typedFn, typedArgs)
614614
}
615615

616-
def adaptTypeArg(tree: tpd.Tree, bound: Type)(implicit ctx: Context): tpd.Tree = {
617-
//val was = tree.tpe.EtaExpandIfHK(bound)
618-
//val now = tree.tpe.adaptIfHK(bound) // ###
619-
//if (was != now) println(i"diff adapt ${tree.tpe} to $bound, was: $was, now: $now")
616+
def adaptTypeArg(tree: tpd.Tree, bound: Type)(implicit ctx: Context): tpd.Tree =
620617
tree.withType(tree.tpe.adaptIfHK(bound))
621-
}
622618

623619
/** Rewrite `new Array[T](....)` trees to calls of newXYZArray methods. */
624620
def convertNewArray(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ trait Checking {
401401
errorTree(tpt, d"missing type parameter for ${tpt.tpe}")
402402
}
403403
else tpt
404+
405+
def checkLowerNotHK(sym: Symbol, tparams: List[Symbol], pos: Position)(implicit ctx: Context) =
406+
if (tparams.nonEmpty)
407+
sym.info match {
408+
case info: TypeAlias => // ok
409+
case TypeBounds(lo, _) =>
410+
for (tparam <- tparams)
411+
if (tparam.typeRef.occursIn(lo))
412+
ctx.error(i"type parameter ${tparam.name} may not occur in lower bound $lo", pos)
413+
case _ =>
414+
}
404415
}
405416

406417
trait NoChecking extends Checking {
@@ -414,4 +425,5 @@ trait NoChecking extends Checking {
414425
override def checkNoDoubleDefs(cls: Symbol)(implicit ctx: Context): Unit = ()
415426
override def checkParentCall(call: Tree, caller: ClassSymbol)(implicit ctx: Context) = ()
416427
override def checkSimpleKinded(tpt: Tree)(implicit ctx: Context): Tree = tpt
428+
override def checkLowerNotHK(sym: Symbol, tparams: List[Symbol], pos: Position)(implicit ctx: Context) = ()
417429
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,6 @@ class Namer { typer: Typer =>
845845
* of arguments in F-bounds, because the recursive type was initialized with
846846
* TypeBounds.empty.
847847
*/
848-
// ### Check whether this is still needed!
849848
def etaExpandArgs(implicit ctx: Context) = new TypeMap {
850849
def apply(tp: Type): Type = tp match {
851850
case tp: RefinedType =>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
930930

931931
def typedTypeDef(tdef: untpd.TypeDef, sym: Symbol)(implicit ctx: Context): Tree = track("typedTypeDef") {
932932
val TypeDef(name, rhs) = tdef
933+
checkLowerNotHK(sym, tdef.tparams.map(symbolOfTree), tdef.pos)
933934
completeAnnotations(tdef, sym)
934935
val _ = typedType(rhs) // unused, typecheck only to remove from typedTree
935936
assignType(cpy.TypeDef(tdef)(name, TypeTree(sym.info), Nil), sym)

test/dotc/tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class tests extends CompilerTest {
147147
@Test def neg_cycles = compileFile(negDir, "cycles", xerrors = 8)
148148
@Test def neg_boundspropagation = compileFile(negDir, "boundspropagation", xerrors = 5)
149149
@Test def neg_refinedSubtyping = compileFile(negDir, "refinedSubtyping", xerrors = 2)
150+
@Test def neg_hklower = compileFile(negDir, "hklower", xerrors = 3)
150151
@Test def neg_i0091_infpaths = compileFile(negDir, "i0091-infpaths", xerrors = 3)
151152
@Test def neg_i0248_inherit_refined = compileFile(negDir, "i0248-inherit-refined", xerrors = 4)
152153
@Test def neg_i0281 = compileFile(negDir, "i0281-null-primitive-conforms", xerrors = 3)

tests/neg/hklower.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Test {
2+
3+
type T[X] // OK
4+
type U[X] = T[X] // OK
5+
6+
type V[X] >: T[X] // error
7+
type W[X] >: T[X] <: T[X] // error
8+
9+
def f[C[X] >: T[X]]() = ??? // error
10+
11+
}

0 commit comments

Comments
 (0)