Skip to content

Commit acdc951

Browse files
committed
AST: Begin accepting @cdecl on enums (but not @_cdecl)
1 parent 96a2833 commit acdc951

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
@@ -2398,6 +2398,12 @@ void AttributeChecker::visitCDeclAttr(CDeclAttr *attr) {
23982398
attr->Name);
23992399
}
24002400

2401+
// @_cdecl was never accepted on enums. Keep the previous diagnostic.
2402+
if (isa<EnumDecl>(D) && attr->Underscored) {
2403+
diagnose(attr->getLocation(), diag::attr_only_one_decl_kind,
2404+
attr, "func");
2405+
}
2406+
24012407
if (!attr->Underscored &&
24022408
!Ctx.LangOpts.hasFeature(Feature::CDecl)) {
24032409
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
@@ -14,7 +14,7 @@ func emptyName(x: Int) -> Int { return x }
1414
@cdecl("noBody")
1515
func noBody(x: Int) -> Int // expected-error{{expected '{' in body of function}}
1616

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

2020
var computed: Int {
@@ -51,10 +51,10 @@ class Foo {
5151
@cdecl("Foo_foo_2") // expected-error{{@cdecl can only be applied to global functions}}
5252
static func foo(x: Int) -> Int { return x }
5353

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

57-
@cdecl("Foo_deinit") // expected-error{{@cdecl may only be used on 'func'}}
57+
@cdecl("Foo_deinit") // expected-error{{'@cdecl' attribute cannot be applied to this declaration}}
5858
deinit {}
5959
}
6060

0 commit comments

Comments
 (0)