Skip to content

Commit 5df615d

Browse files
committed
Sema: Reject enums marked with both @cdecl and @objc
1 parent acdc951 commit 5df615d

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,9 @@ ERROR(cdecl_empty_name,none,
20782078
"%0 symbol name cannot be empty", (DeclAttribute))
20792079
ERROR(cdecl_throws,none,
20802080
"raising errors from %0 functions is not supported", (DeclAttribute))
2081+
ERROR(cdecl_incompatible_with_objc,none,
2082+
"cannot apply both '@cdecl' and '@objc' to %kindonly0",
2083+
(const Decl *))
20812084
ERROR(cdecl_feature_required,none,
20822085
"'@cdecl' requires '-enable-experimental-feature CDecl'",
20832086
())

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2404,9 +2404,15 @@ void AttributeChecker::visitCDeclAttr(CDeclAttr *attr) {
24042404
attr, "func");
24052405
}
24062406

2407+
// Reject using both @cdecl and @objc on the same decl.
2408+
if (D->getAttrs().getAttribute<ObjCAttr>()) {
2409+
diagnose(attr->getLocation(), diag::cdecl_incompatible_with_objc, D);
2410+
}
2411+
2412+
// @cdecl needs to be enabled via a feature flag.
24072413
if (!attr->Underscored &&
24082414
!Ctx.LangOpts.hasFeature(Feature::CDecl)) {
2409-
Ctx.Diags.diagnose(attr->getLocation(), diag::cdecl_feature_required);
2415+
diagnose(attr->getLocation(), diag::cdecl_feature_required);
24102416
}
24112417
}
24122418

test/attr/attr_cdecl_official.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ func swiftEnum(x: SwiftEnum) {} // expected-error{{cannot be represented}} expec
4444
@cdecl("cEnum")
4545
func cEnum(x: CEnum) {}
4646

47+
@cdecl("CDeclAndObjC") // expected-error {{cannot apply both '@cdecl' and '@objc' to enum}}
48+
@objc
49+
enum CDeclAndObjC: Int { case A, B }
50+
51+
@cdecl("TwoCDecls") // expected-note {{attribute already specified here}}
52+
@_cdecl("TwoCDecls") // expected-error {{duplicate attribute}}
53+
func TwoCDecls() {}
54+
4755
class Foo {
4856
@cdecl("Foo_foo") // expected-error{{@cdecl can only be applied to global functions}}
4957
func foo(x: Int) -> Int { return x }

0 commit comments

Comments
 (0)