Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider static and non-static methods as non-double def #19400

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
dwijnand marked this conversation as resolved.
Show resolved Hide resolved

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
dwijnand marked this conversation as resolved.
Show resolved Hide resolved

/** 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 @@ -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
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