bug icon indicating copy to clipboard operation
bug copied to clipboard

protected class definition is visible in inner scopes via import statement

Open unkarjedy opened this issue 3 years ago • 4 comments

related https://youtrack.jetbrains.com/issue/SCL-20190 might be related: https://github.com/scala/bug/issues/11554

Reproduction steps

ThisBuild / version := "0.1.0-SNAPSHOT"

ThisBuild / scalaVersion := "2.12.15"

lazy val root = (project in file("."))
  .settings(
    name := "untitled9",
    libraryDependencies ++= Seq(
      "org.apache.spark" %% "spark-sql" % "3.2.1",
    )
  )

org.apache.spark.sql.types.AtomicType is protected[sql] Note, it's companion object is public.

These examples are expectedly not compiled, producing compilation error:

object Main {
  val a: org.apache.spark.sql.types.AtomicType = ???
}
import org.apache.spark.sql.types.AtomicType

object Main {
  val a: AtomicType = ???
}

These are unexpectedly compiled fine without any compilation errors:

import org.apache.spark.sql.types.AtomicType

object Main {
  {
    val a: AtomicType = ???
  }
}
import org.apache.spark.sql.types.AtomicType

object Main {
  def foo = {
    val a: AtomicType = ???
  }
}

unkarjedy avatar May 11 '22 09:05 unkarjedy

Curious if the bug also exists in Scala 3.

SethTisue avatar Jun 30 '22 14:06 SethTisue

Gee, I get the opposite symptom, where it errors on the locals but not the member (both 2.12 and 2.13):

scalac -d out -cp out client.scala
client.scala:9: error: not found: type Thing
      def inU: Thing = ???
               ^
client.scala:14: error: not found: type Thing
      def inF: Thing = ???
               ^
client.scala:19: error: not found: type Thing
      def inBraces: Thing = ???
                    ^
three errors found

where as in spark there is a public companion (doesn't seem to matter)

package p.q.r.s {
  protected[r] class Thing
  object Thing
}

and

package p {
  import p.q.r.s._

  object Test {
    def t: Thing = ???

    def u: Any = locally {
      def inU: Thing = ???
      null
    }

    def f = {
      def inF: Thing = ???
      null
    }

    {
      def inBraces: Thing = ???
      ()
    }
  }

  package q.r {
    object OK {
      def t: Thing = ???
    }
  }
}

som-snytt avatar Jun 30 '22 16:06 som-snytt

Scalac 3 correctly issues 4 errors:

scalac -d out -cp out client.scala
-- [E006] Not Found Error: client.scala:6:11 ---------------------------------------------------------------------------
6 |    def t: Thing = ???
  |           ^^^^^
  |           Not found: type Thing

longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: client.scala:9:15 ---------------------------------------------------------------------------
9 |      def inU: Thing = ???
  |               ^^^^^
  |               Not found: type Thing

longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: client.scala:14:15 --------------------------------------------------------------------------
14 |      def inF: Thing = ???
   |               ^^^^^
   |               Not found: type Thing

longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: client.scala:19:20 --------------------------------------------------------------------------
19 |      def inBraces: Thing = ???
   |                    ^^^^^
   |                    Not found: type Thing

longer explanation available when compiling with `-explain`
4 errors found

som-snytt avatar Jun 30 '22 16:06 som-snytt

OK, I confirmed the first example. It errors with val but not with def. Ha! Just when I thought I understood how anything works. (Or doesn't work.)

However, I could not reproduce the "compiles fine":

➜  example sbt "; clean; compile"
[info] welcome to sbt 1.6.2 (Oracle Corporation Java 18.0.1.1)
[info] loading global plugins from .../.sbt/1.0/plugins
[info] loading project definition from .../t12589/example/project
[info] loading settings for project root from build.sbt ...
[info] set current project to untitled9 (in build file:.../t12589/example/)
[success] Total time: 0 s, completed Jun 30, 2022, 9:42:08 AM
[info] compiling 1 Scala source to .../t12589/example/target/scala-2.12/classes ...
[error] .../t12589/example/src/main/scala/example.scala:9:14: not found: type AtomicType
[error]     def inF: AtomicType = ???     // error!
[error]              ^
[error] .../t12589/example/src/main/scala/example.scala:13:14: not found: type AtomicType
[error]     val inG: AtomicType = ???     // error!
[error]              ^
[error] .../t12589/example/src/main/scala/example.scala:19:12: not found: type AtomicType
[error]     val a: AtomicType = ???
[error]            ^
[error] three errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 4 s, completed Jun 30, 2022, 9:42:12 AM

som-snytt avatar Jun 30 '22 16:06 som-snytt