Skip to content

Commit

Permalink
Consider static and non-static methods as non-double def
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jan 8, 2024
1 parent dce597f commit e873376
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions tests/run/19394.scala
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit e873376

Please sign in to comment.