Skip to content

Commit 71035a5

Browse files
committed
Sema: Accept @_implementationOnly on non-public structs
Gate it behind CheckImplementationOnly.
1 parent 62ee448 commit 71035a5

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

include/swift/AST/DeclAttr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ SIMPLE_DECL_ATTR(_alwaysEmitIntoClient, AlwaysEmitIntoClient,
474474
83)
475475

476476
SIMPLE_DECL_ATTR(_implementationOnly, ImplementationOnly,
477-
OnImport | OnFunc | OnConstructor | OnVar | OnSubscript,
477+
OnImport | OnFunc | OnConstructor | OnVar | OnSubscript | OnStruct,
478478
UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | UnreachableInABIAttr,
479479
84)
480480

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3946,6 +3946,9 @@ ERROR(implementation_only_override_import_without_attr,none,
39463946
"override of %kindonly0 imported as implementation-only must be declared "
39473947
"'@_implementationOnly'",
39483948
(const ValueDecl *))
3949+
ERROR(implementation_only_on_structs_feature,none,
3950+
"'@_implementationOnly' on structs requires "
3951+
"'-enable-experimental-feature CheckImplementationOnly'", ())
39493952

39503953
ERROR(import_attr_conflict,none,
39513954
"%0 inconsistently imported with %1",
@@ -4128,7 +4131,7 @@ WARNING(attr_has_no_effect_on_decl_with_access_level,none,
41284131
(DeclAttribute, AccessLevel))
41294132
ERROR(attr_not_on_decl_with_invalid_access_level,none,
41304133
"'%0' may not be used on "
4131-
"%select{private|fileprivate|internal|package|%error|%error}1 declarations",
4134+
"%select{private|fileprivate|internal|package|public|%error}1 declarations",
41324135
(DeclAttribute, AccessLevel))
41334136

41344137
ERROR(attr_has_no_effect_decl_not_available_before,none,

lib/Sema/TypeCheckAttr.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4995,7 +4995,25 @@ AttributeChecker::visitImplementationOnlyAttr(ImplementationOnlyAttr *attr) {
49954995
return;
49964996
}
49974997

4998+
// @_implementationOnly on structs only applies to non-public types.
49984999
auto *VD = cast<ValueDecl>(D);
5000+
if (isa<StructDecl>(VD)) {
5001+
if (!Ctx.LangOpts.hasFeature(Feature::CheckImplementationOnly)) {
5002+
diagnoseAndRemoveAttr(attr,
5003+
diag::implementation_only_on_structs_feature);
5004+
return;
5005+
}
5006+
5007+
auto access =
5008+
VD->getFormalAccessScope(/*useDC=*/nullptr,
5009+
/*treatUsableFromInlineAsPublic=*/true);
5010+
if (access.isPublicOrPackage())
5011+
diagnoseAndRemoveAttr(
5012+
attr, diag::attr_not_on_decl_with_invalid_access_level,
5013+
attr, access.accessLevelForDiagnostics());
5014+
return;
5015+
}
5016+
49995017
auto *overridden = VD->getOverriddenDecl();
50005018
if (!overridden) {
50015019
diagnoseAndRemoveAttr(attr, diag::implementation_only_decl_non_override);

0 commit comments

Comments
 (0)