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

Private member of jointly compiled java class is importable. #3160

Closed
scabug opened this issue Mar 9, 2010 · 9 comments
Closed

Private member of jointly compiled java class is importable. #3160

scabug opened this issue Mar 9, 2010 · 9 comments
Milestone

Comments

@scabug
Copy link

scabug commented Mar 9, 2010

A private member of a Java class, whose source is jointly compiled by scalac, is considered importable. If the java file is precompiled with javac and included on the classpath of scalac, it is no longer importable.

(I raised this on scala-user, but didn't get a reply.)

E:\code\scratch\access>cat HasPrivateFoo.java
class HasPrivateFoo {
   private Object foo = null;
}
E:\code\scratch\access>cat Import.scala
object Import {
   def test(foo: Any, h: HasPrivateFoo) {
      import h._
      foo
   }
}

E:\code\scratch\access>scalac -version
Scala compiler version 2.8.0.Beta1-prerelease -- Copyright 2002-2010, LAMP/EPFL

E:\code\scratch\access>javac HasPrivateFoo.java

E:\code\scratch\access>scalac -classpath . Import.scala

E:\code\scratch\access>scalac Import.scala HasPrivateFoo.java
Import.scala:4: error: reference to foo is ambiguous;
it is both defined in method test and imported subsequently by
import h._
      foo
      ^
one error found

E:\code\scratch\access>scalac -version
Scala compiler version 2.8.0.Beta1-prerelease -- Copyright 2002-2010, LAMP/EPFL

E:\code\scratch\access>rm *class

E:\code\scratch\access>E:\tools\scala-2.8.0.r21113-b20100309020141\bin\scalac Import.scala HasPrivateFoo.java
Import.scala:4: error: reference to foo is ambiguous;
it is both defined in method test and imported subsequently by
import h._
      foo
      ^
one error found
@scabug
Copy link
Author

scabug commented Mar 9, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3160?orig=1
Reporter: @retronym

@scabug
Copy link
Author

scabug commented Mar 9, 2010

@axel22 said:
Is it ok to assign this to you, Miles? The fix is not very urgent.

Thanks

@scabug
Copy link
Author

scabug commented Mar 9, 2010

@milessabin said:
Sure ... I'll see what I can do.

@scabug
Copy link
Author

scabug commented Apr 13, 2010

@paulp said:
See also #2133 (and #3285 which I just closed as duplicate of that.)

@scabug
Copy link
Author

scabug commented Jan 12, 2011

@paulp said:
Based on the resolution of #2133, this isn't even a bug unless there will
be different rules for java. Martin said:

Stephane's original example is not covered by the spec, which says that
all importable members are counted. A member is importable as long as
it is not private[this]. Why this wording? Because an inner class might
validly import the private members of its companion object.  

That would be a distressing outcome, and not just for this. It seems like we shouldn't have to pollute the namespace of everyone who imports a package with a bunch of inaccessible symbols, just to keep the door open for some hypothetical inner classes.

Also, see previous ticket of mine #3836, "ambiguous references which aren't." But it's worse than I had realized at the time. You can't even use aliases in your own package, private to your own package, without ruining the package.

package object foo {
  private[foo] type InputStream = java.io.InputStream
  // oddly there is no conflict if there's no qualifier,
  // though the quote from martin would imply otherwise.
  // private type InputStream = java.io.InputStream
}

object bar {
  import foo._
  import java.io._

  var x: InputStream = null
}

// 9: error: reference to InputStream is ambiguous;
// it is imported twice in the same scope by
// import java.io._
// and import foo._
//   var x: InputStream = null
//          ^
// one error found

I think this is terribly unfortunate. And should be treated as an
encapsulation failure, not a mere inconvenience (though it is quite
inconvenient.)

In the wontfix for #3836 martin says he doesn't want name lookup to start intruding on symbol resolution and typing. That's fine. I see this as like implicit resolution or type inference, where there is often some ambiguity which would be resolved if the process continued. An implicit is ruled out because the type parameters don't match its bounds, etc.

But the biggest thing is that private names which affect an unrelated class's namespace cannot really be called private.

@scabug
Copy link
Author

scabug commented Mar 12, 2012

@Blaisorblade said (edited on Mar 12, 2012 12:51:58 AM UTC):
Paul, before reading your comment, I added a comment to #2133, where I also complained (for the same reasons you give above about failure of encapsulation), and reopened that bug:
https://issues.scala-lang.org/browse/SI-2133?focusedCommentId=56541#comment-56541
The reasons you give above seem more valid, so maybe you should close the bug again. Sorry for the noise!

@som-snytt
Copy link

scala/scala#1521

@SethTisue
Copy link
Member

@som-snytt should this ticket be closed?

@som-snytt
Copy link

som-snytt commented Feb 17, 2018

scala/scala#5970

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

No branches or pull requests

4 participants