diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index 6024eab29722..583afa8dc39c 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -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 diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index ab896eabeb6d..fd6fed05b4d0 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -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) diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index c98be88487f5..a16e2192e558 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -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) diff --git a/tests/neg/i18274.check b/tests/neg/i18274.check new file mode 100644 index 000000000000..2535d641451c --- /dev/null +++ b/tests/neg/i18274.check @@ -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. + --------------------------------------------------------------------------------------------------------------------- diff --git a/tests/neg/i18274.scala b/tests/neg/i18274.scala new file mode 100644 index 000000000000..19c27b969d98 --- /dev/null +++ b/tests/neg/i18274.scala @@ -0,0 +1,3 @@ +//> using options -explain + +object Foo extends AnyVal // error