Skip to content

Commit 5978df0

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 42c3bf2 commit 5978df0

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
@@ -2135,16 +2135,18 @@ void AttributeChecker::visitExternAttr(ExternAttr *attr) {
21352135
if (cName->empty())
21362136
diagnose(attr->getLocation(), diag::extern_empty_c_name);
21372137
}
2138+
2139+
// Ensure the decl has C compatible interface. Otherwise it produces diagnostics.
2140+
if (!isCCompatibleFuncDecl(FD)) {
2141+
attr->setInvalid();
2142+
// Mark the decl itself invalid not to require body even with invalid ExternAttr.
2143+
FD->setInvalid();
2144+
}
21382145
}
21392146

21402147
// @_cdecl cannot be mixed with @_extern since @_cdecl is for definitions
21412148
if (D->getAttrs().hasAttribute<CDeclAttr>())
21422149
diagnose(attr->getLocation(), diag::extern_only_non_other_attr, "@_cdecl");
2143-
2144-
if (!isCCompatibleFuncDecl(FD)) {
2145-
attr->setInvalid();
2146-
FD->setInvalid();
2147-
}
21482150
}
21492151

21502152
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)