From bbeb0ab2dd68420a1f732df6f373b4f83b31801a Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 24 Aug 2023 14:43:12 +0200 Subject: [PATCH] Elide companion defs to a `object` extending `AnyVal` Co-authored-by: Matt Bovel --- compiler/src/dotty/tools/dotc/ast/Desugar.scala | 2 +- .../src/dotty/tools/dotc/reporting/messages.scala | 13 +++++++++---- compiler/src/dotty/tools/dotc/typer/Checking.scala | 2 ++ tests/neg/i18274.check | 9 +++++++++ tests/neg/i18274.scala | 3 +++ 5 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 tests/neg/i18274.check create mode 100644 tests/neg/i18274.scala diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index c104c603422d..8a83e19562a0 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 5848d9e7cba1..65be1a88dfe2 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -1678,10 +1678,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 c17c5f25ab5f..783540f4f2a6 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -716,6 +716,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