Skip to content

Add error message for polymorphic methods in structured refinements #3744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public enum ErrorMessageID {
SymbolChangedSemanticsInVersionID,
UnableToEmitSwitchID,
MissingCompanionForStaticID,
PolymorphicMethodMissingTypeInParentID,
;

public int errorNumber() {
Expand Down
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2064,4 +2064,14 @@ object messages {
val explanation =
hl"An object that contains ${"@static"} members must have a companion class."
}

case class PolymorphicMethodMissingTypeInParent(rsym: Symbol, parentSym: Symbol)(implicit ctx: Context)
extends Message(PolymorphicMethodMissingTypeInParentID) {
val kind = "Syntax"
val msg = hl"polymorphic refinement $rsym without matching type in parent $parentSym is no longer allowed"
val explanation =
hl"""Polymorphic $rsym is not allowed in the structural refinement of $parentSym because
|$rsym does not override any method in $parentSym. Structural refinement does not allow for
|polymorphic methods."""
}
}
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
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) }
ctx.error(PolymorphicMethodMissingTypeInParent(rsym, tpt1.symbol), refinement.pos)
}
assignType(cpy.RefinedTypeTree(tree)(tpt1, refinements1), tpt1, refinements1, refineCls)
}

Expand Down
17 changes: 17 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1277,4 +1277,21 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val MissingCompanionForStatic(member) = messages.head
assertEquals(member.show, "method bar")
}

@Test def polymorphicMethodMissingTypeInParent =
checkMessagesAfter("frontend") {
"""
|object Test {
| import scala.reflect.Selectable.reflectiveSelectable
| def foo(x: { def get[T](a: T): Int }) = 5
|}
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx

assertMessageCount(1, messages)
val PolymorphicMethodMissingTypeInParent(rsym, parentSym) = messages.head
assertEquals("method get", rsym.show)
assertEquals("class Object", parentSym.show)
}
}