Skip to content

Commit 275495b

Browse files
committed
Consider static and non-static methods as non-double def
1 parent dce597f commit 275495b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,8 @@ trait Checking {
11671167
def javaFieldMethodPair =
11681168
decl.is(JavaDefined) && other.is(JavaDefined) &&
11691169
decl.is(Method) != other.is(Method)
1170-
if (decl.matches(other) && !javaFieldMethodPair) {
1170+
def staticNonStaticPair = decl.isScalaStatic != other.isScalaStatic
1171+
if (decl.matches(other) && !javaFieldMethodPair && !staticNonStaticPair) {
11711172
def doubleDefError(decl: Symbol, other: Symbol): Unit =
11721173
if (!decl.info.isErroneous && !other.info.isErroneous)
11731174
report.error(DoubleDefinition(decl, other, cls), decl.srcPos)

tests/run/19394.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import scala.annotation.{ static, targetName }
2+
3+
class Foo
4+
object Foo:
5+
@static def foo: String = "foo"
6+
@targetName("foo") def foo: String = foo
7+
8+
class Bar
9+
object Bar:
10+
@static def bar: String = "bar"
11+
def bar: String = bar
12+
13+
object Test:
14+
def main(args: Array[String]): Unit =
15+
assert(Foo.foo == "foo")
16+
assert(classOf[Foo].getMethod("foo").invoke(null) == "foo") // static
17+
assert(Foo.getClass.getMethod("foo").invoke(Foo) == "foo") // instance, on module class
18+
19+
assert(Bar.bar == "bar")
20+
assert(classOf[Bar].getMethod("bar").invoke(null) == "bar")
21+
assert(Bar.getClass.getMethod("bar").invoke(Bar) == "bar")

0 commit comments

Comments
 (0)