Skip to content

Commit 076ae9e

Browse files
committed
Sema: Reject enums marked with both @cdecl and @objc
1 parent 5c2efac commit 076ae9e

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
@@ -2056,6 +2056,9 @@ ERROR(cdecl_empty_name,none,
20562056
"%0 symbol name cannot be empty", (DeclAttribute))
20572057
ERROR(cdecl_throws,none,
20582058
"raising errors from %0 functions is not supported", (DeclAttribute))
2059+
ERROR(cdecl_incompatible_with_objc,none,
2060+
"cannot apply both '@cdecl' and '@objc' to %kindonly0",
2061+
(const Decl *))
20592062
ERROR(cdecl_feature_required,none,
20602063
"@cdecl requires '-enable-experimental-feature CDecl'",
20612064
())

lib/Sema/TypeCheckAttr.cpp

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

2387+
// Reject using both @cdecl and @objc on the same decl.
2388+
if (D->getAttrs().getAttribute<ObjCAttr>()) {
2389+
diagnose(attr->getLocation(), diag::cdecl_incompatible_with_objc, D);
2390+
}
2391+
2392+
// @cdecl needs to be enabled via a feature flag.
23872393
if (!attr->Underscored &&
23882394
!Ctx.LangOpts.hasFeature(Feature::CDecl)) {
2389-
Ctx.Diags.diagnose(attr->getLocation(), diag::cdecl_feature_required);
2395+
diagnose(attr->getLocation(), diag::cdecl_feature_required);
23902396
}
23912397
}
23922398

test/attr/attr_cdecl_official.swift

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

62+
@cdecl("CDeclAndObjC") // expected-error {{cannot apply both '@cdecl' and '@objc' to enum}}
63+
@objc
64+
enum CDeclAndObjC: Int { case A, B }
65+
66+
@cdecl("TwoCDecls") // expected-note {{attribute already specified here}}
67+
@_cdecl("TwoCDecls") // expected-error {{duplicate attribute}}
68+
func TwoCDecls() {}
69+
6270
class Foo {
6371
@cdecl("Foo_foo") // expected-error{{@cdecl can only be applied to global functions}}
6472
func foo(x: Int) -> Int { return x }

0 commit comments

Comments
 (0)