Skip to content

Commit 678e8e4

Browse files
committed
Disallow polymorphic refinements in stuctural types.
We can't handle them with the proposed scheme. I made a note in scala#1886.
1 parent 2bbf9ca commit 678e8e4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
10391039
for (refinement <- refinements1) { // TODO: get clarity whether we want to enforce these conditions
10401040
typr.println(s"adding refinement $refinement")
10411041
checkRefinementNonCyclic(refinement, refineCls, seen)
1042-
}
1042+
val rsym = refinement.symbol
1043+
if (rsym.info.isInstanceOf[PolyType] && rsym.allOverriddenSymbols.isEmpty)
1044+
ctx.error(i"polymorphic refinement $rsym without matching type in parent $tpt1 is no longer allowed", refinement.pos) }
10431045
assignType(cpy.RefinedTypeTree(tree)(tpt1, refinements1), tpt1, refinements1, refineCls)
10441046
}
10451047

tests/neg/structural.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Test3 {
2+
import scala.reflect.Selectable.reflectiveSelectable
3+
def g(x: { type T ; def t: T ; def f(a: T): Boolean }) = x.f(x.t) // error: no ClassTag for x.T
4+
g(new { type T = Int; def t = 4; def f(a:T) = true })
5+
g(new { type T = Any; def t = 4; def f(a:T) = true })
6+
val y: { type T = Int; def t = 4; def f(a:T) = true }
7+
= new { type T = Int; def t = 4; def f(a:T) = true }
8+
9+
def h(x: { def f[T](a: T): Int }) = x.f[Int](4) // error: polymorphic refinement method ... no longer allowed
10+
11+
}

0 commit comments

Comments
 (0)