Skip to content

Extend transitive availability checking to initial value expressions #22438

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

Closed
Closed
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
25 changes: 25 additions & 0 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "TypeChecker.h"
#include "MiscDiagnostics.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/TypeRefinementContext.h"
Expand Down Expand Up @@ -1408,6 +1409,30 @@ someEnclosingDeclMatches(SourceRange ReferenceRange,
// Climb the DeclContext hierarchy to see if any of the containing
// declarations matches the predicate.
const DeclContext *DC = ReferenceDC;

if (auto *InitializerDC = dyn_cast<PatternBindingInitializer>(DC)) {
const PatternBindingDecl *BindingDecl = InitializerDC->getBinding();
assert(BindingDecl);

const Pattern *RelevantPattern =
BindingDecl->getPattern(InitializerDC->getBindingIndex());

// FIXME: Every variable in the same pattern will have the same attributes,
// so for all the predicates we care about it's a waste to test more than
// one.
// FIXME: If there are /no/ variables in the pattern, we won't be able to
// check anything, even though there may have been attributes syntactically
// written on the PatternBindingDecl (`var` or `let`).
bool SatisfiesPredicate = false;
RelevantPattern->forEachVariable([&](const VarDecl *Var) {
SatisfiesPredicate |= Pred(Var);
});
if (SatisfiesPredicate)
return true;

DC = InitializerDC->getParent();
}

do {
auto *D = DC->getInnermostDeclarationDeclContext();
if (!D)
Expand Down
48 changes: 46 additions & 2 deletions test/attr/attr_availability_transitive_osx.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -parse-as-library
// REQUIRES: OS=macosx

// Allow referencing unavailable API in situations where the caller is marked unavailable in the same circumstances.

@available(OSX, unavailable)
func osx() {} // expected-note 2{{'osx()' has been explicitly marked unavailable here}}
@discardableResult
func osx() -> Int { return 0 } // expected-note 7 {{'osx()' has been explicitly marked unavailable here}}

@available(OSXApplicationExtension, unavailable)
func osx_extension() {}

@available(OSX, unavailable)
func osx_pair() -> (Int, Int) { return (0, 0) } // expected-note {{'osx_pair()' has been explicitly marked unavailable here}}

func call_osx_extension() {
osx_extension() // OK; osx_extension is only unavailable if -application-extension is passed.
}
Expand All @@ -35,3 +39,43 @@ func osx_extension_call_osx_extension() {
func osx_extension_call_osx() {
osx() // expected-error {{'osx()' is unavailable}}
}

@available(OSX, unavailable)
var osx_init_osx = osx() // OK

@available(OSXApplicationExtension, unavailable)
var osx_extension_init_osx = osx() // expected-error {{'osx()' is unavailable}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this test pass?

@available(OSX, unavailable)
var osx_inner_init_osx = { let inner_var = osx() }

If so, is it worth adding to the test suite? If not, do we want to merge this change anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely seems worth adding! I think it'll work with this change, but it's always worth checking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aha, it does not work. Important info!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to look at this this afternoon, and if there's not an easy fix maybe we'll just take this part.


struct Outer {
@available(OSX, unavailable)
var osx_init_osx = osx() // OK

@available(OSX, unavailable)
lazy var osx_lazy_osx = osx() // OK

@available(OSXApplicationExtension, unavailable)
var osx_extension_init_osx = osx() // expected-error {{'osx()' is unavailable}}

@available(OSXApplicationExtension, unavailable)
var osx_extension_lazy_osx = osx() // expected-error {{'osx()' is unavailable}}

@available(OSX, unavailable)
var osx_init_multi1_osx = osx(), osx_init_multi2_osx = osx() // OK

@available(OSXApplicationExtension, unavailable)
var osx_extension_init_multi1_osx = osx(), osx_extension_init_multi2_osx = osx() // expected-error 2 {{'osx()' is unavailable}}

@available(OSX, unavailable)
var (osx_init_deconstruct1_osx, osx_init_deconstruct2_osx) = osx_pair() // OK

@available(OSXApplicationExtension, unavailable)
var (osx_extension_init_deconstruct1_osx, osx_extension_init_deconstruct2_osx) = osx_pair() // expected-error {{'osx_pair()' is unavailable}}
}

@available(OSX, unavailable)
struct NotOnOSX {
var osx_init_osx = osx() // OK
lazy var osx_lazy_osx = osx() // OK
var osx_init_multi1_osx = osx(), osx_init_multi2_osx = osx() // OK
var (osx_init_deconstruct1_osx, osx_init_deconstruct2_osx) = osx_pair() // OK
}