-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #21918: Disallow value classes extending type aliases of AnyVal #23021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -743,7 +743,7 @@ object Checking { | |
| } | ||
|
|
||
| /** Verify classes extending AnyVal meet the requirements */ | ||
| def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(using Context): Unit = { | ||
| def checkDerivedValueClass(cdef: untpd.TypeDef, clazz: Symbol, stats: List[Tree])(using Context): Unit = { | ||
| def checkValueClassMember(stat: Tree) = stat match { | ||
| case _: TypeDef if stat.symbol.isClass => | ||
| report.error(ValueClassesMayNotDefineInner(clazz, stat.symbol), stat.srcPos) | ||
|
|
@@ -760,6 +760,15 @@ object Checking { | |
| // enum extending a value class type (AnyVal or an alias of it) | ||
| // The error message 'EnumMayNotBeValueClassesID' will take care of generating the error message (See #22236) | ||
| if (clazz.isDerivedValueClass && !clazz.isEnumAnonymClass) { | ||
| val parentOpt = cdef.rhs match { | ||
| case impl: Template => | ||
| impl.parents.headOption | ||
| case _ => None | ||
| } | ||
| val isExtendingAliasOfAnyVal = parentOpt.exists { parent => | ||
| parent.symbol.isAliasType && parent.tpe.nn.dealias =:= defn.AnyValType | ||
| } | ||
|
||
|
|
||
| if (clazz.is(Trait)) | ||
| report.error(CannotExtendAnyVal(clazz), clazz.srcPos) | ||
| if clazz.is(Module) then | ||
|
|
@@ -770,6 +779,8 @@ object Checking { | |
| report.error(ValueClassesMayNotBeAbstract(clazz), clazz.srcPos) | ||
| if (!clazz.isStatic) | ||
| report.error(ValueClassesMayNotBeContainted(clazz), clazz.srcPos) | ||
| if (isExtendingAliasOfAnyVal) | ||
| report.error(ValueClassCannotExtendAliasOfAnyVal(clazz, parentOpt.get.symbol), clazz.srcPos) | ||
| if (isDerivedValueClass(underlyingOfValueClass(clazz.asClass).classSymbol)) | ||
| report.error(ValueClassesMayNotWrapAnotherValueClass(clazz), clazz.srcPos) | ||
| else { | ||
|
|
@@ -1307,8 +1318,8 @@ trait Checking { | |
| else tpt | ||
|
|
||
| /** Verify classes extending AnyVal meet the requirements */ | ||
| def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(using Context): Unit = | ||
| Checking.checkDerivedValueClass(clazz, stats) | ||
| def checkDerivedValueClass(cdef: untpd.TypeDef, clazz: Symbol, stats: List[Tree])(using Context): Unit = | ||
| Checking.checkDerivedValueClass(cdef, clazz, stats) | ||
|
|
||
| /** Check that case classes are not inherited by case classes. | ||
| */ | ||
|
|
@@ -1689,7 +1700,7 @@ trait NoChecking extends ReChecking { | |
| override def checkNoTargetNameConflict(stats: List[Tree])(using Context): Unit = () | ||
| override def checkParentCall(call: Tree, caller: ClassSymbol)(using Context): Unit = () | ||
| override def checkSimpleKinded(tpt: Tree)(using Context): Tree = tpt | ||
| override def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(using Context): Unit = () | ||
| override def checkDerivedValueClass(cdef: untpd.TypeDef, clazz: Symbol, stats: List[Tree])(using Context): Unit = () | ||
| override def checkCaseInheritance(parentSym: Symbol, caseCls: ClassSymbol, pos: SrcPos)(using Context): Unit = () | ||
| override def checkNoForwardDependencies(vparams: List[ValDef])(using Context): Unit = () | ||
| override def checkMembersOK(tp: Type, pos: SrcPos)(using Context): Type = tp | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| type AliasToAnyVal = AnyVal | ||
| class Foo(a: Int) extends AliasToAnyVal // error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome errors is an endless source of conflict.
https://github.com/scala/scala3/pull/23002/files