Skip to content

Fix crash when refining protocol from a testable import #25445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7177,8 +7177,7 @@ inline bool Decl::isPotentiallyOverridable() const {
isa<SubscriptDecl>(this) ||
isa<FuncDecl>(this) ||
isa<DestructorDecl>(this)) {
return getDeclContext()->getSelfClassDecl() ||
isa<ProtocolDecl>(getDeclContext());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. We just…didn't need the protocol case? Oh.

return getDeclContext()->getSelfClassDecl();
} else {
return false;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,10 +831,14 @@ static void checkOverrideAccessControl(ValueDecl *baseDecl, ValueDecl *decl,
if (ctx.isAccessControlDisabled())
return;

if (isa<ProtocolDecl>(decl->getDeclContext()))
return;

auto &diags = ctx.Diags;

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

bool isAccessor = isa<AccessorDecl>(decl);

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

} else if (!isa<ConstructorDecl>(decl) &&
!isa<ProtocolDecl>(decl->getDeclContext())) {
} else if (!isa<ConstructorDecl>(decl)) {
auto matchAccessScope =
baseDecl->getFormalAccessScope(dc);
auto classAccessScope =
Expand Down
3 changes: 3 additions & 0 deletions test/multifile/Inputs/protocol-override-other.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
protocol Base {
var foo: String { get }
}
9 changes: 9 additions & 0 deletions test/multifile/protocol-override.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module-path %t/other.swiftmodule %S/Inputs/protocol-override-other.swift -module-name other -enable-testing
// RUN: %target-swift-frontend -typecheck %s -I %t

@testable import other

protocol Sub : Base {
var foo: String { get set }
}