aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2017-04-10 16:18:05 +0200
committerMartin Odersky <odersky@gmail.com>2017-04-10 16:18:23 +0200
commitbb0faff0d45f873fff9054fd8baec92eabb78c3d (patch)
tree1ab4f4dc9d5d1a1a3ca2d7f632b1aebe4ea59e41
parentb3d683a4088f3db894c026070449637ec74e92fd (diff)
downloaddotty-bb0faff0d45f873fff9054fd8baec92eabb78c3d.tar.gz
dotty-bb0faff0d45f873fff9054fd8baec92eabb78c3d.tar.bz2
dotty-bb0faff0d45f873fff9054fd8baec92eabb78c3d.zip
Skolemize arguments to dependent methods as necessary.
This was missing before, led to errors not being detected.
-rw-r--r--compiler/src/dotty/tools/dotc/typer/Applications.scala5
-rw-r--r--compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala20
-rw-r--r--tests/neg/i2142.scala28
3 files changed, 49 insertions, 4 deletions
diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala
index 4e43e429b..fcc1a3b72 100644
--- a/compiler/src/dotty/tools/dotc/typer/Applications.scala
+++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala
@@ -395,9 +395,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
def addTyped(arg: Arg, formal: Type): Type => Type = {
addArg(typedArg(arg, formal), formal)
if (methodType.isParamDependent)
- _.substParam(methodType.newParamRef(n), typeOfArg(arg))
- else
- identity
+ substArgForParam(_, typeOfArg(arg), methodType.paramRefs(n))
+ else identity
}
def missingArg(n: Int): Unit = {
diff --git a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
index 2aa7036b4..d04b16451 100644
--- a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
+++ b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
@@ -315,10 +315,28 @@ trait TypeAssigner {
}
}
+ /** Substitute argument type `argType` for parameter `pref` in type `tp`,
+ * skolemizing the argument type if it is not stable and `pref` occurs in `tp`.
+ */
+ def substArgForParam(tp: Type, argType: Type, pref: ParamRef)(implicit ctx: Context) = {
+ val tp1 = tp.substParam(pref, argType)
+ if ((tp1 eq tp) || argType.isStable) tp1
+ else tp.substParam(pref, SkolemType(argType.widen))
+ }
+
def assignType(tree: untpd.Apply, fn: Tree, args: List[Tree])(implicit ctx: Context) = {
val ownType = fn.tpe.widen match {
case fntpe: MethodType =>
- if (sameLength(fntpe.paramInfos, args) || ctx.phase.prev.relaxedTyping) fntpe.instantiate(args.tpes)
+ def substArgsForParams(tp: Type, args: List[Tree], params: List[ParamRef]): Type = params match {
+ case param :: params1 =>
+ val tp1 = substArgForParam(tp, args.head.tpe, param)
+ substArgsForParams(tp1, args.tail, params1)
+ case Nil =>
+ tp
+ }
+ if (sameLength(fntpe.paramInfos, args) || ctx.phase.prev.relaxedTyping)
+ if (fntpe.isDependent) substArgsForParams(fntpe.resultType, args, fntpe.paramRefs)
+ else fntpe.resultType
else
errorType(i"wrong number of arguments for $fntpe: ${fn.tpe}, expected: ${fntpe.paramInfos.length}, found: ${args.length}", tree.pos)
case t =>
diff --git a/tests/neg/i2142.scala b/tests/neg/i2142.scala
new file mode 100644
index 000000000..7aeef95f0
--- /dev/null
+++ b/tests/neg/i2142.scala
@@ -0,0 +1,28 @@
+object Foo {
+
+class A
+val a1 = new A()
+val a2 = new A()
+
+def f(x: A, y: x.type) = ()
+f(a1, a1) // ok
+f(a1, a2) // error
+f(new A(), new A()) // error
+f(new A(), a1) // error
+
+def g(x: A)(y: x.type) = ()
+g(a1)(a1) // ok
+g(a1)(a2) // error
+g(new A())(new A()) // error
+g(new A())(a1) // error
+
+val x0 = g(new A()) _
+x0 (new A()) // error
+
+class C[T]
+
+def h(x: A): C[x.type] = ???
+val x = h(a1)
+val y = h(new A())
+
+}