Skip to content

Commit 57841fd

Browse files
Backport "Add explanation to checkCaseClassInheritanceInvariant error msg" to LTS (#21055)
Backports #20141 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 9c10446 + 68f31cf commit 57841fd

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -811,11 +811,16 @@ object RefChecks {
811811
* can assume invariant refinement for case classes in `constrainPatternType`.
812812
*/
813813
def checkCaseClassInheritanceInvariant() =
814-
for (caseCls <- clazz.info.baseClasses.tail.find(_.is(Case)))
815-
for (baseCls <- caseCls.info.baseClasses.tail)
816-
if (baseCls.typeParams.exists(_.paramVarianceSign != 0))
817-
for (problem <- variantInheritanceProblems(baseCls, caseCls, "non-variant", "case "))
818-
report.errorOrMigrationWarning(problem, clazz.srcPos, from = `3.0`)
814+
for
815+
caseCls <- clazz.info.baseClasses.tail.find(_.is(Case))
816+
baseCls <- caseCls.info.baseClasses.tail
817+
if baseCls.typeParams.exists(_.paramVarianceSign != 0)
818+
problem <- variantInheritanceProblems(baseCls, caseCls, i"base $baseCls", "case ")
819+
withExplain = problem.appendExplanation:
820+
"""Refining a basetype of a case class is not allowed.
821+
|This is a limitation that enables better GADT constraints in case class patterns""".stripMargin
822+
do report.errorOrMigrationWarning(withExplain, clazz.srcPos, from = `3.0`)
823+
819824
checkNoAbstractMembers()
820825
if (abstractErrors.isEmpty)
821826
checkNoAbstractDecls(clazz)
@@ -844,7 +849,7 @@ object RefChecks {
844849
for {
845850
cls <- clazz.info.baseClasses.tail
846851
if cls.paramAccessors.nonEmpty && !mixins.contains(cls)
847-
problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, "parameterized", "super")
852+
problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, i"parameterized base $cls", "super")
848853
}
849854
report.error(problem, clazz.srcPos)
850855
}
@@ -867,7 +872,7 @@ object RefChecks {
867872
if (combinedBT =:= thisBT) None // ok
868873
else
869874
Some(
870-
em"""illegal inheritance: $clazz inherits conflicting instances of $baseStr base $baseCls.
875+
em"""illegal inheritance: $clazz inherits conflicting instances of $baseStr.
871876
|
872877
| Direct basetype: $thisBT
873878
| Basetype via $middleStr$middle: $combinedBT""")
@@ -966,9 +971,9 @@ object RefChecks {
966971
end checkNoPrivateOverrides
967972

968973
def checkVolatile(sym: Symbol)(using Context): Unit =
969-
if sym.isVolatile && !sym.is(Mutable) then
974+
if sym.isVolatile && !sym.is(Mutable) then
970975
report.warning(VolatileOnVal(), sym.srcPos)
971-
976+
972977
/** Check that unary method definition do not receive parameters.
973978
* They can only receive inferred parameters such as type parameters and implicit parameters.
974979
*/

tests/neg/i18552.check

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- Error: tests/neg/i18552.scala:9:6 -----------------------------------------------------------------------------------
2+
9 |class MB(id:Int) extends MA(id) with M[B] // error
3+
| ^
4+
| illegal inheritance: class MB inherits conflicting instances of base trait M.
5+
|
6+
| Direct basetype: M[B]
7+
| Basetype via case class MA: M[A]
8+
|---------------------------------------------------------------------------------------------------------------------
9+
| Explanation (enabled by `-explain`)
10+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
11+
| Refining a basetype of a case class is not allowed.
12+
| This is a limitation that enables better GADT constraints in case class patterns
13+
---------------------------------------------------------------------------------------------------------------------

tests/neg/i18552.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//> using options -explain
2+
3+
trait A
4+
trait B extends A
5+
6+
trait M[+T]
7+
8+
case class MA(id:Int) extends M[A]
9+
class MB(id:Int) extends MA(id) with M[B] // error

0 commit comments

Comments
 (0)