Skip to content

[4.0] [Clang importer] Infer @_downgrade_exhaustivity_check for new-to-2017 enum elements in Swift 3 #10623

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
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
35 changes: 35 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7016,6 +7016,41 @@ void ClangImporter::Implementation::importAttributes(
PlatformAgnostic, /*Implicit=*/false);

MappedDecl->getAttrs().add(AvAttr);

// For enum cases introduced in the 2017 SDKs, add
// @_downgrade_exhaustivity_check in Swift 3.
if (C.LangOpts.isSwiftVersion3() && isa<EnumElementDecl>(MappedDecl)) {
bool downgradeExhaustivity = false;
switch (*platformK) {
case PlatformKind::OSX:
case PlatformKind::OSXApplicationExtension:
downgradeExhaustivity = (introduced.getMajor() == 10 &&
introduced.getMinor() &&
*introduced.getMinor() == 13);
break;

case PlatformKind::iOS:
case PlatformKind::iOSApplicationExtension:
case PlatformKind::tvOS:
case PlatformKind::tvOSApplicationExtension:
downgradeExhaustivity = (introduced.getMajor() == 11);
break;

case PlatformKind::watchOS:
case PlatformKind::watchOSApplicationExtension:
downgradeExhaustivity = (introduced.getMajor() == 4);
break;

case PlatformKind::none:
break;
}

if (downgradeExhaustivity) {
auto attr =
new (C) DowngradeExhaustivityCheckAttr(/*isImplicit=*/true);
MappedDecl->getAttrs().add(attr);
}
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/ClangImporter/Inputs/custom-modules/AvailabilityExtras.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import Foundation;

void unavail1(void) __attribute__((unavailable("first")));
void unavail1(void);
void unavail1(void);
Expand Down Expand Up @@ -84,3 +86,10 @@ struct NSSwiftUnavailableStruct {
} __attribute__((availability(swift, unavailable)));

void unavailableWithOS() __attribute__((availability(ios, deprecated=8.0))) __attribute__((availability(swift, unavailable))) __attribute__((availability(macosx, deprecated=10.10))) ;

typedef NS_ENUM(NSInteger, NSEnumAddedCasesIn2017) {
NSEnumAddedCasesIn2017ExistingCaseOne,
NSEnumAddedCasesIn2017ExistingCaseTwo,
NSEnumAddedCasesIn2017ExistingCaseThree,
NSEnumAddedCasesIn2017NewCaseOne __attribute__((availability(macosx,introduced=10.13))) __attribute__((availability(ios,introduced=11.0))) __attribute__((availability(tvos,introduced=11.0))) __attribute__((availability(watchos,introduced=4.0)))
};
18 changes: 18 additions & 0 deletions test/ClangImporter/availability_open_enums.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -I %S/Inputs/custom-modules -swift-version 4 %s

// REQUIRES: objc_interop

import Foundation
import AvailabilityExtras

func exhaustiveSwitch(e: NSEnumAddedCasesIn2017) {
switch e { // expected-error{{switch must be exhaustive}}
// expected-note@-1{{add missing case: '.newCaseOne'}}
case .existingCaseOne:
return
case .existingCaseTwo:
return
case .existingCaseThree:
return
}
}
18 changes: 18 additions & 0 deletions test/ClangImporter/availability_open_enums_swift3.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify -I %S/Inputs/custom-modules -swift-version 3 %s

// REQUIRES: objc_interop

import Foundation
import AvailabilityExtras

func exhaustiveSwitch(e: NSEnumAddedCasesIn2017) {
switch e { // expected-warning{{switch must be exhaustive}}
// expected-note@-1{{add missing case: '.newCaseOne'}}
case .existingCaseOne:
return
case .existingCaseTwo:
return
case .existingCaseThree:
return
}
}