Skip to content

Sema: Allow unavailable protocol witnesses in unavailable nominals or extensions #63898

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
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
18 changes: 17 additions & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,23 @@ RequirementCheck WitnessChecker::checkWitness(ValueDecl *requirement,

if (match.Witness->getAttrs().isUnavailable(getASTContext()) &&
!requirement->getAttrs().isUnavailable(getASTContext())) {
return CheckKind::WitnessUnavailable;
auto nominalOrExtensionIsUnavailable = [&]() {
if (auto extension = dyn_cast<ExtensionDecl>(DC)) {
if (extension->getAttrs().isUnavailable(getASTContext()))
return true;
}

if (auto adoptingNominal = DC->getSelfNominalTypeDecl()) {
if (adoptingNominal->getAttrs().isUnavailable(getASTContext()))
return true;
}

return false;
};

// Allow unavailable nominals or extension to have unavailable witnesses.
if (!nominalOrExtensionIsUnavailable())
return CheckKind::WitnessUnavailable;
}

return CheckKind::Success;
Expand Down
22 changes: 22 additions & 0 deletions test/decl/protocol/req/unavailable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ struct ConformsToP2 {
extension ConformsToP2: P {} // expected-error{{type 'ConformsToP2' does not conform to protocol 'P'}}
// expected-error@-1 {{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}

@available(*, unavailable)
struct ConformsToP3: P {
func foo(bar: Foo) { }
}

// Ok, an unavailable decl should be allowed to witness a requirement when the
// conforming type is itself unavailable.
@available(*, unavailable)
struct ConformsToP4: P {
@available(*, unavailable)
func foo(bar: Foo) { }
}

struct ConformsToP5 {}

// Ok, an unavailable decl should be allowed to witness a requirement when the
// conformance extension is itself unavailable.
@available(*, unavailable)
extension ConformsToP5: P {
@available(*, unavailable)
func foo(bar: Foo) { }
}

// Include message string from @available attribute if provided
protocol Unavail {
Expand Down