Skip to content

protect[something] on trait doesn't work #4845

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
scabug opened this issue Jul 27, 2011 · 3 comments
Closed

protect[something] on trait doesn't work #4845

scabug opened this issue Jul 27, 2011 · 3 comments
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Jul 27, 2011

As reported in the Scala-User mailing list (~2011-07-26T13:09), protected[packagename] on trait doesn't work, ie. the trait seems to be public.
I paste here my observations:

  • file T1.scala
    package c.d

protected[d] trait MyTrait
{
def doFoo() { println("Foo") }
}

  • file T2.scala

package a.b

import c.d.MyTrait

class MyClass extends MyTrait
{
def doBar() { doFoo(); println("Bar") }
}

object X
{
def main(args: Array[String])
{
val mc = new MyClass
mc.doBar()
}
}

D:\Temp\Scala

C:\Java\scala-2.8.1.final\bin\scalac.bat T1.scala

D:\Temp\Scala

C:\Java\scala-2.8.1.final\bin\scalac.bat T2.scala
T2.scala:5: error: trait MyTrait cannot be accessed in package c.d
class MyClass extends MyTrait
^
T2.scala:7: error: not found: value doFoo
def doBar() { doFoo(); println("Bar") }
^
two errors found

D:\Temp\Scala

C:\Java\scala-2.9.0.final\bin\scalac.bat T1.scala

D:\Temp\Scala

C:\Java\scala-2.9.0.final\bin\scalac.bat T2.scala

D:\Temp\Scala

C:\Java\scala-2.9.0.final\bin\scala.bat a.b.X
Foo
Bar

I took a code snippet from the online Programming in Scala [1] to activate the protection, ie. to get a compilation error:

package bobsrockets
{
package navigation
{
private[navigation] class Navigator
{
protected[navigation] def useStarChart() {}
class LegOfJourney
{
private[Navigator] val distance = 100
}
private[this] var speed = 200
}
}
package launch
{
import navigation._
object Vehicle
{
private[launch] val guide = new Navigator
def vroom() = guide.useStarChart
}
}
}

D:\Temp\Scala

C:\Java\scala-2.8.1.final\bin\scalac.bat T3.scala
T3.scala:20: error: class Navigator cannot be accessed in package bobsrockets.navigation
private[launch] val guide = new Navigator
^
one error found

D:\Temp\Scala

C:\Java\scala-2.9.0.final\bin\scalac.bat T3.scala
error: scala.MatchError: <unknown tree of class class scala.tools.nsc.typechecker.Infer$Inferencer$AccessError> (of class scala.tools.nsc.typechecker.Infer$Inferencer$AccessError)
at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:754)
at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53)
at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:243)
at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:831)
[...] (long stack trace)

[1] http://www.artima.com/pins1ed/packages-and-imports.html "Public members" (no anchors, alas)

@scabug
Copy link
Author

scabug commented Jul 27, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4845?orig=1
Reporter: Philippe Lhoste (philho)
Affected Versions: 2.9.0

@som-snytt
Copy link

This is enforced under separate compilation.

t4845.scala


package p.q {
  protected[p] trait T {
    def t = 42
  }
}
t4845b.scala

package v.w {
  class C extends p.q.T {
    def c = 27
  }

  object Test extends App {
    println {
      new C().t
    }
  }
}

yields indeed

t4845b.scala:3: error: trait T in package q cannot be accessed as a member of package p.q from package w in package v
 Access to protected trait T not permitted because
 enclosing package w in package v is not a subclass of
 package q in package p where target is defined
  class C extends p.q.T {
                      ^

or

3 |  class C extends p.q.T {
  |                  ^^^^^
  |          trait T cannot be accessed as a member of p.q.type from class C.
  |            protected[p] trait T can only be accessed from package p.

I did not test whether dotty actually handles access differently.

@scala scala deleted a comment from scabug May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants