File tree 4 files changed +38
-2
lines changed
test/dotty/tools/dotc/reporting
4 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -145,7 +145,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
145
145
NoMatchingOverloadID ,
146
146
StableIdentPatternID ,
147
147
StaticFieldsShouldPrecedeNonStaticID ,
148
- IllegalSuperAccessorID
148
+ IllegalSuperAccessorID ,
149
+ TraitParameterUsedAsParentPrefixID
149
150
150
151
def errorNumber = ordinal - 2
151
152
}
Original file line number Diff line number Diff line change @@ -2362,4 +2362,20 @@ object messages {
2362
2362
}
2363
2363
val explanation : String = " "
2364
2364
}
2365
+
2366
+ case class TraitParameterUsedAsParentPrefix (cls : Symbol )(implicit val ctx : Context )
2367
+ extends Message (TraitParameterUsedAsParentPrefixID ) {
2368
+ val kind : String = " Reference"
2369
+ val msg : String =
2370
+ s " ${cls.show} cannot extend from a parent that is derived via its own parameters "
2371
+ val explanation : String =
2372
+ ex """
2373
+ |The parent class/trait that ${cls.show} extends from is obtained from
2374
+ |the parameter of ${cls.show}. This is disallowed in order to prevent
2375
+ |outer-related Null Pointer Exceptions in Scala.
2376
+ |
2377
+ |In order to fix this issue consider directly extending from the parent rather
2378
+ |than obtaining it from the parameters of ${cls.show}.
2379
+ | """ .stripMargin
2380
+ }
2365
2381
}
Original file line number Diff line number Diff line change @@ -116,7 +116,7 @@ object RefChecks {
116
116
case TypeRef (ref : TermRef , _) =>
117
117
val paramRefs = ref.namedPartsWith(ntp => ntp.symbol.enclosingClass == cls)
118
118
if (paramRefs.nonEmpty)
119
- ctx.error(" trait parameters cannot be used as parent prefixes " , parent.sourcePos)
119
+ ctx.error(TraitParameterUsedAsParentPrefix (cls) , parent.sourcePos)
120
120
case _ =>
121
121
}
122
122
Original file line number Diff line number Diff line change @@ -1638,4 +1638,23 @@ class ErrorMessagesTests extends ErrorMessagesTest {
1638
1638
message.msg
1639
1639
)
1640
1640
}
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 (cls) :: Nil = messages
1654
+ assertEquals(" trait Test" , cls.show)
1655
+ assertEquals(
1656
+ s " ${cls.show} cannot extend from a parent that is derived via its own parameters " ,
1657
+ messages.head.msg
1658
+ )
1659
+ }
1641
1660
}
You can’t perform that action at this time.
0 commit comments