Skip to content

Commit

Permalink
Add explanation to checkCaseClassInheritanceInvariant error msg (#20141)
Browse files Browse the repository at this point in the history
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]>
[Cherry-picked ed9fecc][modified]
  • Loading branch information
WojciechMazur committed Jul 5, 2024
1 parent ff1bc12 commit 68f31cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
23 changes: 14 additions & 9 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,16 @@ object RefChecks {
* can assume invariant refinement for case classes in `constrainPatternType`.
*/
def checkCaseClassInheritanceInvariant() =
for (caseCls <- clazz.info.baseClasses.tail.find(_.is(Case)))
for (baseCls <- caseCls.info.baseClasses.tail)
if (baseCls.typeParams.exists(_.paramVarianceSign != 0))
for (problem <- variantInheritanceProblems(baseCls, caseCls, "non-variant", "case "))
report.errorOrMigrationWarning(problem, clazz.srcPos, from = `3.0`)
for
caseCls <- clazz.info.baseClasses.tail.find(_.is(Case))
baseCls <- caseCls.info.baseClasses.tail
if baseCls.typeParams.exists(_.paramVarianceSign != 0)
problem <- variantInheritanceProblems(baseCls, caseCls, i"base $baseCls", "case ")
withExplain = problem.appendExplanation:
"""Refining a basetype of a case class is not allowed.
|This is a limitation that enables better GADT constraints in case class patterns""".stripMargin
do report.errorOrMigrationWarning(withExplain, clazz.srcPos, from = `3.0`)

checkNoAbstractMembers()
if (abstractErrors.isEmpty)
checkNoAbstractDecls(clazz)
Expand Down Expand Up @@ -844,7 +849,7 @@ object RefChecks {
for {
cls <- clazz.info.baseClasses.tail
if cls.paramAccessors.nonEmpty && !mixins.contains(cls)
problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, "parameterized", "super")
problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, i"parameterized base $cls", "super")
}
report.error(problem, clazz.srcPos)
}
Expand All @@ -867,7 +872,7 @@ object RefChecks {
if (combinedBT =:= thisBT) None // ok
else
Some(
em"""illegal inheritance: $clazz inherits conflicting instances of $baseStr base $baseCls.
em"""illegal inheritance: $clazz inherits conflicting instances of $baseStr.
|
| Direct basetype: $thisBT
| Basetype via $middleStr$middle: $combinedBT""")
Expand Down Expand Up @@ -966,9 +971,9 @@ object RefChecks {
end checkNoPrivateOverrides

def checkVolatile(sym: Symbol)(using Context): Unit =
if sym.isVolatile && !sym.is(Mutable) then
if sym.isVolatile && !sym.is(Mutable) then
report.warning(VolatileOnVal(), sym.srcPos)

/** Check that unary method definition do not receive parameters.
* They can only receive inferred parameters such as type parameters and implicit parameters.
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/neg/i18552.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Error: tests/neg/i18552.scala:9:6 -----------------------------------------------------------------------------------
9 |class MB(id:Int) extends MA(id) with M[B] // error
| ^
| illegal inheritance: class MB inherits conflicting instances of base trait M.
|
| Direct basetype: M[B]
| Basetype via case class MA: M[A]
|---------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Refining a basetype of a case class is not allowed.
| This is a limitation that enables better GADT constraints in case class patterns
---------------------------------------------------------------------------------------------------------------------
9 changes: 9 additions & 0 deletions tests/neg/i18552.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//> using options -explain

trait A
trait B extends A

trait M[+T]

case class MA(id:Int) extends M[A]
class MB(id:Int) extends MA(id) with M[B] // error

0 comments on commit 68f31cf

Please sign in to comment.