@@ -901,20 +901,26 @@ object RefChecks {
901
901
}
902
902
}
903
903
904
- /** Check that inheriting a case class does not constitute a variant refinement
904
+ /** Check that inheriting a case class or a sealed trait (or a sealed class) does not constitute a variant refinement
905
905
* of a base type of the case class. It is because of this restriction that we
906
- * can assume invariant refinement for case classes in `constrainPatternType `.
906
+ * can assume invariant refinement for these classes in `constrainSimplePatternType `.
907
907
*/
908
- def checkCaseClassInheritanceInvariant () =
908
+ def checkVariantInheritanceProblems () =
909
909
for
910
- caseCls <- clazz.info.baseClasses.tail.find(_.is(Case ))
911
- baseCls <- caseCls.info.baseClasses.tail
910
+ middle <- clazz.info.baseClasses.tail
911
+ if middle.isOneOf(CaseOrSealed )
912
+ baseCls <- middle.info.baseClasses.tail
912
913
if baseCls.typeParams.exists(_.paramVarianceSign != 0 )
913
- problem <- variantInheritanceProblems(baseCls, caseCls, i " base $baseCls" , " case " )
914
- withExplain = problem.appendExplanation:
915
- """ Refining a basetype of a case class is not allowed.
916
- |This is a limitation that enables better GADT constraints in case class patterns""" .stripMargin
917
- do report.errorOrMigrationWarning(withExplain, clazz.srcPos, MigrationVersion .Scala2to3 )
914
+ problem <- {
915
+ val middleStr = if middle.is(Case ) then " case " else if middle.is(Sealed ) then " sealed " else " "
916
+ variantInheritanceProblems(baseCls, middle, " " , middleStr)
917
+ }
918
+ do
919
+ val withExplain = problem.appendExplanation:
920
+ """ Refining a basetype of a case class or a sealed trait (or a sealed class) is not allowed.
921
+ |This is a limitation that enables better GADT constraints in case class and sealed hierarchy patterns""" .stripMargin
922
+ report.errorOrMigrationWarning(withExplain, clazz.srcPos, MigrationVersion .Scala2to3 )
923
+
918
924
checkNoAbstractMembers()
919
925
if (abstractErrors.isEmpty)
920
926
checkNoAbstractDecls(clazz)
@@ -923,7 +929,7 @@ object RefChecks {
923
929
report.error(abstractErrorMessage, clazz.srcPos)
924
930
925
931
checkMemberTypesOK()
926
- checkCaseClassInheritanceInvariant ()
932
+ checkVariantInheritanceProblems ()
927
933
}
928
934
929
935
if (! clazz.is(Trait ) && checker.checkInheritedTraitParameters) {
@@ -943,7 +949,7 @@ object RefChecks {
943
949
for {
944
950
cls <- clazz.info.baseClasses.tail
945
951
if cls.paramAccessors.nonEmpty && ! mixins.contains(cls)
946
- problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, i " parameterized base $cls " , " super" )
952
+ problem <- variantInheritanceProblems(cls, clazz.asClass.superClass, " parameterized " , " super" )
947
953
}
948
954
report.error(problem, clazz.srcPos)
949
955
}
@@ -966,7 +972,7 @@ object RefChecks {
966
972
if (combinedBT =:= thisBT) None // ok
967
973
else
968
974
Some (
969
- em """ illegal inheritance: $clazz inherits conflicting instances of $baseStr.
975
+ em """ illegal inheritance: $clazz inherits conflicting instances of ${ baseStr} base $baseCls .
970
976
|
971
977
| Direct basetype: $thisBT
972
978
| Basetype via $middleStr$middle: $combinedBT""" )
0 commit comments