Skip to content

Commit a0ad862

Browse files
committed
Add error message when trait parameter prefixes are used for its parent
lampepfl#1589
1 parent 45b6ebd commit a0ad862

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
145145
NoMatchingOverloadID,
146146
StableIdentPatternID,
147147
StaticFieldsShouldPrecedeNonStaticID,
148-
IllegalSuperAccessorID
148+
IllegalSuperAccessorID,
149+
TraitParameterUsedAsParentPrefixID
149150

150151
def errorNumber = ordinal - 2
151152
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,4 +2362,17 @@ object messages {
23622362
}
23632363
val explanation: String = ""
23642364
}
2365+
2366+
case class TraitParameterUsedAsParentPrefix(typ: NamedType)(implicit val ctx: Context)
2367+
extends Message(TraitParameterUsedAsParentPrefixID) {
2368+
val kind: String = "Reference"
2369+
val msg: String =
2370+
s"${typ.symbol.show} cannot extend from a parent that is derived via its own parameters"
2371+
val explanation: String =
2372+
ex"""
2373+
|${typ.symbol.show} extends from a reference to an enclosed type from its
2374+
| own parameter which is illegal. This can be renedied by removing the parameter
2375+
| prefix from it's parent.
2376+
|""".stripMargin
2377+
}
23652378
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ object RefChecks {
116116
case TypeRef(ref: TermRef, _) =>
117117
val paramRefs = ref.namedPartsWith(ntp => ntp.symbol.enclosingClass == cls)
118118
if (paramRefs.nonEmpty)
119-
ctx.error("trait parameters cannot be used as parent prefixes", parent.sourcePos)
119+
ctx.error(TraitParameterUsedAsParentPrefix(paramRefs.tail.head), parent.sourcePos)
120120
case _ =>
121121
}
122122

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,4 +1638,24 @@ class ErrorMessagesTests extends ErrorMessagesTest {
16381638
message.msg
16391639
)
16401640
}
1641+
1642+
@Test def traitParametersUsedAsParentPrefix() =
1643+
checkMessagesAfter(RefChecks.name) {
1644+
"""
1645+
|class Outer {
1646+
| trait Inner
1647+
| trait Test(val outer: Outer) extends outer.Inner
1648+
|}
1649+
|""".stripMargin
1650+
}.expect {
1651+
(ictx, messages) =>
1652+
implicit val ctx: Context = ictx
1653+
val TraitParameterUsedAsParentPrefix(typ) :: Nil = messages
1654+
assertEquals(1, messages.size)
1655+
assertEquals("trait Test", typ.symbol.show)
1656+
assertEquals(
1657+
s"${typ.symbol.show} cannot extend from a parent that is derived via its own parameters",
1658+
messages.head.msg
1659+
)
1660+
}
16411661
}

0 commit comments

Comments
 (0)