Skip to content

Commit d8cbb57

Browse files
authored
Merge pull request #84069 from tshortli/available-attr-exportability-reason
Sema: Introduce an `ExportabilityReason` for availability attributes
2 parents 4236db7 + 91ddf93 commit d8cbb57

File tree

5 files changed

+61
-59
lines changed

5 files changed

+61
-59
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3826,7 +3826,8 @@ ERROR(decl_from_hidden_module,none,
38263826
"as result builder here|"
38273827
"in an extension with public or '@usableFromInline' members|"
38283828
"in an extension with conditional conformances|"
3829-
"in a public or '@usableFromInline' conformance}1; "
3829+
"in a public or '@usableFromInline' conformance|"
3830+
"in an '@available' attribute here}1; "
38303831
"%select{%2 has been imported as implementation-only|"
38313832
"it is an SPI imported from %2|"
38323833
"it is SPI|"
@@ -3841,7 +3842,8 @@ ERROR(typealias_desugars_to_type_from_hidden_module,none,
38413842
"as result builder here|"
38423843
"in an extension with public or '@usableFromInline' members|"
38433844
"in an extension with conditional conformance|"
3844-
"in a public or '@usableFromInline' conformance}3 "
3845+
"in a public or '@usableFromInline' conformance|"
3846+
"<<ERROR>>}3 "
38453847
"because %select{%4 has been imported as implementation-only|"
38463848
"it is an SPI imported from %4|"
38473849
"<<ERROR>>|"
@@ -3855,7 +3857,7 @@ ERROR(conformance_from_implementation_only_module,none,
38553857
"as result builder here|"
38563858
"in an extension with public or '@usableFromInline' members|"
38573859
"in an extension with conditional conformances|"
3858-
"<<ERROR>>}2; "
3860+
"<<ERROR>>|<<ERROR>>}2; "
38593861
"%select{%3 has been imported as implementation-only|"
38603862
"the conformance is declared as SPI in %3|"
38613863
"the conformance is declared as SPI|"

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
257257
return true;
258258
case ExportabilityReason::Inheritance:
259259
return isa<ProtocolDecl>(D);
260+
case ExportabilityReason::AvailableAttribute:
261+
// If the context is an extension and that extension has an explicit
262+
// access level, then access has already been diagnosed for the
263+
// @available attribute.
264+
if (auto *ED = dyn_cast_or_null<ExtensionDecl>(DC->getAsDecl()))
265+
return !ED->getAttrs().getAttribute<AccessControlAttr>();
266+
return false;
260267
default:
261268
return false;
262269
}

lib/Sema/TypeCheckAccess.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,11 +2261,13 @@ class DeclAvailabilityChecker : public DeclVisitor<DeclAvailabilityChecker> {
22612261

22622262
void checkAvailabilityDomains(const Decl *D) {
22632263
D = D->getAbstractSyntaxDeclForAttributes();
2264+
2265+
auto where = Where.withReason(ExportabilityReason::AvailableAttribute);
22642266
for (auto attr : D->getSemanticAvailableAttrs()) {
22652267
if (auto *domainDecl = attr.getDomain().getDecl()) {
22662268
diagnoseDeclAvailability(domainDecl,
22672269
attr.getParsedAttr()->getDomainLoc(), nullptr,
2268-
Where, std::nullopt);
2270+
where, std::nullopt);
22692271
}
22702272
}
22712273
}
@@ -2557,18 +2559,6 @@ class DeclAvailabilityChecker : public DeclVisitor<DeclAvailabilityChecker> {
25572559
: ExportabilityReason::ExtensionWithConditionalConformances;
25582560
checkConstrainedExtensionRequirements(ED, reason);
25592561

2560-
// Diagnose the exportability of the availability domains referenced by the
2561-
// @available attributes attached to the extension.
2562-
if (Where.isExported()) {
2563-
for (auto availableAttr : ED->getSemanticAvailableAttrs()) {
2564-
if (auto *domainDecl = availableAttr.getDomain().getDecl()) {
2565-
TypeChecker::diagnoseDeclRefExportability(
2566-
availableAttr.getParsedAttr()->getDomainLoc(), domainDecl,
2567-
Where.withReason(reason));
2568-
}
2569-
}
2570-
}
2571-
25722562
// If we haven't already visited the extended nominal visit it here.
25732563
// This logic is too wide but prevents false reports of an unused public
25742564
// import. We should instead check for public generic requirements

lib/Sema/TypeCheckAvailability.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ enum class ExportabilityReason : unsigned {
7777
ResultBuilder,
7878
ExtensionWithPublicMembers,
7979
ExtensionWithConditionalConformances,
80-
Inheritance
80+
Inheritance,
81+
AvailableAttribute,
8182
};
8283

8384
/// A description of the restrictions on what declarations can be referenced

0 commit comments

Comments
 (0)