Skip to content

Runtime crash of @static methods #11100

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

Closed
liufengyun opened this issue Jan 13, 2021 · 0 comments · Fixed by #11126
Closed

Runtime crash of @static methods #11100

liufengyun opened this issue Jan 13, 2021 · 0 comments · Fixed by #11126
Assignees
Milestone

Comments

@liufengyun
Copy link
Contributor

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.

@liufengyun liufengyun self-assigned this Jan 13, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this issue Jan 14, 2021
liufengyun added a commit to dotty-staging/dotty that referenced this issue Jan 15, 2021
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants