From 678e8e47b630786df7548c1be5bee744342f826c Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 28 Jan 2017 17:11:56 +1100 Subject: [PATCH] Disallow polymorphic refinements in stuctural types. We can't handle them with the proposed scheme. I made a note in #1886. --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 4 +++- tests/neg/structural.scala | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/neg/structural.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index ec4c135f74c2..48636c27c430 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1039,7 +1039,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit for (refinement <- refinements1) { // TODO: get clarity whether we want to enforce these conditions typr.println(s"adding refinement $refinement") checkRefinementNonCyclic(refinement, refineCls, seen) - } + val rsym = refinement.symbol + if (rsym.info.isInstanceOf[PolyType] && rsym.allOverriddenSymbols.isEmpty) + ctx.error(i"polymorphic refinement $rsym without matching type in parent $tpt1 is no longer allowed", refinement.pos) } assignType(cpy.RefinedTypeTree(tree)(tpt1, refinements1), tpt1, refinements1, refineCls) } diff --git a/tests/neg/structural.scala b/tests/neg/structural.scala new file mode 100644 index 000000000000..aab52b2cb74d --- /dev/null +++ b/tests/neg/structural.scala @@ -0,0 +1,11 @@ +object Test3 { + import scala.reflect.Selectable.reflectiveSelectable + def g(x: { type T ; def t: T ; def f(a: T): Boolean }) = x.f(x.t) // error: no ClassTag for x.T + g(new { type T = Int; def t = 4; def f(a:T) = true }) + g(new { type T = Any; def t = 4; def f(a:T) = true }) + val y: { type T = Int; def t = 4; def f(a:T) = true } + = new { type T = Int; def t = 4; def f(a:T) = true } + + def h(x: { def f[T](a: T): Int }) = x.f[Int](4) // error: polymorphic refinement method ... no longer allowed + +}