|
| 1 | +// '@_private import' is intended to be used to compile "thunks" which contain |
| 2 | +// modified versions of declarations. For this to work properly, name lookup |
| 3 | +// needs to favor declarations in the private import over other imports. This |
| 4 | +// emulates the increased visibility of same-module declarations in the original |
| 5 | +// file, preserving source compatibility as much as possible. |
| 6 | +// |
| 7 | +// In this test: |
| 8 | +// |
| 9 | +// * Module Host contains this file and Host.swift. Note that this file compiles |
| 10 | +// successfully with COMPILING_THUNK undefined. |
| 11 | +// * This file is also built as a thunk with COMPILING_THUNK defined to "edit" |
| 12 | +// it. |
| 13 | +// * We also build three other modules called Most, Ghost, and Toast. Each of |
| 14 | +// these is imported by both Host and the thunk. |
| 15 | +// |
| 16 | +// There are various same-name types scattered throughout these modules. If |
| 17 | +// name lookup selects the right ones for each of the test cases, the compiler |
| 18 | +// will generate the expected diagnostics and the test will pass. If it selects |
| 19 | +// incorrect types, deprecation warnings will be emitted and the test will fail. |
| 20 | + |
| 21 | +// We'll put our modules here. |
| 22 | +// RUN: %empty-directory(%t) |
| 23 | + |
| 24 | +// Build some libraries to work with. |
| 25 | +// RUN: %target-swift-frontend -emit-module -parse-as-library -module-name Most -emit-module-path %t/Most.swiftmodule -primary-file %S/Inputs/private_import/Most.swift |
| 26 | +// RUN: %target-swift-frontend -emit-module -parse-as-library -module-name Ghost -emit-module-path %t/Ghost.swiftmodule -primary-file %S/Inputs/private_import/Ghost.swift |
| 27 | +// RUN: %target-swift-frontend -emit-module -enable-private-imports -parse-as-library -module-name Toast -emit-module-path %t/Toast.swiftmodule -primary-file %S/Inputs/private_import/Toast.swift |
| 28 | + |
| 29 | +// Build the host module that the thunk is pretending to be part of. |
| 30 | +// RUN: %target-swift-frontend -emit-module -module-name Host -I %t -emit-module-path %t/Host.swiftmodule -enable-private-imports %s %S/Inputs/private_import/Host.swift |
| 31 | + |
| 32 | +// Build a thunk for the host module. |
| 33 | +// RUN: %target-typecheck-verify-swift -parse-as-library -I %t -DCOMPILING_THUNK |
| 34 | + |
| 35 | +#if COMPILING_THUNK |
| 36 | +@_private(sourceFile: "private_import.swift") import Host |
| 37 | +#endif |
| 38 | + |
| 39 | +import Most |
| 40 | +import Ghost |
| 41 | + |
| 42 | +@_private(sourceFile: "Toast.swift") import Toast |
| 43 | + |
| 44 | +// |
| 45 | +// Types with varying definitions |
| 46 | +// |
| 47 | + |
| 48 | +struct Hunk {} // Present in both Host and thunk |
| 49 | + |
| 50 | +#if COMPILING_THUNK |
| 51 | + |
| 52 | +struct Thunk {} // Only present in thunk |
| 53 | +struct Bunk {} // Not deprecated in thunk |
| 54 | + |
| 55 | +#else |
| 56 | + |
| 57 | +struct Shrunk {} // Only present in Host |
| 58 | +@available(*, deprecated, message: "got Host version") |
| 59 | +struct Bunk {} // Only deprecated in Host |
| 60 | + |
| 61 | +#endif |
| 62 | + |
| 63 | +// |
| 64 | +// Test cases |
| 65 | +// |
| 66 | + |
| 67 | +#if COMPILING_THUNK |
| 68 | +func thunkCanUseTypesOnlyInThunk(_: Thunk) {} |
| 69 | +#endif |
| 70 | + |
| 71 | +func thunkCanRedeclareTypes(_: Hunk) {} |
| 72 | +func thunkCanUseTypesOnlyInHost(_: Shrunk, _: Post) {} |
| 73 | +func thunkTypesShadowHostTypes(_: Bunk) {} |
| 74 | + |
| 75 | +func hostTypesShadowMostTypes(_: Boast) {} |
| 76 | +// no-error@-1; previously, this would give "'Boast' is ambiguous for type lookup in this context" |
| 77 | + |
| 78 | +#if COMPILING_THUNK |
| 79 | +func ambiguousWithTwoNonPrivateImports(_: Coast) {} |
| 80 | +// expected-error@-1{{'Coast' is ambiguous for type lookup in this context}} |
| 81 | +#endif |
| 82 | + |
| 83 | +// The intended clients of '@_private import' should not actually use it twice |
| 84 | +// in a single file, so this behavior is more accidental than anything. |
| 85 | +func ambiguousWithTwoPrivateImports(_: Roast) {} |
| 86 | +// expected-error@-1{{'Roast' is ambiguous for type lookup in this context}} |
0 commit comments