Skip to content

Commit fdb2ced

Browse files
Backport "Consider static and non-static methods as non-double def" to LTS (#20826)
Backports #19400 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 02ebf61 + 398ffc5 commit fdb2ced

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,9 @@ class JSCodeGen()(using genCtx: Context) {
793793
name.name
794794
}.toSet
795795

796+
val staticNames = moduleClass.companionClass.info.allMembers
797+
.collect { case d if d.name.isTermName && d.symbol.isScalaStatic => d.name }.toSet
798+
796799
val members = {
797800
moduleClass.info.membersBasedOnFlags(required = Flags.Method,
798801
excluded = Flags.ExcludedForwarder).map(_.symbol)
@@ -815,6 +818,7 @@ class JSCodeGen()(using genCtx: Context) {
815818
|| hasAccessBoundary
816819
|| isOfJLObject
817820
|| m.hasAnnotation(jsdefn.JSNativeAnnot) || isDefaultParamOfJSNativeDef // #4557
821+
|| staticNames(m.name)
818822
}
819823

820824
val forwarders = for {
@@ -4769,7 +4773,7 @@ class JSCodeGen()(using genCtx: Context) {
47694773
}
47704774

47714775
private def isMethodStaticInIR(sym: Symbol): Boolean =
4772-
sym.is(JavaStatic)
4776+
sym.is(JavaStatic) || sym.isScalaStatic
47734777

47744778
/** Generate a Class[_] value (e.g. coming from classOf[T]) */
47754779
private def genClassConstant(tpe: Type)(implicit pos: Position): js.Tree =

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,8 @@ trait Checking {
11251125
def javaFieldMethodPair =
11261126
decl.is(JavaDefined) && other.is(JavaDefined) &&
11271127
decl.is(Method) != other.is(Method)
1128-
if (decl.matches(other) && !javaFieldMethodPair) {
1128+
def staticNonStaticPair = decl.isScalaStatic != other.isScalaStatic
1129+
if (decl.matches(other) && !javaFieldMethodPair && !staticNonStaticPair) {
11291130
def doubleDefError(decl: Symbol, other: Symbol): Unit =
11301131
if (!decl.info.isErroneous && !other.info.isErroneous)
11311132
report.error(DoubleDefinition(decl, other, cls), decl.srcPos)

tests/run/i17332.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package foo {
2+
3+
import annotation.static
4+
5+
class MirrorHelpers
6+
7+
object MirrorHelpers:
8+
9+
@static
10+
def throwStuff(i: Int): Any = throw new NoSuchElementException(String.valueOf(i))
11+
12+
}
13+
14+
@main def Test =
15+
try
16+
foo.MirrorHelpers.throwStuff(23)
17+
??? // ko
18+
catch case ex: NoSuchElementException if ex.getMessage == "23" => () // ok

tests/run/i19394.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.annotation.{ static, targetName }
2+
3+
class Foo
4+
object Foo:
5+
@static def foo: String = "foo"
6+
@targetName("foo") def fooBincompat: 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(Bar.bar == "bar")
17+
18+
import scala.reflect.Selectable.reflectiveSelectable
19+
assert(Foo.asInstanceOf[{ def foo: String }].foo == "foo")
20+
assert(Bar.asInstanceOf[{ def bar: String }].bar == "bar")

tests/run/i19396.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import scala.annotation.static
2+
3+
class Foo
4+
5+
object Foo {
6+
@static def foo = "foo"
7+
}
8+
9+
class Bar {
10+
def bar = Foo.foo
11+
}
12+
13+
object Test:
14+
def main(args: Array[String]): Unit =
15+
Foo.foo
16+
Bar().bar

tests/run/static/i2054.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// scalajs: --skip --pending
2-
31
import scala.annotation.static
42

53
class Test

0 commit comments

Comments
 (0)