Skip to content

Commit ee4fd56

Browse files
authored
Merge pull request #83910 from tshortli/custom-availability-domain-access-checking
Sema: Check access, availability, and exportability of availability domains
2 parents 94f7c54 + 0b5d510 commit ee4fd56

16 files changed

+587
-98
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6989,6 +6989,17 @@ WARNING(attr_availability_expected_version_spec, none,
69896989
ERROR(attr_availability_requires_custom_availability, none,
69906990
"%0 requires '-enable-experimental-feature CustomAvailability'",
69916991
(AvailabilityDomain))
6992+
ERROR(attr_availability_domain_access, none,
6993+
"availability domain '%0' is "
6994+
"%select{private|fileprivate|internal|package|%error|%error}1 "
6995+
"and cannot be used in '%2' on "
6996+
"%select{private|fileprivate|internal|package|public|%error}3 %kind4",
6997+
(AvailabilityDomain, AccessLevel, DeclAttribute, AccessLevel,
6998+
const Decl *))
6999+
ERROR(attr_availability_domain_not_usable_from_inline, none,
7000+
"availability domain '%0' used in '%1' on %kind2 must be "
7001+
"'@usableFromInline' or public",
7002+
(AvailabilityDomain, DeclAttribute, const Decl *))
69927003

69937004
ERROR(availability_decl_unavailable, none,
69947005
"%0 is unavailable%select{ in %2|}1%select{|: %3}3",

lib/AST/AvailabilityDomain.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ getCustomDomainKind(clang::FeatureAvailKind featureAvailKind) {
4141
static const CustomAvailabilityDomain *
4242
customDomainForClangDecl(ValueDecl *decl) {
4343
auto *clangDecl = decl->getClangDecl();
44-
ASSERT(clangDecl);
45-
46-
auto *varDecl = dyn_cast<clang::VarDecl>(clangDecl);
44+
auto *varDecl = dyn_cast_or_null<clang::VarDecl>(clangDecl);
4745
if (!varDecl)
4846
return nullptr;
4947

lib/AST/DiagnosticEngine.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "swift/AST/DiagnosticEngine.h"
19+
#include "swift/AST/AvailabilityDomain.h"
1920
#include "swift/AST/ASTContext.h"
2021
#include "swift/AST/ASTPrinter.h"
2122
#include "swift/AST/Decl.h"
@@ -877,6 +878,20 @@ static void formatDiagnosticArgument(StringRef Modifier,
877878
assert(Modifier.empty() && "Improper modifier for ValueDecl argument");
878879
}
879880

881+
// Handle declarations representing an AvailabilityDomain specially.
882+
if (auto VD = dyn_cast<ValueDecl>(D)) {
883+
if (auto domain = AvailabilityDomain::forCustom(const_cast<ValueDecl *>(VD))) {
884+
Out << "availability domain";
885+
886+
if (includeName) {
887+
Out << " " << FormatOpts.OpeningQuotationMark;
888+
Out << domain->getNameForDiagnostics();
889+
Out << FormatOpts.ClosingQuotationMark;
890+
}
891+
break;
892+
}
893+
}
894+
880895
if (includeName) {
881896
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
882897
// If it's an accessor, describe that and then switch to discussing its

lib/Sema/MiscDiagnostics.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5257,6 +5257,12 @@ static bool diagnoseAvailabilityCondition(PoundAvailableInfo *info,
52575257
return true;
52585258
}
52595259

5260+
// Check the availability of the domain decl.
5261+
if (auto *domainDecl = spec.getDomain().getDecl()) {
5262+
auto where = ExportContext::forFunctionBody(DC, loc);
5263+
diagnoseDeclAvailability(domainDecl, loc, nullptr, where);
5264+
}
5265+
52605266
hasValidSpecs = true;
52615267
if (!domain.isPlatform())
52625268
allValidSpecsArePlatform = false;

0 commit comments

Comments
 (0)