Skip to content

Commit fd84324

Browse files
EugeneFlesselleaherlihynatsukagami
committed
Add explanation to checkCaseClassInheritanceInvariant error msg
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 1e8a653 commit fd84324

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,14 @@ 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, enabling better GADT constraints in case class patterns."
901+
do report.errorOrMigrationWarning(withExplain, clazz.srcPos, MigrationVersion.Scala2to3)
899902
checkNoAbstractMembers()
900903
if (abstractErrors.isEmpty)
901904
checkNoAbstractDecls(clazz)
@@ -924,7 +927,7 @@ object RefChecks {
924927
for {
925928
cls <- clazz.info.baseClasses.tail
926929
if cls.paramAccessors.nonEmpty && !mixins.contains(cls)
927-
problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, "parameterized", "super")
930+
problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, i"parameterized base $cls", "super")
928931
}
929932
report.error(problem, clazz.srcPos)
930933
}
@@ -947,7 +950,7 @@ object RefChecks {
947950
if (combinedBT =:= thisBT) None // ok
948951
else
949952
Some(
950-
em"""illegal inheritance: $clazz inherits conflicting instances of $baseStr base $baseCls.
953+
em"""illegal inheritance: $clazz inherits conflicting instances of $baseStr.
951954
|
952955
| Direct basetype: $thisBT
953956
| Basetype via $middleStr$middle: $combinedBT""")

tests/neg/i18552.check

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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, enabling better GADT constraints in case class patterns.
12+
---------------------------------------------------------------------------------------------------------------------

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)