Skip to content

Can generate singleton type instances #12486

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
kynthus opened this issue May 15, 2021 · 1 comment · Fixed by #12580
Closed

Can generate singleton type instances #12486

kynthus opened this issue May 15, 2021 · 1 comment · Fixed by #12580
Assignees
Milestone

Comments

@kynthus
Copy link

kynthus commented May 15, 2021

Compiler version

3.0.0

Minimized code

val hello: String = "hello"

object MyObj {
  val a: Int = 123
  val b: Double = 456.789
  val c: String = "ABC"
}

scala> val stringFromSingleton: String = new hello.type()    // Generate `<val>.type`
val stringFromSingleton: String = ""

scala> val myObjFromSingleton: MyObj.type = new MyObj.type() // Generate `<object>.type`
val myObjFromSingleton: MyObj.type = MyObj$@2114eb1a

Output

If generate a singleton type of val, the constructor of the type of immutable variable is called.
If generate a singleton type of object, another object is generated.

Since they are separate instances, false will be returned when comparing reference by eq method.

scala> hello eq stringFromSingleton
val res1: Boolean = false

scala> MyObj eq myObjFromSingleton
val res2: Boolean = false

In addition, for non-REPL, creating a object resulted in a java.lang.IllegalAccessError.

// GenSingleton.scala

object MyObj {
  val a: Int = 123
  val b: Double = 456.789
  val c: String = "ABC"
}

object GenSingleton {
  def main(args: Array[String]): Unit = {
    val myObjFromSingleton: MyObj.type = new MyObj.type() // java.lang.IllegalAccessError
    val hello: String = "hello"
    val stringFromSingleton: String = new hello.type()
  }
}
# scalac GenSingleton.scala
# scala GenSingleton
Exception in thread "main" java.lang.IllegalAccessError: class GenSingleton$ tried to access private method 'void MyObj$.<init>()' (GenSingleton$ and MyObj$ are in unnamed module of loader 'app')
        at GenSingleton$.main(GenSingleton.scala:9)
        at GenSingleton.main(GenSingleton.scala)

Expectation

I expect that the singleton type cannot be generated by new.
It seems that generation is allowed from 3.0.0-RC2 and later, and it is a compile error 3.0.0-RC1 and earlier.

# scala -version
Starting scala3 REPL...
Scala compiler version 3.0.0-RC1 -- Copyright 2002-2021, LAMP/EPFL

val hello: String = "hello"

object MyObj {
  val a: Int = 123
  val b: Double = 456.789
  val c: String = "ABC"
}

scala> val stringFromSingleton: String = new hello.type()
1 |val stringFromSingleton: String = new hello.type()
  |                                      ^^^^^^^^^^
  |                                      (hello : String) is not a class type

scala> val myObjFromSingleton: MyObj.type = new MyObj.type()
1 |val myObjFromSingleton: MyObj.type = new MyObj.type()
  |                                         ^^^^^^^^^^
  |                                         MyObj.type is not a class type
@OlivierBlanvillain
Copy link
Contributor

Regression bisected to 25fa24e

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.

4 participants