From c6deecd556d73dc0c80e84cf3eeefdabfeb92558 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 1 Apr 2021 22:58:49 +0200 Subject: [PATCH] Warn for usage of open modifier on traits or classes where redundant Fixes #11963 --- compiler/src/dotty/tools/dotc/typer/Checking.scala | 12 ++++++++++-- tests/neg-custom-args/fatal-warnings/i11963a.scala | 1 + tests/neg-custom-args/fatal-warnings/i11963b.scala | 1 + tests/neg-custom-args/fatal-warnings/i11963c.scala | 6 ++++++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/neg-custom-args/fatal-warnings/i11963a.scala create mode 100644 tests/neg-custom-args/fatal-warnings/i11963b.scala create mode 100644 tests/neg-custom-args/fatal-warnings/i11963c.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 9f48913d1f67..471353d6fadf 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -425,6 +425,7 @@ object Checking { /** Check that symbol's definition is well-formed. */ def checkWellFormed(sym: Symbol)(using Context): Unit = { def fail(msg: Message) = report.error(msg, sym.srcPos) + def warn(msg: Message) = report.warning(msg, sym.srcPos) def checkWithDeferred(flag: FlagSet) = if (sym.isOneOf(flag)) @@ -464,8 +465,15 @@ object Checking { fail(em"only classes can be ${(sym.flags & ClassOnlyFlags).flagsString}") if (sym.is(AbsOverride) && !sym.owner.is(Trait)) fail(AbstractOverrideOnlyInTraits(sym)) - if (sym.is(Trait) && sym.is(Final)) - fail(TraitsMayNotBeFinal(sym)) + if sym.is(Trait) then + if sym.is(Final) then + fail(TraitsMayNotBeFinal(sym)) + else if sym.is(Open) then + warn(RedundantModifier(Open)) + if sym.isAllOf(Abstract | Open) then + warn(RedundantModifier(Open)) + if sym.is(Open) && sym.isLocal then + warn(RedundantModifier(Open)) // Skip ModuleVal since the annotation will also be on the ModuleClass if (sym.hasAnnotation(defn.TailrecAnnot) && !sym.isOneOf(Method | ModuleVal)) fail(TailrecNotApplicable(sym)) diff --git a/tests/neg-custom-args/fatal-warnings/i11963a.scala b/tests/neg-custom-args/fatal-warnings/i11963a.scala new file mode 100644 index 000000000000..58d64d061162 --- /dev/null +++ b/tests/neg-custom-args/fatal-warnings/i11963a.scala @@ -0,0 +1 @@ +open trait Foo // error diff --git a/tests/neg-custom-args/fatal-warnings/i11963b.scala b/tests/neg-custom-args/fatal-warnings/i11963b.scala new file mode 100644 index 000000000000..9fae92747d53 --- /dev/null +++ b/tests/neg-custom-args/fatal-warnings/i11963b.scala @@ -0,0 +1 @@ +open abstract class Foo // error diff --git a/tests/neg-custom-args/fatal-warnings/i11963c.scala b/tests/neg-custom-args/fatal-warnings/i11963c.scala new file mode 100644 index 000000000000..ebd56e1127c8 --- /dev/null +++ b/tests/neg-custom-args/fatal-warnings/i11963c.scala @@ -0,0 +1,6 @@ +object Test { + def foo: Any = { + open class Bar // error + new Bar + } +}