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

Type of package #18109

Closed
prolativ opened this issue Jun 30, 2023 · 2 comments · Fixed by #18232
Closed

Type of package #18109

prolativ opened this issue Jun 30, 2023 · 2 comments · Fixed by #18232
Assignees
Labels
area:typer itype:bug regression This worked in a previous version but doesn't anymore
Milestone

Comments

@prolativ
Copy link
Contributor

Compiler version

3.3.0, 3.3.2-RC1-bin-20230629-90c59bd-NIGHTLY

Minimized code

package foo {}

package bar {
  object Test {
    def qux[A] = 123
    def main(args: Array[String]): Unit = {
      val y = qux[foo.type]
      val x = valueOf[foo.type]
    }
  }
}

Compilation output

Warning: mocking up superclass for module class foo

Runtime output

Exception in thread "main" java.lang.NoClassDefFoundError: foo
        at bar.Test$.main(PackageType.scala:8)
        at bar.Test.main(PackageType.scala)
Caused by: java.lang.ClassNotFoundException: foo
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
        ... 2 more

Expectation

Should it be possible to refer to the type of a package at all? It seems harmless if one treats it as a phantom type (the snippet doesn't crash at runtime if val x = valueOf[foo.type] gets commented out but still leaving the invocation of qux[foo.type]). On the other hand the compiler sees such types as subtypes of Singleton and tries to provide a given instance of ValueOf for them.
Neither scala 3.2.2 nor 2.13.11 allow referring to types of packages as shown below:

  • scala 3.2.2
[error] ./PackageType.scala:7:19
[error] package foo is not a value
[error]       val y = qux[foo.type]
[error]                   ^^^
[error] ./PackageType.scala:8:23
[error] package foo is not a value
[error]       val x = valueOf[foo.type]
[error]                       ^^^
  • scala 2.13.11
[error] ./PackageType.scala:7:15
[error] type arguments [foo.type] do not conform to method qux's type parameter bounds [A]
[error]       val y = qux[foo.type]
[error]               ^^^^^^^^^^^^^
[error] ./PackageType.scala:8:15
[error] type arguments [foo.type] do not conform to method valueOf's type parameter bounds [T]
[error]       val x = valueOf[foo.type]
[error]               ^^^^^^^^^^^^^^^^^

@odersky should we keep the possibility to refer to package types but make it safe at runtime or should this be disabled? Would allowing such references actually require a change in the language specification (introduced by a SIP)?

@prolativ prolativ added itype:bug area:typer stat:needs spec regression This worked in a previous version but doesn't anymore labels Jun 30, 2023
@odersky
Copy link
Contributor

odersky commented Jun 30, 2023

I think it should be forbidden. Packages are not values, so it makes no sense to define a singleton type for them. I wonder how it regressed from 3.2.2, though?

@prolativ
Copy link
Contributor Author

prolativ commented Jul 3, 2023

Output from the bisect script:

Last good release: 3.3.0-RC1-bin-20221209-231f9ab-NIGHTLY
First bad release: 3.3.0-RC1-bin-20221213-5929a50-NIGHTLY

bisect found first bad commitPrevious HEAD position was ebb2b223ce Don't search implicit arguments in singleton type prefix

which seems to point here ebb2b22

@Kordyjan Kordyjan added the Spree Suitable for a future Spree label Jul 17, 2023
odersky added a commit to dotty-staging/dotty that referenced this issue Jul 17, 2023
@odersky odersky removed the Spree Suitable for a future Spree label Jul 18, 2023
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Kordyjan pushed a commit to dotty-staging/dotty that referenced this issue Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:typer itype:bug regression This worked in a previous version but doesn't anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants