Skip to content

Commit

Permalink
Elide companion defs to a object extending AnyVal (scala#18451)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki authored Oct 18, 2023
2 parents 7be9af0 + bbeb0ab commit f62429b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ object desugar {
}
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
companionDefs(anyRef, companionMembers)
else if (isValueClass)
else if isValueClass && !isObject then
companionDefs(anyRef, Nil)
else Nil

Expand Down
13 changes: 9 additions & 4 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1686,10 +1686,15 @@ class CannotExtendAnyVal(sym: Symbol)(using Context)
extends SyntaxMsg(CannotExtendAnyValID) {
def msg(using Context) = i"""$sym cannot extend ${hl("AnyVal")}"""
def explain(using Context) =
i"""Only classes (not traits) are allowed to extend ${hl("AnyVal")}, but traits may extend
|${hl("Any")} to become ${Green("\"universal traits\"")} which may only have ${hl("def")} members.
|Universal traits can be mixed into classes that extend ${hl("AnyVal")}.
|"""
if sym.is(Trait) then
i"""Only classes (not traits) are allowed to extend ${hl("AnyVal")}, but traits may extend
|${hl("Any")} to become ${Green("\"universal traits\"")} which may only have ${hl("def")} members.
|Universal traits can be mixed into classes that extend ${hl("AnyVal")}.
|"""
else if sym.is(Module) then
i"""Only classes (not objects) are allowed to extend ${hl("AnyVal")}.
|"""
else ""
}

class CannotExtendJavaEnum(sym: Symbol)(using Context)
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ object Checking {
if (isDerivedValueClass(clazz)) {
if (clazz.is(Trait))
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
if clazz.is(Module) then
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
if (clazz.is(Abstract))
report.error(ValueClassesMayNotBeAbstract(clazz), clazz.srcPos)
if (!clazz.isStatic)
Expand Down
9 changes: 9 additions & 0 deletions tests/neg/i18274.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- [E068] Syntax Error: tests/neg/i18274.scala:3:7 ---------------------------------------------------------------------
3 |object Foo extends AnyVal // error
| ^
| object Foo cannot extend AnyVal
|---------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Only classes (not objects) are allowed to extend AnyVal.
---------------------------------------------------------------------------------------------------------------------
3 changes: 3 additions & 0 deletions tests/neg/i18274.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//> using options -explain

object Foo extends AnyVal // error

0 comments on commit f62429b

Please sign in to comment.