Skip to content

Commit 1e1361f

Browse files
authored
Merge pull request #85369 from xymus/exportability-nle-classes-and-structs
Sema: Exportability check enums and classes in non-library-evolution mode
2 parents 1bb65d8 + b107169 commit 1e1361f

File tree

7 files changed

+256
-25
lines changed

7 files changed

+256
-25
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 | OnStruct,
477+
OnImport | OnFunc | OnConstructor | OnVar | OnSubscript | OnStruct | OnClass | OnEnum,
478478
UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | UnreachableInABIAttr,
479479
84)
480480

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3851,7 +3851,7 @@ ERROR(decl_from_hidden_module,none,
38513851
"C++ types from imported module %2 do not support library evolution|"
38523852
"it was imported via the internal bridging header|"
38533853
"%2 was not imported publicly|"
3854-
"it is a struct marked '@_implementationOnly'}3",
3854+
"%0 is marked '@_implementationOnly'}3",
38553855
(const Decl *, unsigned, Identifier, unsigned))
38563856
ERROR(typealias_desugars_to_type_from_hidden_module,none,
38573857
"%0 aliases '%1.%2' and cannot be used %select{here|"
@@ -3870,7 +3870,7 @@ ERROR(typealias_desugars_to_type_from_hidden_module,none,
38703870
"C++ types from imported module %4 do not support library evolution|"
38713871
"it was imported via the internal bridging header|"
38723872
"%4 was not imported publicly|"
3873-
"it is a struct marked '@_implementationOnly'}5",
3873+
"%0 is marked '@_implementationOnly'}5",
38743874
(const TypeAliasDecl *, StringRef, StringRef, unsigned, Identifier, unsigned))
38753875
ERROR(conformance_from_implementation_only_module,none,
38763876
"cannot use conformance of %0 to %1 %select{here|as property wrapper here|"
@@ -3887,7 +3887,7 @@ ERROR(conformance_from_implementation_only_module,none,
38873887
"C++ types from imported module %3 do not support library evolution|"
38883888
"it was imported via the internal bridging header|"
38893889
"%3 was not imported publicly|"
3890-
"it is a struct marked '@_implementationOnly'}4",
3890+
"%0 is marked '@_implementationOnly'}4",
38913891
(Type, Identifier, unsigned, Identifier, unsigned))
38923892
NOTE(assoc_conformance_from_implementation_only_module,none,
38933893
"in associated type %0 (inferred as %1)", (Type, Type))
@@ -3952,8 +3952,8 @@ ERROR(implementation_only_override_import_without_attr,none,
39523952
"override of %kindonly0 imported as implementation-only must be declared "
39533953
"'@_implementationOnly'",
39543954
(const ValueDecl *))
3955-
ERROR(implementation_only_on_structs_feature,none,
3956-
"'@_implementationOnly' on structs requires "
3955+
ERROR(implementation_only_on_types_feature,none,
3956+
"'@_implementationOnly' on a type requires "
39573957
"'-enable-experimental-feature CheckImplementationOnly'", ())
39583958

39593959
ERROR(import_attr_conflict,none,
@@ -7360,7 +7360,7 @@ ERROR(inlinable_decl_ref_from_hidden_module,
73607360
"C++ APIs from imported module %2 do not support library evolution|"
73617361
"it was imported via the internal bridging header|"
73627362
"%2 was not imported publicly|"
7363-
"it is a struct marked '@_implementationOnly'}3",
7363+
"%0 is marked '@_implementationOnly'}3",
73647364
(const ValueDecl *, unsigned, Identifier, unsigned))
73657365

73667366
ERROR(inlinable_typealias_desugars_to_type_from_hidden_module,
@@ -7373,7 +7373,7 @@ ERROR(inlinable_typealias_desugars_to_type_from_hidden_module,
73737373
"C++ types from imported module %4 do not support library evolution|"
73747374
"it was imported via the internal bridging header|"
73757375
"%4 was not imported publicly|"
7376-
"it is a struct marked '@_implementationOnly'}5",
7376+
"%0 is marked '@_implementationOnly'}5",
73777377
(const TypeAliasDecl *, StringRef, StringRef, unsigned, Identifier, unsigned))
73787378

73797379
NOTE(missing_import_inserted,

lib/AST/Availability.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,19 @@ bool swift::isExported(const ValueDecl *VD) {
10091009
if (property->isLayoutExposedToClients(/*applyImplicit=*/true))
10101010
return true;
10111011

1012+
// Is this a type exposed by default in a non-resilient module?
1013+
if (isa<NominalTypeDecl>(VD) &&
1014+
VD->getASTContext().LangOpts.hasFeature(
1015+
Feature::CheckImplementationOnly) &&
1016+
VD->getDeclContext()->getParentModule()->getResilienceStrategy() !=
1017+
ResilienceStrategy::Resilient &&
1018+
!VD->getAttrs().hasAttribute<ImplementationOnlyAttr>())
1019+
return true;
1020+
1021+
// Case of an enum not marked @_implementationOnly in a non-resilient module?
1022+
if (auto *EED = dyn_cast<EnumElementDecl>(VD))
1023+
return isExported(EED->getParentEnum());
1024+
10121025
return false;
10131026
}
10141027

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5025,12 +5025,12 @@ AttributeChecker::visitImplementationOnlyAttr(ImplementationOnlyAttr *attr) {
50255025
return;
50265026
}
50275027

5028-
// @_implementationOnly on structs only applies to non-public types.
50295028
auto *VD = cast<ValueDecl>(D);
5030-
if (isa<StructDecl>(VD)) {
5029+
5030+
// @_implementationOnly on types only applies to non-public types.
5031+
if (isa<NominalTypeDecl>(D)) {
50315032
if (!Ctx.LangOpts.hasFeature(Feature::CheckImplementationOnly)) {
5032-
diagnoseAndRemoveAttr(attr,
5033-
diag::implementation_only_on_structs_feature);
5033+
diagnoseAndRemoveAttr(attr, diag::implementation_only_on_types_feature);
50345034
return;
50355035
}
50365036

test/Sema/Inputs/implementation-only-imports/directs.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ extension StructFromIndirect {
2929
set {}
3030
}
3131
}
32+
33+
public struct RawTypeFromDirect : Equatable, ExpressibleByIntegerLiteral {
34+
public typealias IntegerLiteralType = Int
35+
public init(integerLiteral: Int) {}
36+
}
37+
38+
public protocol ProtocolFromDirect { }

0 commit comments

Comments
 (0)