Skip to content

Runtime crash of @static methods #11100

Closed
@liufengyun

Description

@liufengyun

Minimized code

import scala.annotation.static

class C {
  val a: Int = 3
  class D
  object D {
    @static def foo: Int = a * a
  }
}

@main
def Test =
  val c = new C
  println(c.D.foo)

Output

java.lang.NoSuchFieldError: MODULE$
	at C$D.foo(main.scala:7)
	at main$package$.Test(main.scala:14)
	at Test.main(main.scala:11)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sbt.Run.invokeMain(Run.scala:115)
	at sbt.Run.execute$1(Run.scala:79)
	at sbt.Run.$anonfun$runWithLoader$4(Run.scala:92)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
	at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:10)
	at sbt.TrapExit$App.run(TrapExit.scala:257)
	at java.lang.Thread.run(Thread.java:748)

Expectation

We should probably only allow @static methods to be defined in a static object.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions