From 275495b74e8e36a418b5e1e5fff38a71a22a565f Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 8 Jan 2024 18:16:00 +0000 Subject: [PATCH] Consider static and non-static methods as non-double def --- .../src/dotty/tools/dotc/typer/Checking.scala | 3 ++- tests/run/19394.scala | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/run/19394.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 3e5d65070a80..9594fe3f5373 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -1167,7 +1167,8 @@ trait Checking { def javaFieldMethodPair = decl.is(JavaDefined) && other.is(JavaDefined) && decl.is(Method) != other.is(Method) - if (decl.matches(other) && !javaFieldMethodPair) { + def staticNonStaticPair = decl.isScalaStatic != other.isScalaStatic + if (decl.matches(other) && !javaFieldMethodPair && !staticNonStaticPair) { def doubleDefError(decl: Symbol, other: Symbol): Unit = if (!decl.info.isErroneous && !other.info.isErroneous) report.error(DoubleDefinition(decl, other, cls), decl.srcPos) diff --git a/tests/run/19394.scala b/tests/run/19394.scala new file mode 100644 index 000000000000..51d65d30dfa8 --- /dev/null +++ b/tests/run/19394.scala @@ -0,0 +1,21 @@ +import scala.annotation.{ static, targetName } + +class Foo +object Foo: + @static def foo: String = "foo" + @targetName("foo") def foo: String = foo + +class Bar +object Bar: + @static def bar: String = "bar" + def bar: String = bar + +object Test: + def main(args: Array[String]): Unit = + assert(Foo.foo == "foo") + assert(classOf[Foo].getMethod("foo").invoke(null) == "foo") // static + assert(Foo.getClass.getMethod("foo").invoke(Foo) == "foo") // instance, on module class + + assert(Bar.bar == "bar") + assert(classOf[Bar].getMethod("bar").invoke(null) == "bar") + assert(Bar.getClass.getMethod("bar").invoke(Bar) == "bar")