Skip to content

Commit

Permalink
Backport "Consider static and non-static methods as non-double def" t…
Browse files Browse the repository at this point in the history
…o LTS (#20826)

Backports #19400 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jun 28, 2024
2 parents 02ebf61 + 398ffc5 commit fdb2ced
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,9 @@ class JSCodeGen()(using genCtx: Context) {
name.name
}.toSet

val staticNames = moduleClass.companionClass.info.allMembers
.collect { case d if d.name.isTermName && d.symbol.isScalaStatic => d.name }.toSet

val members = {
moduleClass.info.membersBasedOnFlags(required = Flags.Method,
excluded = Flags.ExcludedForwarder).map(_.symbol)
Expand All @@ -815,6 +818,7 @@ class JSCodeGen()(using genCtx: Context) {
|| hasAccessBoundary
|| isOfJLObject
|| m.hasAnnotation(jsdefn.JSNativeAnnot) || isDefaultParamOfJSNativeDef // #4557
|| staticNames(m.name)
}

val forwarders = for {
Expand Down Expand Up @@ -4769,7 +4773,7 @@ class JSCodeGen()(using genCtx: Context) {
}

private def isMethodStaticInIR(sym: Symbol): Boolean =
sym.is(JavaStatic)
sym.is(JavaStatic) || sym.isScalaStatic

/** Generate a Class[_] value (e.g. coming from classOf[T]) */
private def genClassConstant(tpe: Type)(implicit pos: Position): js.Tree =
Expand Down
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 @@ -1125,7 +1125,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
18 changes: 18 additions & 0 deletions tests/run/i17332.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package foo {

import annotation.static

class MirrorHelpers

object MirrorHelpers:

@static
def throwStuff(i: Int): Any = throw new NoSuchElementException(String.valueOf(i))

}

@main def Test =
try
foo.MirrorHelpers.throwStuff(23)
??? // ko
catch case ex: NoSuchElementException if ex.getMessage == "23" => () // ok
20 changes: 20 additions & 0 deletions tests/run/i19394.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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(Bar.bar == "bar")

import scala.reflect.Selectable.reflectiveSelectable
assert(Foo.asInstanceOf[{ def foo: String }].foo == "foo")
assert(Bar.asInstanceOf[{ def bar: String }].bar == "bar")
16 changes: 16 additions & 0 deletions tests/run/i19396.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import scala.annotation.static

class Foo

object Foo {
@static def foo = "foo"
}

class Bar {
def bar = Foo.foo
}

object Test:
def main(args: Array[String]): Unit =
Foo.foo
Bar().bar
2 changes: 0 additions & 2 deletions tests/run/static/i2054.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// scalajs: --skip --pending

import scala.annotation.static

class Test
Expand Down

0 comments on commit fdb2ced

Please sign in to comment.