Skip to content

Sema: Only diagnose explicit unavailable Clang enum elements #77254

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
11 changes: 9 additions & 2 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ using namespace swift;
static EnumElementDecl *
extractEnumElement(DeclContext *DC, SourceLoc UseLoc,
const VarDecl *constant) {
diagnoseDeclAvailability(constant, UseLoc, nullptr,
ExportContext::forFunctionBody(DC, UseLoc));
auto &ctx = DC->getASTContext();
auto availabilityContext = TypeChecker::availabilityAtLocation(UseLoc, DC);
if (auto unmetRequirement =
checkDeclarationAvailability(constant, DC, availabilityContext)) {
// Only diagnose explicit unavailability.
if (!unmetRequirement->getRequiredNewerAvailabilityRange(ctx))
diagnoseDeclAvailability(constant, UseLoc, nullptr,
ExportContext::forFunctionBody(DC, UseLoc));
}

const FuncDecl *getter = constant->getAccessor(AccessorKind::Get);
if (!getter)
Expand Down
33 changes: 33 additions & 0 deletions test/ClangImporter/availability_macosx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// REQUIRES: OS=macosx

import AppKit
import Foundation
import AvailabilityExtras

Expand All @@ -14,3 +15,35 @@ func test_unavailable_because_deprecated() {
func test_swift_unavailable_wins() {
unavailableWithOS() // expected-error {{'unavailableWithOS()' is unavailable in Swift}}
}

func bezierPathElementToInteger(_ e: NSBezierPathElement) -> Int {
switch e {
case .moveTo: return 1
case .lineTo: return 2
case .curveTo: return 3 // no error
case .cubicCurveTo: return 3
case .closePath: return 4
case .quadraticCurveTo: return 5
@unknown default: return -1
}
}

func integerToBezierPathElement(_ i: Int) -> NSBezierPathElement {
// expected-note@-1 2 {{add @available attribute to enclosing global function}}
switch i {
case 1:
return .moveTo
case 2:
return .lineTo
case 3:
return .curveTo // expected-error {{'curveTo' is only available in}}
// expected-note@-1 {{add 'if #available' version check}}
case 4:
return .closePath
case 5:
return .quadraticCurveTo // expected-error {{'quadraticCurveTo' is only available in}}
// expected-note@-1 {{add 'if #available' version check}}
default:
fatalError()
}
}
9 changes: 9 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/AppKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,12 @@ typedef NSPoint *NSPointPointer;
@interface NSDocument (URL)
@property (copy,nonnull) NSURL *URL;
@end

typedef NS_ENUM(NSUInteger, NSBezierPathElement) {
NSBezierPathElementMoveTo,
NSBezierPathElementLineTo,
NSBezierPathElementCubicCurveTo __attribute__((availability(macosx,introduced=52))),
NSBezierPathElementClosePath,
NSBezierPathElementQuadraticCurveTo __attribute__((availability(macosx,introduced=52))),
NSBezierPathElementCurveTo __attribute__((availability(macosx,introduced=51,deprecated=52,message="Use NSBezierPathElementCubicCurveTo"))) = NSBezierPathElementCubicCurveTo,
};