Skip to content

Commit 021dc8e

Browse files
committed
[Sema] Error on implementation-only imported types in SPI with @_spiOnly
This used to be just a warning because it happened to work when clients used only swiftmodules. Some projects have been using it as an alternative to @_spiOnly imports. Now that we have an official feature we can complete the diagnositc and error on implementation-only imports types used in SPI.
1 parent 6d396b4 commit 021dc8e

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lib/Sema/TypeCheckAccess.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,9 @@ swift::getDisallowedOriginKind(const Decl *decl,
15481548
if (howImported != RestrictedImportKind::None) {
15491549
// Temporarily downgrade implementation-only exportability in SPI to
15501550
// a warning.
1551-
if (where.isSPI())
1551+
if (where.isSPI() &&
1552+
where.getFragileFunctionKind().kind == FragileFunctionKind::None &&
1553+
!SF->getASTContext().LangOpts.EnableSPIOnlyImports)
15521554
downgradeToWarning = DowngradeToWarning::Yes;
15531555

15541556
if (where.isSPI() && howImported == RestrictedImportKind::SPIOnly)

test/SPI/report-ioi-in-spi.swift

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -emit-module %t/Lib.swift -I %t \
5+
// RUN: -module-name Lib -emit-module-path %t/Lib.swiftmodule \
6+
// RUN: -swift-version 5
7+
8+
/// Use of IOI types in SPI signatures is an error with -experimental-spi-only-imports
9+
// RUN: %target-swift-frontend -emit-module %t/ClientSPIOnlyMode.swift -I %t \
10+
// RUN: -swift-version 5 -verify \
11+
// RUN: -experimental-spi-only-imports
12+
13+
/// Use of IOI types in SPI signatures is a warning without -experimental-spi-only-imports
14+
// RUN: %target-swift-frontend -emit-module %t/ClientDefaultMode.swift -I %t \
15+
// RUN: -swift-version 5 -verify
16+
17+
/// This is a warning in swiftinterfaces
18+
// R UN: %target-swift-typecheck-module-from-interface(%t/Client.private.swiftinterface) \
19+
// R UN: -I %t -module-name Client
20+
21+
//--- Lib.swift
22+
23+
public struct IOIStruct {
24+
public init() {}
25+
}
26+
27+
//--- ClientSPIOnlyMode.swift
28+
29+
@_implementationOnly import Lib
30+
31+
@_spi(X) public func spiClient(s: IOIStruct) -> IOIStruct { // expected-error 2 {{cannot use struct 'IOIStruct' here; 'Lib' has been imported as implementation-only}}
32+
return IOIStruct()
33+
}
34+
35+
@_spi(X) @inlinable public func inlinableClient(s: IOIStruct) -> IOIStruct { // expected-error 2 {{struct 'IOIStruct' cannot be used in an '@inlinable' function because 'Lib' was imported implementation-only}}
36+
return IOIStruct() // expected-error {{struct 'IOIStruct' cannot be used in an '@inlinable' function because 'Lib' was imported implementation-only}}
37+
// expected-error @-1 {{initializer 'init()' cannot be used in an '@inlinable' function because 'Lib' was imported implementation-only}}
38+
}
39+
40+
//--- ClientDefaultMode.swift
41+
42+
@_implementationOnly import Lib
43+
44+
@_spi(X) public func spiClient(s: IOIStruct) -> IOIStruct { // expected-warning 2 {{cannot use struct 'IOIStruct' here; 'Lib' has been imported as implementation-only}}
45+
return IOIStruct()
46+
}
47+
48+
@_spi(X) @inlinable public func inlinableClient(s: IOIStruct) -> IOIStruct { // expected-error 2 {{struct 'IOIStruct' cannot be used in an '@inlinable' function because 'Lib' was imported implementation-only}}
49+
return IOIStruct() // expected-error {{struct 'IOIStruct' cannot be used in an '@inlinable' function because 'Lib' was imported implementation-only}}
50+
// expected-error @-1 {{initializer 'init()' cannot be used in an '@inlinable' function because 'Lib' was imported implementation-only}}
51+
}
52+
53+
//--- Client.private.swiftinterface
54+
55+
// swift-interface-format-version: 1.0
56+
// swift-compiler-version: Swift version 5.8-dev effective-4.1.50
57+
// swift-module-flags: -swift-version 4 -module-name Client
58+
@_implementationOnly import Lib
59+
60+
@_spi(X) public func spiClient() -> IOIStruct { fatalError() }

0 commit comments

Comments
 (0)