Skip to content

Commit 7c9a626

Browse files
authored
Merge pull request #58801 from tshortli/accept-existential-without-any-in-swiftinterfaces
2 parents c80fa27 + 60146b3 commit 7c9a626

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,11 +3572,12 @@ void swift::performAbstractFuncDeclDiagnostics(AbstractFunctionDecl *AFD) {
35723572
}
35733573

35743574
// Perform MiscDiagnostics on Switch Statements.
3575-
static void checkSwitch(ASTContext &ctx, const SwitchStmt *stmt) {
3575+
static void checkSwitch(ASTContext &ctx, const SwitchStmt *stmt,
3576+
DeclContext *DC) {
35763577
// We want to warn about "case .Foo, .Bar where 1 != 100:" since the where
35773578
// clause only applies to the second case, and this is surprising.
35783579
for (auto cs : stmt->getCases()) {
3579-
TypeChecker::checkExistentialTypes(ctx, cs);
3580+
TypeChecker::checkExistentialTypes(ctx, cs, DC);
35803581

35813582
// The case statement can have multiple case items, each can have a where.
35823583
// If we find a "where", and there is a preceding item without a where, and
@@ -5070,10 +5071,10 @@ void swift::performSyntacticExprDiagnostics(const Expr *E,
50705071
void swift::performStmtDiagnostics(const Stmt *S, DeclContext *DC) {
50715072
auto &ctx = DC->getASTContext();
50725073

5073-
TypeChecker::checkExistentialTypes(ctx, const_cast<Stmt *>(S));
5074-
5074+
TypeChecker::checkExistentialTypes(ctx, const_cast<Stmt *>(S), DC);
5075+
50755076
if (auto switchStmt = dyn_cast<SwitchStmt>(S))
5076-
checkSwitch(ctx, switchStmt);
5077+
checkSwitch(ctx, switchStmt, DC);
50775078

50785079
checkStmtConditionTrailingClosure(ctx, S);
50795080

lib/Sema/TypeCheckType.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4382,6 +4382,11 @@ void TypeChecker::checkExistentialTypes(Decl *decl) {
43824382
if (!decl || decl->isInvalid())
43834383
return;
43844384

4385+
// Skip diagnosing existential `any` requirements in swiftinterfaces.
4386+
auto sourceFile = decl->getDeclContext()->getParentSourceFile();
4387+
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface)
4388+
return;
4389+
43854390
auto &ctx = decl->getASTContext();
43864391
if (auto *protocolDecl = dyn_cast<ProtocolDecl>(decl)) {
43874392
checkExistentialTypes(ctx, protocolDecl->getTrailingWhereClause());
@@ -4412,10 +4417,16 @@ void TypeChecker::checkExistentialTypes(Decl *decl) {
44124417
decl->walk(visitor);
44134418
}
44144419

4415-
void TypeChecker::checkExistentialTypes(ASTContext &ctx, Stmt *stmt) {
4420+
void TypeChecker::checkExistentialTypes(ASTContext &ctx, Stmt *stmt,
4421+
DeclContext *DC) {
44164422
if (!stmt)
44174423
return;
44184424

4425+
// Skip diagnosing existential `any` requirements in swiftinterfaces.
4426+
auto sourceFile = DC->getParentSourceFile();
4427+
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface)
4428+
return;
4429+
44194430
ExistentialTypeVisitor visitor(ctx, /*checkStatements=*/true);
44204431
stmt->walk(visitor);
44214432
}

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ Expr *resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
295295
void checkExistentialTypes(Decl *decl);
296296

297297
/// Check for invalid existential types in the given statement.
298-
void checkExistentialTypes(ASTContext &ctx, Stmt *stmt);
298+
void checkExistentialTypes(ASTContext &ctx, Stmt *stmt, DeclContext *DC);
299299

300300
/// Check for invalid existential types in the underlying type of
301301
/// the given type alias.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-compiler-version: Swift version 5.7-dev (LLVM cd62c186b914a47, Swift d74d00ef63ab637)
3+
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name ExistentialAnyMissing
4+
import Swift
5+
public protocol P {
6+
}
7+
public protocol Q {
8+
associatedtype A : ExistentialAnyMissing.P
9+
}
10+
public func takesAndReturnsP(_ x: ExistentialAnyMissing.P) -> ExistentialAnyMissing.P
11+
public func takesAndReturnsOptionalP(_ x: ExistentialAnyMissing.P?) -> ExistentialAnyMissing.P?
12+
public func takesAndReturnsQ(_ x: ExistentialAnyMissing.Q) -> ExistentialAnyMissing.Q
13+
public struct S {
14+
public var p: ExistentialAnyMissing.P
15+
public var maybeP: ExistentialAnyMissing.P?
16+
public var q: ExistentialAnyMissing.Q
17+
public var maybeQ: ExistentialAnyMissing.Q?
18+
}
19+
@inlinable internal func inlinableTakesAny(_ a: Any) {
20+
switch a {
21+
case is P: break
22+
case is Q: break
23+
default: break
24+
}
25+
}

test/ModuleInterface/existential-any.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name main
33
// RUN: %FileCheck %s < %t.swiftinterface
44

5+
// Verify that `any` is not required in swiftinterfaces.
6+
// RUN: %target-swift-typecheck-module-from-interface(%S/Inputs/existential-any-ignore-missing-in-interface.swiftinterface) -module-name ExistentialAnyMissing
7+
58
// CHECK: public protocol P
69
public protocol P { }
710

0 commit comments

Comments
 (0)