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 1 commit
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, parentTpt: tpd.Tree)(implicit ctx: Context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would pass parentTpt as a Symbol.

extends Message(PolymorphicMethodMissingTypeInParentID) {
val kind = "Syntax"
val msg = hl"polymorphic refinement $rsym without matching type in parent $parentTpt is no longer allowed"
val explanation =
hl"""Polymorphic $rsym is not allowed in the structural refinement of $parentTpt because
|$rsym does not override any symbol in $parentTpt. Structural refinement does not allow for
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any method in ...

|polymorphic methods."""
}
}
2 changes: 1 addition & 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,7 @@ 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), 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, parentTpt) = messages.head
assertEquals("method get", rsym.show)
assertEquals("Object", parentTpt.show)
}
}