Skip to content

Commit 5c2efac

Browse files
committed
AST: Begin accepting @cdecl on enums (but not @_cdecl)
1 parent 83c0667 commit 5c2efac

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

include/swift/AST/DeclAttr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ SIMPLE_DECL_ATTR(_show_in_interface, ShowInInterface,
365365
62)
366366

367367
DECL_ATTR(_cdecl, CDecl,
368-
OnFunc | OnAccessor,
368+
OnFunc | OnAccessor | OnEnum,
369369
LongAttribute | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | ForbiddenInABIAttr,
370370
63)
371371
DECL_ATTR_ALIAS(cdecl, CDecl)

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,12 @@ void AttributeChecker::visitCDeclAttr(CDeclAttr *attr) {
23782378
attr->Name);
23792379
}
23802380

2381+
// @_cdecl was never accepted on enums. Keep the previous diagnostic.
2382+
if (isa<EnumDecl>(D) && attr->Underscored) {
2383+
diagnose(attr->getLocation(), diag::attr_only_one_decl_kind,
2384+
attr, "func");
2385+
}
2386+
23812387
if (!attr->Underscored &&
23822388
!Ctx.LangOpts.hasFeature(Feature::CDecl)) {
23832389
Ctx.Diags.diagnose(attr->getLocation(), diag::cdecl_feature_required);

test/attr/attr_cdecl.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func emptyName(x: Int) -> Int { return x }
88
@_cdecl("noBody")
99
func noBody(x: Int) -> Int // expected-error{{expected '{' in body of function}}
1010

11-
@_cdecl("property") // expected-error{{may only be used on 'func' declarations}}
11+
@_cdecl("property") // expected-error{{'@_cdecl' attribute cannot be applied to this declaration}}
1212
var property: Int
1313

1414
var computed: Int {
@@ -24,6 +24,9 @@ enum SwiftEnum { case A, B }
2424
@objc enum CEnum: Int { case A, B }
2525
#endif
2626

27+
@_cdecl("enum") // expected-error {{@_cdecl may only be used on 'func' declarations}}
28+
enum UnderscoreCDeclEnum: Int { case A, B }
29+
2730
@_cdecl("swiftStruct")
2831
func swiftStruct(x: SwiftStruct) {} // expected-error{{cannot be represented}} expected-note{{Swift struct}}
2932

@@ -40,10 +43,10 @@ class Foo {
4043
@_cdecl("Foo_foo_2") // expected-error{{can only be applied to global functions}}
4144
static func foo(x: Int) -> Int { return x }
4245

43-
@_cdecl("Foo_init") // expected-error{{may only be used on 'func'}}
46+
@_cdecl("Foo_init") // expected-error{{'@_cdecl' attribute cannot be applied to this declaration}}
4447
init() {}
4548

46-
@_cdecl("Foo_deinit") // expected-error{{may only be used on 'func'}}
49+
@_cdecl("Foo_deinit") // expected-error{{'@_cdecl' attribute cannot be applied to this declaration}}
4750
deinit {}
4851
}
4952

test/attr/attr_cdecl_official.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@cdecl("noBody")
3030
func noBody(x: Int) -> Int // expected-error{{expected '{' in body of function}}
3131

32-
@cdecl("property") // expected-error{{may only be used on 'func' declarations}}
32+
@cdecl("property") // expected-error{{'@cdecl' attribute cannot be applied to this declaration}}
3333
var property: Int
3434

3535
var computed: Int {
@@ -66,10 +66,10 @@ class Foo {
6666
@cdecl("Foo_foo_2") // expected-error{{@cdecl can only be applied to global functions}}
6767
static func foo(x: Int) -> Int { return x }
6868

69-
@cdecl("Foo_init") // expected-error{{@cdecl may only be used on 'func'}}
69+
@cdecl("Foo_init") // expected-error{{'@cdecl' attribute cannot be applied to this declaration}}
7070
init() {}
7171

72-
@cdecl("Foo_deinit") // expected-error{{@cdecl may only be used on 'func'}}
72+
@cdecl("Foo_deinit") // expected-error{{'@cdecl' attribute cannot be applied to this declaration}}
7373
deinit {}
7474
}
7575

0 commit comments

Comments
 (0)