Skip to content

Commit ed9fecc

Browse files
EugeneFlesselleaherlihynatsukagami
authored
Add explanation to checkCaseClassInheritanceInvariant error msg (#20141)
Closes #18552 which was actually not an error, see: https://github.com/scala/scala3/blob/73882c5b62b8cd96031ad975f7677949433a9f21/compiler/src/dotty/tools/dotc/typer/RefChecks.scala#L889-L893 --------- Co-authored-by: Anna Herlihy <[email protected]> Co-authored-by: Natsu Kagami <[email protected]>
1 parent 926e6a3 commit ed9fecc

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,15 @@ object RefChecks {
891891
* can assume invariant refinement for case classes in `constrainPatternType`.
892892
*/
893893
def checkCaseClassInheritanceInvariant() =
894-
for (caseCls <- clazz.info.baseClasses.tail.find(_.is(Case)))
895-
for (baseCls <- caseCls.info.baseClasses.tail)
896-
if (baseCls.typeParams.exists(_.paramVarianceSign != 0))
897-
for (problem <- variantInheritanceProblems(baseCls, caseCls, "non-variant", "case "))
898-
report.errorOrMigrationWarning(problem, clazz.srcPos, MigrationVersion.Scala2to3)
894+
for
895+
caseCls <- clazz.info.baseClasses.tail.find(_.is(Case))
896+
baseCls <- caseCls.info.baseClasses.tail
897+
if baseCls.typeParams.exists(_.paramVarianceSign != 0)
898+
problem <- variantInheritanceProblems(baseCls, caseCls, i"base $baseCls", "case ")
899+
withExplain = problem.appendExplanation:
900+
"""Refining a basetype of a case class is not allowed.
901+
|This is a limitation that enables better GADT constraints in case class patterns""".stripMargin
902+
do report.errorOrMigrationWarning(withExplain, clazz.srcPos, MigrationVersion.Scala2to3)
899903
checkNoAbstractMembers()
900904
if (abstractErrors.isEmpty)
901905
checkNoAbstractDecls(clazz)
@@ -924,7 +928,7 @@ object RefChecks {
924928
for {
925929
cls <- clazz.info.baseClasses.tail
926930
if cls.paramAccessors.nonEmpty && !mixins.contains(cls)
927-
problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, "parameterized", "super")
931+
problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, i"parameterized base $cls", "super")
928932
}
929933
report.error(problem, clazz.srcPos)
930934
}
@@ -947,7 +951,7 @@ object RefChecks {
947951
if (combinedBT =:= thisBT) None // ok
948952
else
949953
Some(
950-
em"""illegal inheritance: $clazz inherits conflicting instances of $baseStr base $baseCls.
954+
em"""illegal inheritance: $clazz inherits conflicting instances of $baseStr.
951955
|
952956
| Direct basetype: $thisBT
953957
| Basetype via $middleStr$middle: $combinedBT""")

tests/neg/i18552.check

+13
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

+9
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)