mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This patch is follow-up work from #78942 and imports non-public members, which were previously not being imported. Those members can be accessed in a Swift file blessed by the SWIFT_PRIVATE_FILEID annotation. As a consequence of this patch, we are also now importing inherited members that are inaccessible from the derived classes, because they were declared private, or because they were inherited via nested private inheritance. We import them anyway but mark them unavailable, for better diagnostics and to (somewhat) simplify the import logic for inheritance. Because non-public base class members are now imported too, this patch inflames an existing issue where a 'using' declaration on an inherited member with a synthesized name (e.g., operators) produces duplicate members, leading to miscompilation (resulting in a runtime crash). This was not previously noticed because a 'using' declaration on a public inherited member is not usually necessary, but is a common way to expose otherwise non-public members. This patch puts in a workaround to prevent this from affecting the behavior of MSVC's std::optional implementation, which uses this pattern of 'using' a private inherited member. That will be fixed in a follow-up patch. Follow-up work is also needed to correctly diagnose ambiguous overloads in cases of multiple inheritance, and to account for virtual inheritance. rdar://137764620
55 lines
1.6 KiB
Swift
55 lines
1.6 KiB
Swift
// Testing a C++ type whose public members expose private members.
|
|
//
|
|
// The typechecker test covers most of the interesting cases, which all happen
|
|
// during semantic checking and shouldn't really affect execution. This
|
|
// executable test only exists to make sure we don't hit any unexpected
|
|
// assertions about access level invariants during latter stages of compilation,
|
|
// and does not exhaustively test runtime behavior (which is not very interesting).
|
|
|
|
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -cxx-interoperability-mode=default)
|
|
//
|
|
// REQUIRES: executable_test
|
|
|
|
import StdlibUnittest
|
|
import AccessInversion
|
|
|
|
var AccessInversionTestSuite = TestSuite("AccessInversion")
|
|
|
|
AccessInversionTestSuite.test("usePrivateAlias") {
|
|
let a: Leaky.AliasToPrivateAlias = true
|
|
expectEqual(a, true)
|
|
}
|
|
|
|
AccessInversionTestSuite.test("usePrivateRec") {
|
|
let p = Leaky.staticReturningPrivateRec()
|
|
// p.privateRecMethod()
|
|
var r = Leaky.RecWithPrivateRec()
|
|
r.mem = p
|
|
}
|
|
|
|
AccessInversionTestSuite.test("usePrivateEnum") {
|
|
let e = Leaky.AliasToPrivateEnum(rawValue: 0)!
|
|
expectEqual(e.rawValue, 0)
|
|
}
|
|
|
|
AccessInversionTestSuite.test("usePrivateEnumClass") {
|
|
let e = Leaky.AliasToPrivateEnumClass(rawValue: 0)!
|
|
|
|
switch e {
|
|
default:
|
|
// There is not much to test since we can't access private enums' variants
|
|
expectEqual(e.rawValue, 0)
|
|
}
|
|
}
|
|
|
|
AccessInversionTestSuite.test("usePrivateDefaultArgs") {
|
|
let leaky = Leaky()
|
|
leaky.defaultArgOfPrivateRec()
|
|
leaky.defaultArgOfPrivateEnum()
|
|
leaky.defaultArgOfPrivateEnumClass()
|
|
leaky.defaultArgOfPrivateConst()
|
|
leaky.defaultArgOfPrivateRecConst()
|
|
}
|
|
|
|
runAllTests()
|