-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #4496: call ensureAccessible to bypass class-level accessibility #4529
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import scala.reflect.Selectable.reflectiveSelectable | ||
|
||
trait Foo1 { val a: Int } | ||
trait Foo2 { def a: Int } | ||
trait Foo3 { var a: Int } | ||
|
||
object TestStructuralVar { | ||
type T0 = {var a: Int} // error | ||
object TestStructuralVar { | ||
type T = {val a: Int; def a_=(x: Int): Unit} | ||
def upcast1(v: Foo1): T = v // error | ||
def upcast2(v: Foo2): T = v // error | ||
def upcast3(v: Foo3): T = v | ||
def verify(v: T) = () | ||
def test(): Unit = { | ||
verify(upcast1(new Foo1 { val a = 10 })) | ||
verify(upcast2(new Foo2 { val a = 10 })) | ||
verify(upcast3(new Foo3 { var a = 10 })) | ||
} | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
TestStructuralVar.test() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import scala.reflect.Selectable.reflectiveSelectable | ||
|
||
trait Foo1 { val a: Int } | ||
trait Foo2 { def a: Int } | ||
trait Foo3 { var a: Int } | ||
|
||
object Test { | ||
private class FooBar1 extends Foo1 { val a: Int = 10 } | ||
private class FooBar2 extends Foo2 { def a: Int = 10 } | ||
private class FooBar3 extends Foo3 { var a: Int = 10 } | ||
|
||
private class Bar1 { val a: Int = 10 } | ||
private class Bar2 { def a: Int = 10 } | ||
private class Bar3 { var a: Int = 10 } | ||
|
||
object TestStructuralVal { | ||
// This test is also an (abstracted) motivating example. | ||
|
||
// Consider one module upcasting all these instances to T. These casts are clearly well-typed. | ||
type T = {val a: Int} | ||
def upcast1(v: Foo1): T = v | ||
def upcast2(v: Foo2): T = v | ||
def upcast3(v: Foo3): T = v | ||
|
||
// These accesses are also clearly well-typed | ||
def consume(v: T) = v.a | ||
inline def consumeInl(v: T) = v.a | ||
def verify(v: T) = { | ||
assert(consume(v) == 10) | ||
assert(consumeInl(v) == 10) | ||
assert(v.a == 10) | ||
} | ||
|
||
def test(): Unit = { | ||
// These calls are also clearly well-typed, hence can't be rejected. | ||
verify(upcast1(new Foo1 { val a = 10 })) | ||
verify(upcast2(new Foo2 { val a = 10 })) | ||
verify(upcast3(new Foo3 { var a = 10 })) | ||
// Ditto, so we must override access control to the class. | ||
verify(upcast1(new FooBar1)) | ||
verify(upcast2(new FooBar2)) | ||
verify(upcast3(new FooBar3)) | ||
|
||
// Other testcases | ||
verify(new {val a = 10} : T) | ||
verify(new {var a = 10} : T) | ||
verify(new {def a = 10} : T) | ||
|
||
verify(new Bar1 : T) | ||
verify(new Bar2 : T) | ||
verify(new Bar3 : T) | ||
} | ||
} | ||
|
||
object TestStructuralDef { | ||
type T = {def a: Int} | ||
def upcast1(v: Foo1): T = v | ||
def upcast2(v: Foo2): T = v | ||
def upcast3(v: Foo3): T = v | ||
def consume(v: T) = v.a | ||
inline def consumeInl(v: T) = v.a | ||
def verify(v: T) = { | ||
assert(consume(v) == 10) | ||
assert(consumeInl(v) == 10) | ||
assert(v.a == 10) | ||
} | ||
|
||
def test(): Unit = { | ||
verify(upcast1(new Foo1 { val a = 10 })) | ||
verify(upcast2(new Foo2 { val a = 10 })) | ||
verify(upcast3(new Foo3 { var a = 10 })) | ||
|
||
verify(upcast1(new FooBar1)) | ||
verify(upcast2(new FooBar2)) | ||
verify(upcast3(new FooBar3)) | ||
|
||
verify(new {val a = 10} : T) | ||
verify(new {var a = 10} : T) | ||
verify(new {def a = 10} : T) | ||
|
||
verify(new Bar1 : T) | ||
verify(new Bar2 : T) | ||
verify(new Bar3 : T) | ||
} | ||
} | ||
|
||
object TestStructuralVar { | ||
type T = {val a: Int; def a_=(x: Int): Unit} | ||
def upcast3(v: Foo3): T = v | ||
def consume(v: T) = v.a | ||
inline def consumeInl(v: T) = v.a | ||
def verify(v: T) = { | ||
assert(consume(v) == 10) | ||
assert(consumeInl(v) == 10) | ||
assert(v.a == 10) | ||
// Pending, per https://github.com/lampepfl/dotty/issues/4528. | ||
// v.a = 11 | ||
// assert(consume(v) == 11) | ||
// assert(consumeInl(v) == 11) | ||
// assert(v.a == 11) | ||
} | ||
|
||
def test(): Unit = { | ||
verify(upcast3(new Foo3 { var a = 10 })) | ||
verify(upcast3(new FooBar3)) | ||
verify(new {var a = 10} : T) | ||
verify(new Bar3 : T) | ||
} | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
TestStructuralVal.test() | ||
TestStructuralDef.test() | ||
TestStructuralVar.test() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated, but maybe this should be allowed and given the same semantics as having a getter/setter ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thoughts, maybe that's not worth it, since then it needs to be meaningful for all possible interpretations of structural types, nevermind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...but maybe the error message could suggest replacing the var by a val and a setter.