Skip to content

API checker: only diagnose adding enum cases to exhaustive enums #31997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/IDE/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ KEY_BOOL(HasStorage, hasStorage)
KEY_BOOL(ReqNewWitnessTableEntry, reqNewWitnessTableEntry)
KEY_BOOL(IsABIPlaceholder, isABIPlaceholder)
KEY_BOOL(IsExternal, isExternal)
KEY_BOOL(IsEnumExhaustive, isEnumExhaustive)
KEY_BOOL(HasMissingDesignatedInitializers, hasMissingDesignatedInitializers)
KEY_BOOL(InheritsConvenienceInitializers, inheritsConvenienceInitializers)

Expand Down
3 changes: 3 additions & 0 deletions test/api-digester/Inputs/cake_baseline/cake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public struct C6 {}
@frozen
public enum IceKind {}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public enum FutureKind {}

public protocol P1 {}

public protocol P2 {}
Expand Down
5 changes: 5 additions & 0 deletions test/api-digester/Inputs/cake_current/cake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public struct C6 {}

public enum IceKind {}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public enum FutureKind {
case FineToAdd
}

public protocol P1 {}

public protocol P2 {}
Expand Down
1 change: 1 addition & 0 deletions test/api-digester/Outputs/cake-abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@
"usr": "s:4cake6NumberO",
"moduleName": "cake",
"enumRawTypeName": "Int",
"isEnumExhaustive": true,
"conformances": [
{
"kind": "Conformance",
Expand Down
1 change: 1 addition & 0 deletions test/api-digester/Outputs/cake.json
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@
"usr": "s:4cake6NumberO",
"moduleName": "cake",
"enumRawTypeName": "Int",
"isEnumExhaustive": true,
"conformances": [
{
"kind": "Conformance",
Expand Down
2 changes: 1 addition & 1 deletion test/api-digester/dump-module.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// REQUIRES: macosx
// REQUIRES: OS=macosx
// RUN: %empty-directory(%t.mod)
// RUN: %empty-directory(%t.sdk)
// RUN: %empty-directory(%t.module-cache)
Expand Down
6 changes: 5 additions & 1 deletion tools/swift-api-digester/ModuleAnalyzerNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ SDKNodeTypeAlias::SDKNodeTypeAlias(SDKNodeInitInfo Info):
SDKNodeDeclType::SDKNodeDeclType(SDKNodeInitInfo Info):
SDKNodeDecl(Info, SDKNodeKind::DeclType), SuperclassUsr(Info.SuperclassUsr),
SuperclassNames(Info.SuperclassNames),
EnumRawTypeName(Info.EnumRawTypeName), IsExternal(Info.IsExternal),
EnumRawTypeName(Info.EnumRawTypeName),
IsExternal(Info.IsExternal),
IsEnumExhaustive(Info.IsEnumExhaustive),
HasMissingDesignatedInitializers(Info.HasMissingDesignatedInitializers),
InheritsConvenienceInitializers(Info.InheritsConvenienceInitializers) {}

Expand Down Expand Up @@ -1426,6 +1428,7 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)

// Get enum raw type name if this is an enum.
if (auto *ED = dyn_cast<EnumDecl>(VD)) {
IsEnumExhaustive = ED->isFormallyExhaustive(nullptr);
if (auto RT = ED->getRawType()) {
if (auto *D = RT->getNominalOrBoundGenericNominal()) {
EnumRawTypeName = D->getName().str();
Expand Down Expand Up @@ -1981,6 +1984,7 @@ void SDKNodeDeclType::jsonize(json::Output &out) {
output(out, KeyKind::KK_superclassUsr, SuperclassUsr);
output(out, KeyKind::KK_enumRawTypeName, EnumRawTypeName);
output(out, KeyKind::KK_isExternal, IsExternal);
output(out, KeyKind::KK_isEnumExhaustive, IsEnumExhaustive);
output(out, KeyKind::KK_hasMissingDesignatedInitializers,
HasMissingDesignatedInitializers);
output(out, KeyKind::KK_inheritsConvenienceInitializers,
Expand Down
6 changes: 6 additions & 0 deletions tools/swift-api-digester/ModuleAnalyzerNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ class SDKNodeDeclType: public SDKNodeDecl {
// Check whether the type declaration is pulled from an external module so we
// can incorporate extensions in the interested module.
bool IsExternal;
bool IsEnumExhaustive;
bool HasMissingDesignatedInitializers;
bool InheritsConvenienceInitializers;
public:
Expand All @@ -550,6 +551,11 @@ class SDKNodeDeclType: public SDKNodeDecl {
return EnumRawTypeName;
}

bool isEnumExhaustive() const {
assert(isEnum());
return IsEnumExhaustive;
}

bool hasMissingDesignatedInitializers() const {
return HasMissingDesignatedInitializers;
};
Expand Down
4 changes: 3 additions & 1 deletion tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,9 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
if (!Ctx.checkingABI()) {
if (auto *Var = dyn_cast<SDKNodeDeclVar>(Right)) {
if (Var->getDeclKind() == DeclKind::EnumElement) {
Var->emitDiag(Var->getLoc(), diag::enum_case_added);
if (Var->getParent()->getAs<SDKNodeDeclType>()->isEnumExhaustive()) {
Var->emitDiag(Var->getLoc(), diag::enum_case_added);
}
}
}
}
Expand Down