Skip to content

Commit 7feb60d

Browse files
committed
SILGen: Handle C functions imported as labeled unit constructors.
1 parent c349f35 commit 7feb60d

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

lib/SIL/AbstractionPattern.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#define DEBUG_TYPE "libsil"
1919
#include "swift/SIL/TypeLowering.h"
20+
#include "swift/AST/ASTContext.h"
2021
#include "swift/AST/Decl.h"
2122
#include "swift/AST/ForeignErrorConvention.h"
2223
#include "swift/Basic/Fallthrough.h"
@@ -261,10 +262,16 @@ AbstractionPattern::getTupleElementType(unsigned index) const {
261262
return AbstractionPattern::getOpaque();
262263
return AbstractionPattern(getGenericSignature(),
263264
cast<TupleType>(getType()).getElementType(index));
264-
case Kind::ClangFunctionParamTupleType:
265+
case Kind::ClangFunctionParamTupleType: {
266+
// Handle the (label: ()) param used by functions imported as labeled
267+
// nullary initializers.
268+
if (isVoidLike(getType()))
269+
return AbstractionPattern(getType()->getASTContext().TheEmptyTupleType);
270+
265271
return AbstractionPattern(getGenericSignature(),
266272
cast<TupleType>(getType()).getElementType(index),
267273
getClangFunctionParameterType(getClangType(), index));
274+
}
268275

269276
case Kind::ObjCMethodFormalParamTupleType: {
270277
auto swiftEltType = cast<TupleType>(getType()).getElementType(index);

test/IDE/Inputs/custom-modules/ImportAsMemberC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void CCRefrigeratorSetPowerSupply(CCRefrigeratorRef fridge,
2727
extern const CCPowerSupplyRef kCCPowerSupplySemiModular
2828
__attribute__((swift_name("CCPowerSupplyRef.semiModular")));
2929

30+
_Nonnull CCPowerSupplyRef CCPowerSupplyCreateDangerous(void)
31+
__attribute__((swift_name("CCPowerSupply.init(dangerous:)")));
3032
#pragma clang arc_cf_code_audited end
3133

3234
extern const double kCCPowerSupplyDefaultPower

test/IDE/import_as_member_cf.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// PRINTC: extension CCPowerSupply {
88
// PRINTC-NEXT: /*not inherited*/ init(watts watts: Double)
99
// PRINTC-NEXT: class let semiModular: CCPowerSupply!
10+
// PRINTC-NEXT: /*not inherited*/ init(dangerous dangerous: ())
1011
// PRINTC-NEXT: class let defaultPower: Double
1112
// PRINTC-NEXT: class let AC: CCPowerSupply
1213
// PRINTC-NEXT: class let DC: CCPowerSupply?

test/SILGen/cf_members.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import ImportAsMember
66

77
func makeMetatype() -> Struct1.Type { return Struct1.self }
88

9+
// CHECK-LABEL: sil @_TF10cf_members17importAsUnaryInitFT_T_
10+
public func importAsUnaryInit() {
11+
// CHECK: function_ref @CCPowerSupplyCreateDangerous : $@convention(c) () -> @owned CCPowerSupply
12+
var a = CCPowerSupply(dangerous: ())
13+
let f: () -> CCPowerSupply = CCPowerSupply.init(dangerous:)
14+
a = f()
15+
}
16+
917
// CHECK-LABEL: sil @_TF10cf_members3foo
1018
public func foo(_ x: Double) {
1119
// CHECK: bb0([[X:%.*]] : $Double):

0 commit comments

Comments
 (0)