Skip to content

Commit 0a945fa

Browse files
committed
[Sema] Fix leak of implementation-only imported types in SPI signatures
rdar://63193289
1 parent dd5a350 commit 0a945fa

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

lib/Sema/TypeCheckAccess.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,11 +1483,14 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
14831483
}
14841484
};
14851485

1486+
// Diagnose public APIs exposing types that are either imported as
1487+
// implementation-only or declared as SPI.
14861488
class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
14871489
class Diagnoser;
14881490

14891491
void checkTypeImpl(
14901492
Type type, const TypeRepr *typeRepr, const SourceFile &SF,
1493+
const Decl *context,
14911494
const Diagnoser &diagnoser) {
14921495
// Don't bother checking errors.
14931496
if (type && type->hasError())
@@ -1500,14 +1503,15 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
15001503
if (typeRepr) {
15011504
const_cast<TypeRepr *>(typeRepr)->walk(TypeReprIdentFinder(
15021505
[&](const ComponentIdentTypeRepr *component) {
1503-
ModuleDecl *M = component->getBoundDecl()->getModuleContext();
1504-
if (!SF.isImportedImplementationOnly(M) &&
1505-
!SF.isImportedAsSPI(component->getBoundDecl()))
1506-
return true;
1507-
1508-
diagnoser.diagnoseType(component->getBoundDecl(), component,
1509-
SF.isImportedImplementationOnly(M));
1510-
foundAnyIssues = true;
1506+
TypeDecl *typeDecl = component->getBoundDecl();
1507+
ModuleDecl *M = typeDecl->getModuleContext();
1508+
bool isImplementationOnly = SF.isImportedImplementationOnly(M);
1509+
if (isImplementationOnly ||
1510+
(SF.isImportedAsSPI(typeDecl) && !context->isSPI())) {
1511+
diagnoser.diagnoseType(typeDecl, component, isImplementationOnly);
1512+
foundAnyIssues = true;
1513+
}
1514+
15111515
// We still continue even in the diagnostic case to report multiple
15121516
// violations.
15131517
return true;
@@ -1525,19 +1529,19 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
15251529

15261530
class ProblematicTypeFinder : public TypeDeclFinder {
15271531
const SourceFile &SF;
1532+
const Decl *context;
15281533
const Diagnoser &diagnoser;
15291534
public:
1530-
ProblematicTypeFinder(const SourceFile &SF, const Diagnoser &diagnoser)
1531-
: SF(SF), diagnoser(diagnoser) {}
1535+
ProblematicTypeFinder(const SourceFile &SF, const Decl *context, const Diagnoser &diagnoser)
1536+
: SF(SF), context(context), diagnoser(diagnoser) {}
15321537

15331538
void visitTypeDecl(const TypeDecl *typeDecl) {
15341539
ModuleDecl *M = typeDecl->getModuleContext();
1535-
if (!SF.isImportedImplementationOnly(M) &&
1536-
!SF.isImportedAsSPI(typeDecl))
1537-
return;
1538-
1539-
diagnoser.diagnoseType(typeDecl, /*typeRepr*/nullptr,
1540-
SF.isImportedImplementationOnly(M));
1540+
bool isImplementationOnly = SF.isImportedImplementationOnly(M);
1541+
if (isImplementationOnly ||
1542+
(SF.isImportedAsSPI(typeDecl) && !context->isSPI()))
1543+
diagnoser.diagnoseType(typeDecl, /*typeRepr*/nullptr,
1544+
isImplementationOnly);
15411545
}
15421546

15431547
void visitSubstitutionMap(SubstitutionMap subs) {
@@ -1597,15 +1601,15 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
15971601
}
15981602
};
15991603

1600-
type.walk(ProblematicTypeFinder(SF, diagnoser));
1604+
type.walk(ProblematicTypeFinder(SF, context, diagnoser));
16011605
}
16021606

16031607
void checkType(
16041608
Type type, const TypeRepr *typeRepr, const Decl *context,
16051609
const Diagnoser &diagnoser) {
16061610
auto *SF = context->getDeclContext()->getParentSourceFile();
16071611
assert(SF && "checking a non-source declaration?");
1608-
return checkTypeImpl(type, typeRepr, *SF, diagnoser);
1612+
return checkTypeImpl(type, typeRepr, *SF, context, diagnoser);
16091613
}
16101614

16111615
void checkType(
@@ -1702,7 +1706,7 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
17021706
AccessScope accessScope =
17031707
VD->getFormalAccessScope(nullptr,
17041708
/*treatUsableFromInlineAsPublic*/true);
1705-
if (accessScope.isPublic() && !accessScope.isSPI())
1709+
if (accessScope.isPublic())
17061710
return false;
17071711

17081712
// Is this a stored property in a non-resilient struct or class?
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/// @_implementationOnly imported decls (SPI or not) should not be exposed in SPI.
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: %target-swift-frontend -emit-module -DLIB %s -module-name Lib -emit-module-path %t/Lib.swiftmodule
5+
// RUN: %target-typecheck-verify-swift -DCLIENT -I %t
6+
7+
#if LIB
8+
9+
@_spi(A) public func spiFunc() {}
10+
11+
@_spi(A) public struct SPIStruct {
12+
public init() {}
13+
}
14+
15+
@_spi(A) public protocol SPIProtocol {}
16+
17+
public func ioiFunc() {}
18+
19+
public struct IOIStruct {
20+
public init() {}
21+
}
22+
23+
public protocol IOIProtocol {}
24+
25+
#elseif CLIENT
26+
27+
@_spi(A) @_implementationOnly import Lib
28+
29+
@_spi(B) public func leakSPIStruct(_ a: SPIStruct) -> SPIStruct { fatalError() } // expected-error 2 {{cannot use struct 'SPIStruct' here; 'Lib' has been imported as implementation-only}}
30+
@_spi(B) public func leakIOIStruct(_ a: IOIStruct) -> IOIStruct { fatalError() } // expected-error 2 {{cannot use struct 'IOIStruct' here; 'Lib' has been imported as implementation-only}}
31+
32+
public struct PublicStruct : IOIProtocol, SPIProtocol { // expected-error {{cannot use protocol 'IOIProtocol' here; 'Lib' has been imported as implementation-only}}
33+
// expected-error @-1 {{cannot use protocol 'SPIProtocol' here; 'Lib' has been imported as implementation-only}}
34+
public var spiStruct = SPIStruct() // expected-error {{cannot use struct 'SPIStruct' here; 'Lib' has been imported as implementation-only}}
35+
public var ioiStruct = IOIStruct() // expected-error {{cannot use struct 'IOIStruct' here; 'Lib' has been imported as implementation-only}}
36+
37+
@inlinable
38+
public func publicInlinable() {
39+
spiFunc() // expected-error {{global function 'spiFunc()' is '@_spi' and cannot be referenced from an '@inlinable' function}}
40+
ioiFunc() // expected-error {{global function 'ioiFunc()' cannot be used in an '@inlinable' function because 'Lib' was imported implementation-only}}
41+
let s = SPIStruct() // expected-error {{struct 'SPIStruct' is '@_spi' and cannot be referenced from an '@inlinable' function}}
42+
let i = IOIStruct() // expected-error {{struct 'IOIStruct' cannot be used in an '@inlinable' function because 'Lib' was imported implementation-only}}
43+
}
44+
}
45+
46+
#endif

0 commit comments

Comments
 (0)