Skip to content

Commit bdbb7dc

Browse files
[c-interop] Check interface C compatibility only for @_extern(c)
`@_extern(wasm)` has no limitation on the function interface, so the check should be applied only for `@_extern(c)`.
1 parent 4a023b4 commit bdbb7dc

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,16 +2132,18 @@ void AttributeChecker::visitExternAttr(ExternAttr *attr) {
21322132
if (cName->empty())
21332133
diagnose(attr->getLocation(), diag::extern_empty_c_name);
21342134
}
2135+
2136+
// Ensure the decl has C compatible interface. Otherwise it produces diagnostics.
2137+
if (!isCCompatibleFuncDecl(FD)) {
2138+
attr->setInvalid();
2139+
// Mark the decl itself invalid not to require body even with invalid ExternAttr.
2140+
FD->setInvalid();
2141+
}
21352142
}
21362143

21372144
// @_cdecl cannot be mixed with @_extern since @_cdecl is for definitions
21382145
if (D->getAttrs().hasAttribute<CDeclAttr>())
21392146
diagnose(attr->getLocation(), diag::extern_only_non_other_attr, "@_cdecl");
2140-
2141-
if (!isCCompatibleFuncDecl(FD)) {
2142-
attr->setInvalid();
2143-
FD->setInvalid();
2144-
}
21452147
}
21462148

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

test/attr/attr_extern.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,22 @@ func mixedAttrs_C_Wasm()
8686
class NonC {}
8787
@_extern(c)
8888
func nonCReturnTypes() -> NonC // expected-error {{'NonC' cannot be represented in C}}
89+
// @_extern(wasm) have no interface limitation
90+
@_extern(wasm, module: "non-c", name: "return_wasm")
91+
func nonCReturnTypesWasm() -> NonC
92+
@_extern(c)
93+
@_extern(wasm, module: "non-c", name: "return_mixed")
94+
func nonCReturnTypesMixed() -> NonC // expected-error {{'NonC' cannot be represented in C}}
95+
8996
@_extern(c)
9097
func nonCParamTypes(_: Int, _: NonC) // expected-error {{'NonC' cannot be represented in C}}
98+
@_extern(wasm, module: "non-c", name: "param_wasm")
99+
func nonCParamTypesWasm(_: Int, _: NonC)
100+
101+
@_extern(c)
102+
@_extern(wasm, module: "non-c", name: "param_mixed")
103+
func nonCParamTypesMixed(_: Int, _: NonC) // expected-error {{'NonC' cannot be represented in C}}
104+
91105
92106
@_extern(c)
93107
func asyncFuncC() async // expected-error {{async functions cannot be represented in C}}

0 commit comments

Comments
 (0)