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

REPL breakages importing from REPL artifacts or root imports (depending on version) #14781

Closed
som-snytt opened this issue Mar 26, 2022 · 6 comments

Comments

@som-snytt
Copy link
Contributor

Minimized code

Welcome to Scala 3.1.1 (18, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> object sun { val x = 42 }
// defined object sun

scala> import sun.x
-- [E008] Not Found Error: ---------------------------------------------------------------------------------------------
1 |import sun.x
  |           ^
  |           value x is not a member of sun
1 error found

scala> import jdk.CollectionConverters.*
-- [E008] Not Found Error: ---------------------------------------------------------------------------------------------
1 |import jdk.CollectionConverters.*
  |       ^^^^^^^^^^^^^^^^^^^^^^^^
  |       value CollectionConverters is not a member of jdk
1 error found

scala> import _root_.
<empty>                 dotty                   javax                   org                     sun
<special-ops>           images                  jdk                     repl                    toolbarButtonGraphics
com                     java                    netscape                scala                   xsbti

or without REPL

import scala.jdk.CollectionConverters._
// not in Scala 2/3
//import jdk.CollectionConverters._
// in Scala 2
//scalac -Yimports:java,scala,scala.Predef,scala.jdk hh.scala
//import CollectionConverters._

object HelloWorld {
  def main(args: Array[String]) = {
    val ss = java.util.List.of("Hello", "world")
    println(ss.asScala.mkString("", ", ", "!"))
  }
}

Output

Random "root" symbols on the class path (see completions above) shadow things I actually care about.

Scala 2 is divided, in that REPL at least sees the sun and not its shadow:

Welcome to Scala 2.13.8 (OpenJDK 64-Bit Server VM, Java 17.0.2).
Type in expressions for evaluation. Or try :help.

scala> object sun { val x = 42 }
object sun

scala> import sun.x
import sun.x

scala> import jdk.CollectionConverters._
                  ^
       error: object CollectionConverters is not a member of package jdk

Expectation

My expectation was that I had graduated from studying Chapter 2 of the spec.

I needed CollectionConverters for a quickie test and couldn't remember how to spell it.

_root_.jdk should be lowest-priority binding of jdk, below the root context scala that makes scala.jdk available.

@som-snytt som-snytt added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 26, 2022
@odersky odersky added stat:cannot reproduce and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 26, 2022
@odersky
Copy link
Contributor

odersky commented Mar 26, 2022

I get:

sbt:scala3> repl
[info] compiling 1 Scala source to /Users/odersky/workspace/dotty/compiler/../out/bootstrap/scala3-compiler-bootstrapped/scala-3.1.3-RC1-bin-SNAPSHOT-nonbootstrapped/classes ...
Welcome to Scala 3.1.3-RC1-bin-SNAPSHOT-nonbootstrapped-git-e6c7f27 (1.8.0_231, Java Java HotSpot(TM) 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                 
scala> object sun { val x = 1 }
// defined object sun
                                                                                                                                 
scala> import sun.x
                                                                                                                                 
scala> 

@som-snytt
Copy link
Contributor Author

Confirmed [edit: partially] fixed since 3.1.1, sorry for the noise. Usually I test on head, but I was probably in the middle of an edit.

The remaining work item is the broken import jdk on the linked scala 2 ticket.

Oh wait, let me try one more time...

➜  snips ~/projects/dotty/bin/scala
Welcome to Scala 3.1.3-RC1-bin-SNAPSHOT-git-6f3fe05 (17.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> import jdk.CollectionConverters._

scala>
➜  snips vi cc.scala
➜  snips ~/projects/dotty/bin/scalac cc.scala
-- [E008] Not Found Error: cc.scala:2:11 -------------------------------------------------------------------------------
2 |import jdk.CollectionConverters._
  |       ^^^^^^^^^^^^^^^^^^^^^^^^
  |       value CollectionConverters is not a member of jdk
1 error found
➜  snips cat cc.scala

import jdk.CollectionConverters._
➜  snips ls jdk
ls: cannot access 'jdk': No such file or directory

Also verified the error in paranoid mode, clean repo on HEAD.

import scala.jdk.CollectionConverters._ compiles as expected, pasting to verify I didn't misspell it.

@odersky
Copy link
Contributor

odersky commented Mar 26, 2022

I still can't reproduce any of this.

@som-snytt
Copy link
Contributor Author

I'll reopen if I can diagnose later when the pain point recurs.

@som-snytt
Copy link
Contributor Author

Note to future self: on the scala 2 ticket, I convinced myself that the "empty root package" aka "single tree" model is not evil.

See comment at https://github.com/lampepfl/dotty/blob/release-3.2.2/compiler/src/dotty/tools/dotc/typer/Typer.scala#L186

At the language level, in the recently tweaked Scala 2 spec, the difference is between: jdk is visible in the "root package" to which all mononymic packages belong; the alternative would have been that jdk is visible by virtue of a root context that is consulted last of all.

The second model, in which a compilation unit is not a single tree, is closer to the previous (misleading) spec which implies that the scala root context sits between user code and the outermost context, much the way symbols are brought into scope by package nesting.

The friction between _root_.jdk and scala.jdk is unfortunate, and similarly _root_.io and scala.io (or java.io).

It may be possible to identify circulating loci of namespace pollution much like the huge garbage patches of the Pacific.

@som-snytt
Copy link
Contributor Author

som-snytt commented Jun 11, 2024

Just confirming that REPL was broken on 3.1.3 but fixed by 3.3.3. It had separate tickets on "priority of artifacts on class path". Edit: the OP was also correct, on 3.1.1 it correctly errors.

➜  snips ~/projects/dotty/bin/scala
Welcome to Scala 3.1.3-RC1-bin-SNAPSHOT-git-6f3fe05 (17.0.10, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> import jdk.CollectionConverters.*

scala>
➜  snips ~/projects/dotty/bin/scalac -d /tmp/sandbox jdk.scala
-- [E008] Not Found Error: jdk.scala:5:13 ------------------------------------------------------------------------------
5 |  import jdk.CollectionConverters.*
  |         ^^^^^^^^^^^^^^^^^^^^^^^^
  |         value CollectionConverters is not a member of jdk
1 error found
➜  snips scala
Welcome to Scala 3.1.3 (17.0.10, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> import jdk.CollectionConverters.*

scala>
➜  snips sdk use scala 3.3.3

Using scala version 3.3.3 in this shell.
➜  snips scala
Welcome to Scala 3.3.3 (17.0.10, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> import jdk.CollectionConverters.*
-- [E008] Not Found Error: ---------------------------------------------------------------------------------------------
1 |import jdk.CollectionConverters.*
  |       ^^^^^^^^^^^^^^^^^^^^^^^^
  |       value CollectionConverters is not a member of jdk
1 error found

➜  snips scala
Welcome to Scala 3.1.1 (17.0.10, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> import jdk.CollectionConverters.*
-- [E008] Not Found Error: ---------------------------------------------------------------------------------------------
1 |import jdk.CollectionConverters.*
  |       ^^^^^^^^^^^^^^^^^^^^^^^^
  |       value CollectionConverters is not a member of jdk
1 error found

Maybe JDK module support is the feature request.

@som-snytt som-snytt changed the title Root packages cast a long shadow REPL breakages importing from REPL artifacts or root imports (depending on version) Jun 11, 2024
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

2 participants