From 8e84fb013abeb613af4a3414b836f02140e2e866 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 29 Jun 2016 19:43:44 +0200 Subject: Eta-expand unapplied types that have type parameters We would like to change from a scheme where eta-expansion was prototype driven to one where unapplied parameterized types are always eta expanded. The reason is that we might miss some eta expansions due to cyclic references. run/colltest4 is an exmaple. Here, we missed an eta expansion in the type of Iterator. The class definition is: trait Iterable[+A] extends IterableOnce[A] with FromIterable[Iterable] { We'd expect that the second parent would expand to FromIterable[[X0] -> Iterable[X0]] But we miss the expansion because at the time we complete Iterable we have not completed FromIterable yet. In fact this happens in both the old and the new hk scheme. But in the old scheme we did not notice the error whereas in the new scheme we get an error in PostTyper that the type Iterable does not conform to its bound `[X0] -> Iterable[X0]`. With this commit, we change the scheme, so that eta-expansion depends on the type parameters of a type itself, instead of the expected type. We should investigate whether we can do a similar change for Scala2 classloading. Check kinds of type parameters Also, do not allow a hk type if the bound is a * type. --- src/dotty/tools/dotc/typer/Typer.scala | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src/dotty/tools/dotc/typer/Typer.scala') diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 225516503..33a94f5c7 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -928,7 +928,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit } def typedAppliedTypeTree(tree: untpd.AppliedTypeTree)(implicit ctx: Context): Tree = track("typedAppliedTypeTree") { - val tpt1 = typed(tree.tpt)(ctx retractMode Mode.Pattern) + val tpt1 = typed(tree.tpt, AnyTypeConstructorProto)(ctx.retractMode(Mode.Pattern)) val tparams = tpt1.tpe.typeParams if (tparams.isEmpty) { ctx.error(d"${tpt1.tpe} does not take type parameters", tree.pos) @@ -1672,6 +1672,17 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit } } + def adaptType(tp: Type): Tree = { + val tree1 = + if (pt != AnyTypeConstructorProto && tp.typeParamSymbols.nonEmpty) { + println(i"lam abs $tp with tparams ${tp.typeParamSymbols}%, %") + tree.withType(tree.tpe.EtaExpand(tp.typeParamSymbols)) + } + else tree + if ((ctx.mode is Mode.Pattern) || tree1.tpe <:< pt) tree1 + else err.typeMismatch(tree1, pt) + } + tree match { case _: MemberDef | _: PackageDef | _: Import | _: WithoutTypeOrPos[_] => tree case _ => tree.tpe.widen match { @@ -1705,9 +1716,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit (_, _) => tree // error will be reported in typedTypeApply } case _ => - if (ctx.mode is Mode.Type) - if ((ctx.mode is Mode.Pattern) || tree.tpe <:< pt) tree - else err.typeMismatch(tree, pt) + if (ctx.mode is Mode.Type) adaptType(tree.tpe) else adaptNoArgs(wtp) } } -- cgit v1.2.3