Skip to content

[5.4] Fix a Family of Crashers in Availability Checking #36104

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 1 commit into from
Feb 23, 2021
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
6 changes: 5 additions & 1 deletion lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ static void computeExportContextBits(ASTContext &Ctx, Decl *D,
if (D->isSPI())
*spi = true;

if (D->isImplicit())
// Defer bodies are desugared to an implicit closure expression. We need to
// dilute the meaning of "implicit" to make sure we're still checking
// availability inside of defer statements.
const auto isDeferBody = isa<FuncDecl>(D) && cast<FuncDecl>(D)->isDeferBody();
if (D->isImplicit() && !isDeferBody)
*implicit = true;

if (D->getAttrs().getDeprecated(Ctx))
Expand Down
29 changes: 29 additions & 0 deletions test/Sema/availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,32 @@ struct VarToFunc {
}
}

struct DeferBody {
func foo() {
enum No: Error {
case no
}

defer {
do {
throw No.no
} catch No.no {
} catch {
}
}
_ = ()
}

func bar() {
@available(*, unavailable)
enum No: Error { // expected-note 2 {{'No' has been explicitly marked unavailable here}}
case no
}
do {
throw No.no
// expected-error@-1 {{'No' is unavailable}}
} catch No.no {} catch _ {}
// expected-error@-1 {{'No' is unavailable}}
}
}

9 changes: 9 additions & 0 deletions test/attr/attr_inlinable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,12 @@ public struct PrivateInlinableCrash {
// expected-error@-2 {{initializer 'init()' is private and cannot be referenced from an '@inlinable' function}}
}
}

// Just make sure we don't crash.
private func deferBodyTestCall() {} // expected-note {{global function 'deferBodyTestCall()' is not '@usableFromInline' or public}}
@inlinable public func deferBodyTest() {
defer {
deferBodyTestCall() // expected-error {{global function 'deferBodyTestCall()' is private and cannot be referenced from an '@inlinable' function}}
}
_ = ()
}