Skip to content

Commit a1b8687

Browse files
[c-interop] Reject @_silgen_name with @_extern
To avoid ambiguity at SIL level name since both tries to change the SIL level name.
1 parent 7a75f5d commit a1b8687

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1877,7 +1877,7 @@ ERROR(extern_not_at_top_level_func,none,
18771877
ERROR(extern_empty_c_name,none,
18781878
"expected non-empty C name in @_extern attribute", ())
18791879
ERROR(extern_only_non_other_attr,none,
1880-
"@_extern attribute cannot be applied to an '%0' declaration", (StringRef))
1880+
"@_extern attribute cannot be applied to an '@%0' declaration", (StringRef))
18811881
ERROR(c_func_variadic, none,
18821882
"cannot declare variadic argument %0 in %kind1",
18831883
(DeclName, const ValueDecl *))

lib/Sema/TypeCheckAttr.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,9 +2144,14 @@ void AttributeChecker::visitExternAttr(ExternAttr *attr) {
21442144
}
21452145
}
21462146

2147-
// @_cdecl cannot be mixed with @_extern since @_cdecl is for definitions
2148-
if (D->getAttrs().hasAttribute<CDeclAttr>())
2149-
diagnose(attr->getLocation(), diag::extern_only_non_other_attr, "@_cdecl");
2147+
for (auto *otherAttr : D->getAttrs()) {
2148+
// @_cdecl cannot be mixed with @_extern since @_cdecl is for definitions
2149+
// @_silgen_name cannot be mixed to avoid SIL-level name ambiguity
2150+
if (isa<CDeclAttr>(otherAttr) || isa<SILGenNameAttr>(otherAttr)) {
2151+
diagnose(attr->getLocation(), diag::extern_only_non_other_attr,
2152+
otherAttr->getAttrName());
2153+
}
2154+
}
21502155
}
21512156

21522157
void AttributeChecker::visitUsedAttr(UsedAttr *attr) {

test/attr/attr_extern.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,16 @@ func withAtCDecl_C()
119119
@_extern(wasm, module: "", name: "") // expected-error {{@_extern attribute cannot be applied to an '@_cdecl' declaration}}
120120
@_cdecl("another_c_name")
121121
func withAtCDecl_Wasm()
122+
123+
@_extern(c) // expected-error {{@_extern attribute cannot be applied to an '@_silgen_name' declaration}}
124+
@_silgen_name("another_sil_name")
125+
func withAtSILGenName_C()
126+
127+
@_extern(wasm, module: "", name: "") // expected-error {{@_extern attribute cannot be applied to an '@_silgen_name' declaration}}
128+
@_silgen_name("another_sil_name")
129+
func withAtSILGenName_Wasm()
130+
131+
@_extern(c) // expected-error {{@_extern attribute cannot be applied to an '@_silgen_name' declaration}} expected-error {{@_extern attribute cannot be applied to an '@_cdecl' declaration}}
132+
@_cdecl("another_c_name")
133+
@_silgen_name("another_sil_name")
134+
func withAtSILGenName_CDecl_C()

0 commit comments

Comments
 (0)