Skip to content

Commit ae76f51

Browse files
authored
Merge pull request swiftlang#67746 from xymus/spi-private-imports
[Sema] @_private imports brings is all SPI of the imported module
2 parents 7e2cdf8 + 69493d1 commit ae76f51

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

lib/AST/Module.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,6 +3419,9 @@ bool SourceFile::isImportedAsSPI(const ValueDecl *targetDecl) const {
34193419
if (shouldImplicitImportAsSPI(targetDecl->getSPIGroups()))
34203420
return true;
34213421

3422+
if (hasTestableOrPrivateImport(AccessLevel::Public, targetDecl, PrivateOnly))
3423+
return true;
3424+
34223425
lookupImportedSPIGroups(targetModule, importedSPIGroups);
34233426
if (importedSPIGroups.empty())
34243427
return false;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/// An `@_private` import opens access to all SPIs of the imported module.
2+
/// Exports of SPI in API are still reported.
3+
4+
// RUN: %empty-directory(%t)
5+
// RUN: split-file %s %t
6+
7+
/// Build the library.
8+
// RUN: %target-swift-frontend -emit-module %t/Lib_FileA.swift %t/Lib_FileB.swift \
9+
// RUN: -module-name Lib -emit-module-path %t/Lib.swiftmodule \
10+
// RUN: -enable-library-evolution -swift-version 5 \
11+
// RUN: -enable-private-imports
12+
13+
/// Typecheck a @_private client.
14+
// RUN: %target-swift-frontend -typecheck -verify -I %t %t/PrivateClient.swift
15+
16+
/// Typecheck a regular client building against the same Lib with private imports enabled.
17+
// RUN: %target-swift-frontend -typecheck -verify -I %t %t/RegularClient.swift
18+
19+
//--- Lib_FileA.swift
20+
@_spi(S) public func spiFuncA() {}
21+
@_spi(S) public struct SPITypeA {}
22+
23+
//--- Lib_FileB.swift
24+
@_spi(S) public func spiFuncB() {}
25+
@_spi(S) public struct SPITypeB {}
26+
27+
//--- PrivateClient.swift
28+
@_private(sourceFile: "Lib_FileA.swift") import Lib
29+
30+
func useOnly(a: SPITypeA, b: SPITypeB) {
31+
spiFuncA()
32+
spiFuncB()
33+
}
34+
35+
public func export(a: SPITypeA, b: SPITypeB) { // expected-error {{cannot use struct 'SPITypeA' here; it is an SPI imported from 'Lib'}}
36+
// expected-error @-1 {{cannot use struct 'SPITypeB' here; it is an SPI imported from 'Lib'}}
37+
spiFuncA()
38+
spiFuncB()
39+
}
40+
41+
@inlinable
42+
public func inlinableExport(a: SPITypeA, b: SPITypeB) { // expected-error {{struct 'SPITypeA' cannot be used in an '@inlinable' function because it is an SPI imported from 'Lib'}}
43+
// expected-error @-1 {{struct 'SPITypeB' cannot be used in an '@inlinable' function because it is an SPI imported from 'Lib'}}
44+
spiFuncA() // expected-error {{global function 'spiFuncA()' cannot be used in an '@inlinable' function because it is an SPI imported from 'Lib'}}
45+
spiFuncB() // expected-error {{global function 'spiFuncB()' cannot be used in an '@inlinable' function because it is an SPI imported from 'Lib'}}
46+
}
47+
48+
//--- RegularClient.swift
49+
import Lib
50+
51+
func useOnly(a: SPITypeA, b: SPITypeB) { // expected-error {{cannot find type 'SPITypeA' in scope}}
52+
// expected-error @-1 {{cannot find type 'SPITypeB' in scope}}
53+
spiFuncA() // expected-error {{cannot find 'spiFuncA' in scope}}
54+
spiFuncB() // expected-error {{cannot find 'spiFuncB' in scope}}
55+
}
56+
57+
public func export(a: SPITypeA, b: SPITypeB) { // expected-error {{cannot find type 'SPITypeA' in scope}}
58+
// expected-error @-1 {{cannot find type 'SPITypeB' in scope}}
59+
spiFuncA() // expected-error {{cannot find 'spiFuncA' in scope}}
60+
spiFuncB() // expected-error {{cannot find 'spiFuncB' in scope}}
61+
}
62+
63+
@inlinable
64+
public func inlinableExport(a: SPITypeA, b: SPITypeB) { // expected-error {{cannot find type 'SPITypeA' in scope}}
65+
// expected-error @-1 {{cannot find type 'SPITypeB' in scope}}
66+
spiFuncA() // expected-error {{cannot find 'spiFuncA' in scope}}
67+
spiFuncB() // expected-error {{cannot find 'spiFuncB' in scope}}
68+
}

0 commit comments

Comments
 (0)