Files
swift-mirror/test/Interop/Cxx/class/access/non-public-inheritance-module-interface.swift
John Hui 66c2e2c52b [cxx-interop] Import non-public inherited members (#79348)
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
2025-02-25 01:03:16 -08:00

120 lines
4.9 KiB
Swift

// This test checks that the module interface correctly reflects the access
// levels of imported inherited members (especially non-public ones).
//
// For private base members and other base members made inaccessible due to
// private inheritance, this test does not check against a specific message for
// the @available(*, unavailable) attributes (to ensure this test is robust
// against message wording changes), but it does require the message to mention
// something about "private".
//
// RUN: %target-swift-ide-test -print-module -module-to-print=NonPublicInheritance -print-access -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
// CHECK: public struct Base {
// CHECK-NEXT: public init()
// CHECK-NEXT: public func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct PublBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: public func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct ProtBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct PrivBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct PublPublBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: public func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct ProtPublBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct PrivPublBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct PublProtBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct ProtProtBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct PrivProtBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct PublPrivBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct ProtPrivBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: public struct PrivPrivBase {
// CHECK-NEXT: public init()
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func publ() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func prot() -> Int32
// CHECK-NEXT: @available(*, unavailable, message: "{{.*}}private{{.*}}")
// CHECK-NEXT: private func priv() -> Int32
// CHECK-NEXT: }