Skip to content

Add error message when trait parameter prefixes are used for its parent #7393

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 1 commit into from
Oct 13, 2019
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 @@ -145,7 +145,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
NoMatchingOverloadID,
StableIdentPatternID,
StaticFieldsShouldPrecedeNonStaticID,
IllegalSuperAccessorID
IllegalSuperAccessorID,
TraitParameterUsedAsParentPrefixID

def errorNumber = ordinal - 2
}
16 changes: 16 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2362,4 +2362,20 @@ object messages {
}
val explanation: String = ""
}

case class TraitParameterUsedAsParentPrefix(cls: Symbol)(implicit val ctx: Context)
extends Message(TraitParameterUsedAsParentPrefixID) {
val kind: String = "Reference"
val msg: String =
s"${cls.show} cannot extend from a parent that is derived via its own parameters"
val explanation: String =
ex"""
|The parent class/trait that ${cls.show} extends from is obtained from
|the parameter of ${cls.show}. This is disallowed in order to prevent
|outer-related Null Pointer Exceptions in Scala.
|
|In order to fix this issue consider directly extending from the parent rather
|than obtaining it from the parameters of ${cls.show}.
|""".stripMargin
}
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ object RefChecks {
case TypeRef(ref: TermRef, _) =>
val paramRefs = ref.namedPartsWith(ntp => ntp.symbol.enclosingClass == cls)
if (paramRefs.nonEmpty)
ctx.error("trait parameters cannot be used as parent prefixes", parent.sourcePos)
ctx.error(TraitParameterUsedAsParentPrefix(cls), parent.sourcePos)
case _ =>
}

Expand Down
19 changes: 19 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1638,4 +1638,23 @@ class ErrorMessagesTests extends ErrorMessagesTest {
message.msg
)
}

@Test def traitParametersUsedAsParentPrefix() =
checkMessagesAfter(RefChecks.name) {
"""
|class Outer {
| trait Inner
| trait Test(val outer: Outer) extends outer.Inner
|}
|""".stripMargin
}.expect {
(ictx, messages) =>
implicit val ctx: Context = ictx
val TraitParameterUsedAsParentPrefix(cls) :: Nil = messages
assertEquals("trait Test", cls.show)
assertEquals(
s"${cls.show} cannot extend from a parent that is derived via its own parameters",
messages.head.msg
)
}
}