Skip to content

Commit

Permalink
Allow the use of unavailable types from unavailable code
Browse files Browse the repository at this point in the history
This change permits the use of unavailable types in unavailable signatures.
It affects only usage of `available(*, unavailable)`.

This fixes a compilation error in swift-corelibs-foundation/Foundation/
URLSession/URLSessionConfiguration.swift where an unavailable property
was typed by an unavailable type.
  • Loading branch information
xymus committed Jul 3, 2019
1 parent eb28e2a commit abbbc01
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1503,13 +1503,17 @@ static bool isInsideUnavailableDeclaration(SourceRange ReferenceRange,
/// Returns true if the reference or any of its parents is an
/// unconditional unavailable declaration for the same platform.
static bool isInsideCompatibleUnavailableDeclaration(
SourceRange ReferenceRange, const DeclContext *ReferenceDC,
const AvailableAttr *attr) {
const ValueDecl *referencedD, SourceRange ReferenceRange,
const DeclContext *ReferenceDC, const AvailableAttr *attr) {
if (!attr->isUnconditionallyUnavailable()) {
return false;
}

// Refuse calling unavailable functions from unavailable code,
// but allow the use of types.
PlatformKind platform = attr->Platform;
if (platform == PlatformKind::none) {
if (platform == PlatformKind::none &&
!isa<TypeDecl>(referencedD)) {
return false;
}

Expand Down Expand Up @@ -2129,7 +2133,7 @@ bool swift::diagnoseExplicitUnavailability(
// unavailability is OK -- the eventual caller can't call the
// enclosing code in the same situations it wouldn't be able to
// call this code.
if (isInsideCompatibleUnavailableDeclaration(R, DC, Attr)) {
if (isInsideCompatibleUnavailableDeclaration(D, R, DC, Attr)) {
return false;
}

Expand Down
13 changes: 13 additions & 0 deletions test/attr/attr_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1087,3 +1087,16 @@ func rdar46348825_deprecated() {}
@available(swift, obsoleted: 4.0, obsoleted: 4.0)
// expected-warning@-1 {{'obsoleted' argument has already been specified}}
func rdar46348825_obsoleted() {}

// Referencing unavailable types in signatures of unavailable functions should be accepted
@available(*, unavailable)
protocol UnavailableProto {
}

@available(*, unavailable)
func unavailableFunc(_ arg: UnavailableProto) -> UnavailableProto {}

@available(*, unavailable)
struct S {
var a: UnavailableProto
}

0 comments on commit abbbc01

Please sign in to comment.