Skip to content

Opaque types are not usable in the REPL under Java 9+ #11884

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
mpilquist opened this issue Mar 24, 2021 · 6 comments
Closed

Opaque types are not usable in the REPL under Java 9+ #11884

mpilquist opened this issue Mar 24, 2021 · 6 comments

Comments

@mpilquist
Copy link
Contributor

mpilquist commented Mar 24, 2021

Compiler version

3.0.0-RC1

Minimized code

  scala3-repl
scala> opaque type Foo = Int
1 |opaque type Foo = Int
  |^^^^^^
  |Illegal start of statement: no modifiers allowed here

scala> object Module:
     |   opaque type Foo = Int
     |   object Foo:
     |     def fromInt(i: Int): Foo = i
     |
// defined object Module

scala> import Module.*

scala> def bar(f: Foo) = println(f)
1 |def bar(f: Foo) = println(f)
  |           ^^^
  |           Not found: type Foo

scala> def bar(f: Module.Foo) = println(f)
1 |def bar(f: Module.Foo) = println(f)
  |           ^^^^^^^^^^
  |           type Foo is not a member of object Module

scala> :reset

scala> object Module:
     |   opaque type Foo = Int
     |   object Foo:
     |     def fromInt(i: Int): Foo = i
     |   def bar(f: Foo) = println(f)
     |   def baz = bar(42)
     |
// defined object Module

Expectation

The top-level definition bug is tracked here: #9879

Not sure what's going on with (1) inability to reference an opaque type defined in an object and (2) lack of opaque type enforcement in the last Module definition. Feels like something about the REPL encoding scheme is at odds with the opaque type implementation scheme.

@bishabosha
Copy link
Member

bishabosha commented Mar 24, 2021

I can't reproduce this, I tried homebrew distribution and coursier scala3-repl and they both work.

Perhaps there is some other Module on the classpath that interferes, e.g. if I compile object Module and add it to the classpath then I get the same error.

@mpilquist
Copy link
Contributor Author

Aha! It works for me when running RC1 REPL under Java 8 and fails on Java 9+

  java -version
openjdk version "9"
OpenJDK Runtime Environment (build 9+181)
OpenJDK 64-Bit Server VM (build 9+181, mixed mode)
  scala3-repl
scala> object Module:
     |   opaque type Foo = Int
     |   object Foo:
     |     def fromInt(i: Int): Foo = i
     |
// defined object Module

scala> def bar(f: Module.Foo) = println(f)
1 |def bar(f: Module.Foo) = println(f)
  |           ^^^^^^^^^^
  |           type Foo is not a member of object Module

scala>
  setjdk 1.8
  java -version
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_272-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.272-b10, mixed mode)
  scala3-repl
scala> object Module:
     |   opaque type Foo = Int
     |   object Foo:
     |     def fromInt(i: Int): Foo = i
     |
// defined object Module

scala> def bar(f: Module.Foo) = println(f)
def bar(f: Module.Foo): Unit

scala>

@mpilquist mpilquist changed the title Opaque types are not usable in the REPL Opaque types are not usable in the REPL under Java 9+ Mar 25, 2021
@bishabosha
Copy link
Member

Thanks, yes changing to JDK 14 breaks it for me

@griggt
Copy link
Contributor

griggt commented Mar 25, 2021

Is this not a duplicate of #11146? Because java.lang.Module exists since JDK 9

@bishabosha
Copy link
Member

bishabosha commented Mar 25, 2021

Is this not a duplicate of #11146? Because java.lang.Module exists since JDK 9

yes I would say so, thank you for spotting

@mpilquist
Copy link
Contributor Author

Wow, great find. I almost never use the word Module in Scala and just happened to do so here to work around #9879.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants