Skip to content

Commit 84822d8

Browse files
authored
Merge pull request #3744 from Wojtechnology/ws-error-poly-refinement
Add error message for polymorphic methods in structured refinements
2 parents 5f23632 + d957f2c commit 84822d8

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public enum ErrorMessageID {
124124
SymbolChangedSemanticsInVersionID,
125125
UnableToEmitSwitchID,
126126
MissingCompanionForStaticID,
127+
PolymorphicMethodMissingTypeInParentID,
127128
;
128129

129130
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,4 +2064,14 @@ object messages {
20642064
val explanation =
20652065
hl"An object that contains ${"@static"} members must have a companion class."
20662066
}
2067+
2068+
case class PolymorphicMethodMissingTypeInParent(rsym: Symbol, parentSym: Symbol)(implicit ctx: Context)
2069+
extends Message(PolymorphicMethodMissingTypeInParentID) {
2070+
val kind = "Syntax"
2071+
val msg = hl"polymorphic refinement $rsym without matching type in parent $parentSym is no longer allowed"
2072+
val explanation =
2073+
hl"""Polymorphic $rsym is not allowed in the structural refinement of $parentSym because
2074+
|$rsym does not override any method in $parentSym. Structural refinement does not allow for
2075+
|polymorphic methods."""
2076+
}
20672077
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11691169
checkRefinementNonCyclic(refinement, refineCls, seen)
11701170
val rsym = refinement.symbol
11711171
if (rsym.info.isInstanceOf[PolyType] && rsym.allOverriddenSymbols.isEmpty)
1172-
ctx.error(i"polymorphic refinement $rsym without matching type in parent $tpt1 is no longer allowed", refinement.pos) }
1172+
ctx.error(PolymorphicMethodMissingTypeInParent(rsym, tpt1.symbol), refinement.pos)
1173+
}
11731174
assignType(cpy.RefinedTypeTree(tree)(tpt1, refinements1), tpt1, refinements1, refineCls)
11741175
}
11751176

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,4 +1277,21 @@ class ErrorMessagesTests extends ErrorMessagesTest {
12771277
val MissingCompanionForStatic(member) = messages.head
12781278
assertEquals(member.show, "method bar")
12791279
}
1280+
1281+
@Test def polymorphicMethodMissingTypeInParent =
1282+
checkMessagesAfter("frontend") {
1283+
"""
1284+
|object Test {
1285+
| import scala.reflect.Selectable.reflectiveSelectable
1286+
| def foo(x: { def get[T](a: T): Int }) = 5
1287+
|}
1288+
""".stripMargin
1289+
}.expect { (ictx, messages) =>
1290+
implicit val ctx: Context = ictx
1291+
1292+
assertMessageCount(1, messages)
1293+
val PolymorphicMethodMissingTypeInParent(rsym, parentSym) = messages.head
1294+
assertEquals("method get", rsym.show)
1295+
assertEquals("class Object", parentSym.show)
1296+
}
12801297
}

0 commit comments

Comments
 (0)