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..c57a9471f345 --- /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 fooBincompat: 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")