Skip to content

Commit af7f127

Browse files
committed
SILGen: Fix crash when referencing dynamic Self from @convention(c) closure
Fixes <rdar://problem/57510056>.
1 parent 1071543 commit af7f127

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ ERROR(unsupported_c_function_pointer_conversion,none,
116116
ERROR(c_function_pointer_from_function_with_context,none,
117117
"a C function pointer cannot be formed from a "
118118
"%select{local function|closure}0 that captures "
119-
"%select{context|generic parameters|dynamic Self type|<<error>}1",
119+
"%select{context|generic parameters|dynamic Self type}1",
120120
(bool, unsigned))
121121

122122
ERROR(objc_selector_malformed,none,"the type ObjectiveC.Selector is malformed",

lib/SILGen/SILGenExpr.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,25 +1550,22 @@ ManagedValue emitCFunctionPointer(SILGenFunction &SGF,
15501550
// C function pointers cannot capture anything from their context.
15511551
auto captures = SGF.SGM.Types.getLoweredLocalCaptures(constant);
15521552

1553-
if (captures.hasGenericParamCaptures() ||
1553+
if (!captures.getCaptures().empty() ||
1554+
captures.hasGenericParamCaptures() ||
15541555
captures.hasDynamicSelfCapture() ||
1555-
captures.hasLocalCaptures() ||
15561556
captures.hasOpaqueValueCapture()) {
1557-
unsigned kind;
1558-
if (captures.hasLocalCaptures())
1559-
kind = 0;
1560-
else if (captures.hasGenericParamCaptures())
1557+
unsigned kind = 0;
1558+
if (captures.hasGenericParamCaptures())
15611559
kind = 1;
1562-
else if (captures.hasLocalCaptures())
1560+
else if (captures.hasDynamicSelfCapture())
15631561
kind = 2;
1564-
else
1565-
kind = 3;
15661562
SGF.SGM.diagnose(expr->getLoc(),
15671563
diag::c_function_pointer_from_function_with_context,
15681564
/*closure*/ constant.hasClosureExpr(),
15691565
kind);
15701566

1571-
return SGF.emitUndef(constantInfo.getSILType());
1567+
auto loweredTy = SGF.getLoweredType(conversionExpr->getType());
1568+
return SGF.emitUndef(loweredTy);
15721569
}
15731570

15741571
return convertCFunctionSignature(

test/SILGen/c_function_pointers.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,10 @@ func pointers_to_nested_local_functions_in_generics<T>(x: T) -> Int{
105105
func capture_list_no_captures(x: Int) {
106106
calls({ [x] in $0 }, 0) // expected-warning {{capture 'x' was never used}}
107107
}
108+
109+
class Selfless {
110+
func capture_dynamic_self() {
111+
calls_no_args { _ = Self.self; return 0 }
112+
// expected-error@-1 {{a C function pointer cannot be formed from a closure that captures dynamic Self type}}
113+
}
114+
}

0 commit comments

Comments
 (0)