Skip to content

Commit 9993124

Browse files
authored
Merge pull request #25445 from slavapestov/testable-protocol-override
Fix crash when refining protocol from a testable import
2 parents a813fad + d7db48f commit 9993124

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7190,8 +7190,7 @@ inline bool Decl::isPotentiallyOverridable() const {
71907190
isa<SubscriptDecl>(this) ||
71917191
isa<FuncDecl>(this) ||
71927192
isa<DestructorDecl>(this)) {
7193-
return getDeclContext()->getSelfClassDecl() ||
7194-
isa<ProtocolDecl>(getDeclContext());
7193+
return getDeclContext()->getSelfClassDecl();
71957194
} else {
71967195
return false;
71977196
}

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,10 +831,14 @@ static void checkOverrideAccessControl(ValueDecl *baseDecl, ValueDecl *decl,
831831
if (ctx.isAccessControlDisabled())
832832
return;
833833

834+
if (isa<ProtocolDecl>(decl->getDeclContext()))
835+
return;
836+
834837
auto &diags = ctx.Diags;
835838

836839
auto dc = decl->getDeclContext();
837840
auto classDecl = dc->getSelfClassDecl();
841+
assert(classDecl != nullptr && "Should have ruled out protocols above");
838842

839843
bool isAccessor = isa<AccessorDecl>(decl);
840844

@@ -851,8 +855,7 @@ static void checkOverrideAccessControl(ValueDecl *baseDecl, ValueDecl *decl,
851855
if (!isAccessor &&
852856
!baseHasOpenAccess &&
853857
baseDecl->getModuleContext() != decl->getModuleContext() &&
854-
!isa<ConstructorDecl>(decl) &&
855-
!isa<ProtocolDecl>(decl->getDeclContext())) {
858+
!isa<ConstructorDecl>(decl)) {
856859
// NSObject.hashValue was made non-overridable in Swift 5; one should
857860
// override NSObject.hash instead.
858861
if (isNSObjectHashValue(baseDecl)) {
@@ -875,8 +878,7 @@ static void checkOverrideAccessControl(ValueDecl *baseDecl, ValueDecl *decl,
875878
}
876879
diags.diagnose(baseDecl, diag::overridden_here);
877880

878-
} else if (!isa<ConstructorDecl>(decl) &&
879-
!isa<ProtocolDecl>(decl->getDeclContext())) {
881+
} else if (!isa<ConstructorDecl>(decl)) {
880882
auto matchAccessScope =
881883
baseDecl->getFormalAccessScope(dc);
882884
auto classAccessScope =
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
protocol Base {
2+
var foo: String { get }
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module-path %t/other.swiftmodule %S/Inputs/protocol-override-other.swift -module-name other -enable-testing
3+
// RUN: %target-swift-frontend -typecheck %s -I %t
4+
5+
@testable import other
6+
7+
protocol Sub : Base {
8+
var foo: String { get set }
9+
}

0 commit comments

Comments
 (0)