Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is this runtime error an intended behavior?#24216

Discussion options

Search Keywords

inline NoClassDefFoundError

Summary

When a package-private function is inlined outside its package, aNoClassDefFoundError (ClassNotFoundException) occurs at runtime.
Is this the expected behavior?

Reproduction Code

/src/main/scala/io/github/foo/internal/function.scala

packageio.github.foo.internalprivate[foo]defpackagePrivateMethod():String="Hello"

/src/main/scala/io/github/foo/Main.scala

packageio.github.foo// Even if you write it like this, the same error occurs.// package internal {//   private[foo] def packagePrivateMethod(): String = "Hello"// }inlinedefhello():Unit= {valgreeting= internal.packagePrivateMethod()  println(greeting)}@maindefmain():Unit= {  hello()}

build.sbt

name := "scala-sand-box"version := "0.1"scalaVersion := "3.7.2"

Error

  • java.lang.NoClassDefFoundError: io/github/foo/internal
  • java.lang.ClassNotFoundException: io.github.foo.internal
Exception in thread "sbt-bg-threads-1" java.lang.NoClassDefFoundError: io/github/foo/internal        at io.github.foo.Main$package$.main(Main.scala:4)        at io.github.foo.main.main(Main.scala:8)        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)        at java.base/java.lang.reflect.Method.invoke(Method.java:580)        at sbt.Run.invokeMain(Run.scala:144)        at sbt.Run.execute$1(Run.scala:94)        at sbt.Run.$anonfun$runWithLoader$5(Run.scala:121)        at sbt.Run$.executeSuccess(Run.scala:187)        at sbt.Run.runWithLoader(Run.scala:121)        at sbt.Defaults$.$anonfun$bgRunTask$6(Defaults.scala:1988)        at sbt.Defaults$.$anonfun$termWrapper$2(Defaults.scala:1927)        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)        at scala.util.Try$.apply(Try.scala:213)        at sbt.internal.BackgroundThreadPool$BackgroundRunnable.run(DefaultBackgroundJobService.scala:367)        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)        at java.base/java.lang.Thread.run(Thread.java:1583)Caused by: java.lang.ClassNotFoundException: io.github.foo.internal        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)        at sbt.internal.ManagedClassLoader.findClass(ManagedClassLoader.java:103)        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:593)        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)        ... 17 more

Environment

  • Scala 3.7.2
  • sbt 1.9.6
  • Microsoft jdk-21.0.0.35-hotspot
  • Windows 11 24H2 (26100.6899)

Edited:

After adding the import forpackagePrivateMethod, it worked correctly.

packageio.github.fooimportio.github.foo.internal.packagePrivateMethodinlinedefhello():Unit= {valgreeting= packagePrivateMethod()  println(greeting)}@maindefmain():Unit= {  hello()}
You must be logged in to vote

In that case, then it looks like a bug.

At typer, the call to qualified access looks like the following, where the special accessor takes the enclosing object, which should be the package object.

    inline def hello(): Unit =      {        val greeting: String =          io.github.foo.inline$packagePrivateMethod$i1(io.github.foo.internal)()        println(greeting)      }:Unit    @main def main(): Unit =      {        io.github.foo.hello()      }    def inline$packagePrivateMethod$i1(x$0: io.github.foo.internal)(): String =      x$0.packagePrivateMethod()

For simplicity, I named my fileharry1.scala:

package io.github.foo.internal {  final lazy module val harry1$package: io.…

Replies: 1 comment 3 replies

Comment options

It's unusual to have more than one dot in a file name.

That matters because the package object name is derived from its file name.

There is a PR to introduce a warning:#22707

The package name `i22670-macro$package` will be encoded on the classpath, and can lead to undefined behaviour.

A related issue was thatscala-cli usesmysource.test.scala for tests. That was a problem for scaladoc.

I don't recall if the use case (arbitrary file names) was intended or expected to be supported.

You must be logged in to vote
3 replies
@harry0000
Comment options

It's unusual to have more than one dot in a file name.

Ah, sorry—that was my mistake.
Because I copied it from the error message, the directory name was mistakenly written with dots instead of slashes. I've corrected the text to use the proper directory path.

Incorrect:io.github.foo.internal.function.scala
Correct:io/github/foo/internal/function.scala

@som-snytt
Comment options

In that case, then it looks like a bug.

At typer, the call to qualified access looks like the following, where the special accessor takes the enclosing object, which should be the package object.

    inline def hello(): Unit =      {        val greeting: String =          io.github.foo.inline$packagePrivateMethod$i1(io.github.foo.internal)()        println(greeting)      }:Unit    @main def main(): Unit =      {        io.github.foo.hello()      }    def inline$packagePrivateMethod$i1(x$0: io.github.foo.internal)(): String =      x$0.packagePrivateMethod()

For simplicity, I named my fileharry1.scala:

package io.github.foo.internal {  final lazy module val harry1$package: io.github.foo.internal.harry1$package =    new io.github.foo.internal.harry1$package()  final module class harry1$package() extends Object() {    this: io.github.foo.internal.harry1$package.type =>    private[foo] def packagePrivateMethod(): String = "Hello"  }}
Answer selected byharry0000
@harry0000
Comment options

Thank you for the answer!
I’ve created an issue to report this as a possible compiler bug.

#24219

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
2 participants
@harry0000@som-snytt

[8]ページ先頭

©2009-2025 Movatter.jp