Scala Programming Language
  1. Scala Programming Language
  2. SI-6601

Scalac ignores private constructor for value class from classpath

    Details

      Description

      1. Create PrivateConstructor.scala:

      class PrivateConstructor private(val s: String) extends AnyVal
      

      2. Run scalac:

      scalac PrivateConstructor.scala
      

      3. Create AccessPrivateConstructor.scala:

      class AccessPrivateConstructor {
        new PrivateConstructor("") // Scalac should forbid accessing to the private constructor!
      }
      

      4. Run scalac:

      scalac AccessPrivateConstructor.scala
      

      I expect scalac to print some compilation error, but scalac does not print any thing. Scalac just ignores the private modifier for constructor when parsing PrivateConstructor.class.

        Activity

        Hide
        Jason Zaugg added a comment -

        Seems to be leftover from https://github.com/scala/scala/commit/437c6261

        extmethods sets notPRIVATE on the primary constructor, but it runs before pickler.

        Show
        Jason Zaugg added a comment - Seems to be leftover from https://github.com/scala/scala/commit/437c6261 extmethods sets notPRIVATE on the primary constructor, but it runs before pickler .
        Show
        Jason Zaugg added a comment - https://github.com/scala/scala/pull/1914
        Hide
        Jason Zaugg added a comment -

        Reopening: the remedy was far worse than the disease:

          ~/code/scala cat sandbox/test.scala 
        class V private (val a: Any) extends AnyVal
          ~/code/scala RUNNER=scalac scala-hash b07228aebe sandbox/test.scala 
        [info] b07228aebe => /Users/jason/usr/scala-v2.10.0-256-gb07228a
          ~/code/scala scala-hash b07228aebe 
        [info] b07228aebe => /Users/jason/usr/scala-v2.10.0-256-gb07228a
        Welcome to Scala version 2.10.1-20130116-230935-b07228aebe (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_27).
        Type in expressions to have them evaluated.
        Type :help for more information.
        
        scala> def foo(v: V) = v.a == v.a
        exception when typing v.a().==(v.a())/class scala.reflect.internal.Trees$Apply
        constructor V in class V cannot be accessed in object $iw in file <console>
        scala.reflect.internal.Types$TypeError: constructor V in class V cannot be accessed in object $iw
        
        Show
        Jason Zaugg added a comment - Reopening: the remedy was far worse than the disease: ~/code/scala cat sandbox/test.scala class V private (val a: Any) extends AnyVal ~/code/scala RUNNER=scalac scala-hash b07228aebe sandbox/test.scala [info] b07228aebe => /Users/jason/usr/scala-v2.10.0-256-gb07228a ~/code/scala scala-hash b07228aebe [info] b07228aebe => /Users/jason/usr/scala-v2.10.0-256-gb07228a Welcome to Scala version 2.10.1-20130116-230935-b07228aebe (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_27). Type in expressions to have them evaluated. Type :help for more information. scala> def foo(v: V) = v.a == v.a exception when typing v.a().==(v.a())/class scala.reflect.internal.Trees$Apply constructor V in class V cannot be accessed in object $iw in file <console> scala.reflect.internal.Types$TypeError: constructor V in class V cannot be accessed in object $iw
        Show
        Jason Zaugg added a comment - https://github.com/scala/scala/pull/2236
        Hide
        Jason Zaugg added a comment -

        We can't fix this in the 2.10.x series as we need an incompatible change of the pickled flags.

        I've submitted a PR to do that for 2.11.0-M3:

        https://github.com/scala/scala/pull/2236

        Show
        Jason Zaugg added a comment - We can't fix this in the 2.10.x series as we need an incompatible change of the pickled flags. I've submitted a PR to do that for 2.11.0-M3: https://github.com/scala/scala/pull/2236

          People

          • Assignee:
            Jason Zaugg
            Reporter:
            杨博
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development