diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2013-10-23 14:18:45 -0700 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2013-10-23 14:18:45 -0700 |
commit | 7d150ce6a56e49222d542f3cf1968a7c70c8668a (patch) | |
tree | 4b79a9d2ca136585fe2d56a08c7c8b6ac50f7283 /test/files | |
parent | 951422163ab8fc47caa60acb66b670a371b5489e (diff) | |
parent | b39a509213f7b43185cca3e633c9211abdc0f126 (diff) | |
download | scala-7d150ce6a56e49222d542f3cf1968a7c70c8668a.tar.gz scala-7d150ce6a56e49222d542f3cf1968a7c70c8668a.tar.bz2 scala-7d150ce6a56e49222d542f3cf1968a7c70c8668a.zip |
Merge pull request #3026 from retronym/ticket/3871
Tests for protected access
Diffstat (limited to 'test/files')
-rw-r--r-- | test/files/neg/t3871.check | 7 | ||||
-rw-r--r-- | test/files/neg/t3871.scala | 11 | ||||
-rw-r--r-- | test/files/neg/t3871b.check | 97 | ||||
-rw-r--r-- | test/files/neg/t3871b.scala | 127 |
4 files changed, 242 insertions, 0 deletions
diff --git a/test/files/neg/t3871.check b/test/files/neg/t3871.check new file mode 100644 index 0000000000..b920357ee6 --- /dev/null +++ b/test/files/neg/t3871.check @@ -0,0 +1,7 @@ +t3871.scala:4: error: variable foo in class Sub2 cannot be accessed in Sub2 + Access to protected method foo not permitted because + enclosing class Base is not a subclass of + class Sub2 where target is defined + s.foo = true + ^ +one error found diff --git a/test/files/neg/t3871.scala b/test/files/neg/t3871.scala new file mode 100644 index 0000000000..fc459867df --- /dev/null +++ b/test/files/neg/t3871.scala @@ -0,0 +1,11 @@ +class Base { + def mkNew() = { + val s = new Sub2 + s.foo = true + s + } +} + +class Sub2 extends Base { + protected var foo = false +} diff --git a/test/files/neg/t3871b.check b/test/files/neg/t3871b.check new file mode 100644 index 0000000000..6ab5ddfaf1 --- /dev/null +++ b/test/files/neg/t3871b.check @@ -0,0 +1,97 @@ +t3871b.scala:61: error: not found: value protOT + protOT // not allowed + ^ +t3871b.scala:77: error: method prot in class A cannot be accessed in E.this.A + Access to protected method prot not permitted because + prefix type E.this.A does not conform to + class B in class E where the access take place + a.prot // not allowed, prefix type `A` does not conform to `B` + ^ +t3871b.scala:79: error: value protT is not a member of E.this.B + b.protT // not allowed + ^ +t3871b.scala:80: error: value protT is not a member of E.this.C + c.protT // not allowed + ^ +t3871b.scala:81: error: value protT is not a member of E.this.A + a.protT // not allowed + ^ +t3871b.scala:91: error: method prot in class A cannot be accessed in E.this.A + Access to protected method prot not permitted because + prefix type E.this.A does not conform to + object B in class E where the access take place + a.prot // not allowed + ^ +t3871b.scala:93: error: value protT is not a member of E.this.B + b.protT // not allowed + ^ +t3871b.scala:94: error: value protT is not a member of E.this.C + c.protT // not allowed + ^ +t3871b.scala:95: error: value protT is not a member of E.this.A + a.protT // not allowed + ^ +t3871b.scala:102: error: method prot in class A cannot be accessed in E.this.B + Access to protected method prot not permitted because + enclosing class Z in class E is not a subclass of + class A in class E where target is defined + b.prot // not allowed + ^ +t3871b.scala:103: error: method prot in class A cannot be accessed in E.this.C + Access to protected method prot not permitted because + enclosing class Z in class E is not a subclass of + class A in class E where target is defined + c.prot // not allowed + ^ +t3871b.scala:104: error: method prot in class A cannot be accessed in E.this.A + Access to protected method prot not permitted because + enclosing class Z in class E is not a subclass of + class A in class E where target is defined + a.prot // not allowed + ^ +t3871b.scala:109: error: value protT is not a member of E.this.B + b.protT // not allowed + ^ +t3871b.scala:110: error: value protT is not a member of E.this.C + c.protT // not allowed + ^ +t3871b.scala:111: error: value protT is not a member of E.this.A + a.protT // not allowed + ^ +t3871b.scala:120: error: method prot in class A cannot be accessed in Other.this.e.B + Access to protected method prot not permitted because + enclosing class Other is not a subclass of + class A in class E where target is defined + b.prot // not allowed + ^ +t3871b.scala:121: error: method prot in class A cannot be accessed in Other.this.e.C + Access to protected method prot not permitted because + enclosing class Other is not a subclass of + class A in class E where target is defined + c.prot // not allowed + ^ +t3871b.scala:122: error: method prot in class A cannot be accessed in Other.this.e.A + Access to protected method prot not permitted because + enclosing class Other is not a subclass of + class A in class E where target is defined + a.prot // not allowed + ^ +t3871b.scala:123: error: method protE in class A cannot be accessed in Other.this.e.B + Access to protected method protE not permitted because + enclosing class Other is not a subclass of + class A in class E where target is defined + b.protE // not allowed + ^ +t3871b.scala:124: error: method protE in class A cannot be accessed in Other.this.e.A + Access to protected method protE not permitted because + enclosing class Other is not a subclass of + class A in class E where target is defined + a.protE // not allowed + ^ +t3871b.scala:125: error: method protE in class A cannot be accessed in Other.this.e.C + Access to protected method protE not permitted because + enclosing class Other is not a subclass of + class A in class E where target is defined + c.protE // not allowed + ^ +21 errors found diff --git a/test/files/neg/t3871b.scala b/test/files/neg/t3871b.scala new file mode 100644 index 0000000000..b490b7789a --- /dev/null +++ b/test/files/neg/t3871b.scala @@ -0,0 +1,127 @@ +/** + +The protected modifier applies to class member definitions. Protected members of a class can be accessed from within + + 0a. the companion module of any of those classes + +A protected identifier x may be used as a member name in a selection r.x only +if one of the following applies: + 1a. The access is within the template defining the member, or, + if a qualification C is given, + 1b. inside the package C, or + 1c. the class C , or its companion module, or + 2. r is one of the reserved words this and super, or + 3. r’s type conforms to a type-instance of the class which contains the access. + + 4. A different form of qualification is protected[this]. A member M marked with this + modifier is called object-protected; it can be accessed only from within the object + in which it is defined. That is, a selection p.M is only legal if the prefix is this + or O.this, for some class O enclosing the reference. In addition, the restrictions + for unqualified protected apply. +*/ + +object E { + val e = new E + import e._ + def n(a: A, b: B, c: C) = { + b.protE // 1c + c.protE // 1c + a.protE // 1c + A.protOE // 1c + } +} + +class E { + object A { + protected def protO = 2 + protected[E] def protOE = 3 + protected[this] def protOT = 3 + } + class A { + protected def prot = 2 + protected[E] def protE = 3 + protected[this] def protT = 4 + + // 1a + prot; protE; protT + def foo = {prot; protE; protT} + new { prot; protE } + def this(a: Any) = {this(); prot; protE; protT} + object B extends A { + A.this.prot + A.this.protE + A.this.protT + } + + import A._ + // 0a + protO + // 3 + protOE + protOT // not allowed + } + + class B extends A { + // 1b + this.prot; this.protE; + super.prot; super.protE; + + // 4 + this.protT + // 4 !!! "or the super keyword" + super.protT + + def n(a: A, b: B, c: C) = { + b.prot // 3 + c.prot // 3 + a.prot // not allowed, prefix type `A` does not conform to `B` + + b.protT // not allowed + c.protT // not allowed + a.protT // not allowed + } + } + object B { + def n(a: A, b: B, c: C) = { + b.prot // 3 !!! + c.prot // 3 !!! + // Wording of 3 seems insufficient, missing: + // "... (if the access is from a class), or + // the type instance of companion class (if the access is from a module)" + a.prot // not allowed + + b.protT // not allowed + c.protT // not allowed + a.protT // not allowed + } + } + class C extends B + + class Z { + def n(a: A, b: B, c: C) = { + b.prot // not allowed + c.prot // not allowed + a.prot // not allowed + b.protE // 2 + a.protE // 2 + c.protE // 2 + + b.protT // not allowed + c.protT // not allowed + a.protT // not allowed + } + } +} + +class Other { + val e = new E + import e._ + def n(a: A, b: B, c: C) = { + b.prot // not allowed + c.prot // not allowed + a.prot // not allowed + b.protE // not allowed + a.protE // not allowed + c.protE // not allowed + } +} |