Skip to content

Commit 581b13f

Browse files
committed
Ignore "noescape" bit for all @convention(c) function pointers
1 parent 23e1773 commit 581b13f

File tree

10 files changed

+32
-22
lines changed

10 files changed

+32
-22
lines changed

include/swift/AST/ExtInfo.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,6 @@ class ASTExtInfoBuilder {
358358

359359
constexpr Representation getRepresentation() const {
360360
unsigned rawRep = bits & RepresentationMask;
361-
assert(rawRep <= unsigned(Representation::Last) &&
362-
"unexpected SIL representation");
363361
return Representation(rawRep);
364362
}
365363

include/swift/AST/Types.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,14 @@ class AnyFunctionType : public TypeBase {
30883088
static_assert(
30893089
ASTExtInfoBuilder::NumMaskBits == NumAFTExtInfoBits,
30903090
"ExtInfo and AnyFunctionTypeBitfields must agree on bit size");
3091+
3092+
// Canonicalize all thin functions to be escaping (to keep compatibility
3093+
// with generic parameters).
3094+
Representation rep = Info.getValue().getRepresentation();
3095+
if (rep == FunctionTypeRepresentation::CFunctionPointer) {
3096+
auto extInfoBuilder = Info->intoBuilder().withNoEscape(false);
3097+
Bits.AnyFunctionType.ExtInfoBits = extInfoBuilder.bits;
3098+
}
30913099
} else {
30923100
Bits.AnyFunctionType.HasExtInfo = false;
30933101
Bits.AnyFunctionType.HasClangTypeInfo = false;

lib/AST/ASTContext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/DiagnosticsSema.h"
2525
#include "swift/AST/DistributedDecl.h"
2626
#include "swift/AST/ExistentialLayout.h"
27+
#include "swift/AST/ExtInfo.h"
2728
#include "swift/AST/FileUnit.h"
2829
#include "swift/AST/ForeignAsyncConvention.h"
2930
#include "swift/AST/ForeignErrorConvention.h"
@@ -3961,6 +3962,13 @@ SILFunctionType::SILFunctionType(
39613962
if (!ext.getClangTypeInfo().empty())
39623963
*getTrailingObjects<ClangTypeInfo>() = ext.getClangTypeInfo();
39633964

3965+
// Canonicalize all thin functions to be escaping (to keep compatibility
3966+
// with generic parameters)
3967+
if (ext.getRepresentation() == SILFunctionTypeRepresentation::CFunctionPointer) {
3968+
auto extInfoBuilder = ext.intoBuilder().withNoEscape(false);
3969+
Bits.SILFunctionType.ExtInfoBits = extInfoBuilder.bits;
3970+
}
3971+
39643972
#ifndef NDEBUG
39653973
if (ext.getRepresentation() == Representation::WitnessMethod)
39663974
assert(!WitnessMethodConformance.isInvalid() &&

test/PrintAsObjC/blocks.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
101101
) {
102102
}
103103

104-
// CHECK-NEXT: - (void (* _Nonnull)(SWIFT_NOESCAPE NSInteger (* _Nonnull)(NSInteger, NSInteger)))returnsFunctionPointerThatTakesFunctionPointer SWIFT_WARN_UNUSED_RESULT;
104+
// CHECK-NEXT: - (void (* _Nonnull)(NSInteger (* _Nonnull)(NSInteger, NSInteger)))returnsFunctionPointerThatTakesFunctionPointer SWIFT_WARN_UNUSED_RESULT;
105105
@objc func returnsFunctionPointerThatTakesFunctionPointer() ->
106106
@convention(c) (_ comparator: @convention(c) (_ x: Int, _ y: Int) -> Int) -> Void {
107107
fatalError()

test/PrintAsObjC/cdecl-imports.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Foundation
1818
public func fwdDeclaresBee() -> Bee { fatalError() }
1919

2020
// CHECK: @class Hive;
21-
// CHECK-LABEL: void fwd_declares_hive(SWIFT_NOESCAPE Hive * _Nonnull (* _Nonnull bzzz)(Bee * _Nonnull));
21+
// CHECK-LABEL: void fwd_declares_hive(Hive * _Nonnull (* _Nonnull bzzz)(Bee * _Nonnull));
2222

2323
@_cdecl("fwd_declares_hive")
2424
public func fwdDeclaresHive(bzzz: @convention(c) (Bee) -> Hive) { fatalError() }

test/PrintAsObjC/cdecl.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public func block_recurring_nightmare(x: @escaping @convention(block) (@conventi
2626
@_cdecl("foo_bar")
2727
func foo(x: Int, bar y: Int) {}
2828

29-
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_nightmare(SWIFT_NOESCAPE float (* _Nonnull x)(NSInteger)))(char) SWIFT_WARN_UNUSED_RESULT;
29+
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_nightmare(float (* _Nonnull x)(NSInteger)))(char) SWIFT_WARN_UNUSED_RESULT;
3030
@_cdecl("function_pointer_nightmare")
3131
func function_pointer_nightmare(x: @convention(c) (Int) -> Float)
3232
-> @convention(c) (CChar) -> Double { return { _ in 0 } }
3333

34-
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_recurring_nightmare(float (* _Nonnull x)(SWIFT_NOESCAPE NSInteger (* _Nonnull)(double))))(SWIFT_NOESCAPE char (* _Nonnull)(unsigned char)) SWIFT_WARN_UNUSED_RESULT;
34+
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_recurring_nightmare(float (* _Nonnull x)(NSInteger (* _Nonnull)(double))))(char (* _Nonnull)(unsigned char)) SWIFT_WARN_UNUSED_RESULT;
3535
@_cdecl("function_pointer_recurring_nightmare")
3636
public func function_pointer_recurring_nightmare(x: @escaping @convention(c) (@convention(c) (Double) -> Int) -> Float)
3737
-> @convention(c) (@convention(c) (CUnsignedChar) -> CChar) -> Double {

test/SIL/clang-function-types-nonwindows.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import ctypes
77

88
public func f(g: @convention(c, cType: "void (*)(size_t)") (Int) -> ()) { g(0) }
99

10-
// CHECK: sil @$s4main1f1gyySiXzC9_ZTSPFvmE_tF : $@convention(thin) (@convention(c, cType: "void (*)(unsigned long)") @noescape (Int) -> ()) -> () {
11-
// CHECK: bb0(%0 : $@convention(c, cType: "void (*)(unsigned long)") @noescape (Int) -> ()):
12-
// CHECK: debug_value %0 : $@convention(c, cType: "void (*)(unsigned long)") @noescape (Int) -> (), let, name "g", argno 1 // id: %1
10+
// CHECK: sil @$s4main1f1gyySiXzC9_ZTSPFvmE_tF : $@convention(thin) (@convention(c, cType: "void (*)(unsigned long)") (Int) -> ()) -> () {
11+
// CHECK: bb0(%0 : $@convention(c, cType: "void (*)(unsigned long)") (Int) -> ()):
12+
// CHECK: debug_value %0 : $@convention(c, cType: "void (*)(unsigned long)") (Int) -> (), let, name "g", argno 1 // id: %1

test/SILGen/c_function_pointers.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func calls(_ arg: @convention(c) (Int) -> Int, _ x: Int) -> Int {
4040
return arg(x)
4141
}
4242
// CHECK-LABEL: sil hidden [ossa] @$s19c_function_pointers5callsyS3iXC_SitF
43-
// CHECK: bb0(%0 : $@convention(c) @noescape (Int) -> Int, %1 : $Int):
43+
// CHECK: bb0(%0 : $@convention(c) (Int) -> Int, %1 : $Int):
4444
// CHECK: [[RESULT:%.*]] = apply %0(%1)
4545
// CHECK: return [[RESULT]]
4646

@@ -60,24 +60,20 @@ func pointers_to_swift_functions(_ x: Int) {
6060
func local(_ y: Int) -> Int { return y }
6161

6262
// CHECK: [[GLOBAL_C:%.*]] = function_ref @$s19c_function_pointers6globalyS2iFTo
63-
// CHECK: [[CVT:%.*]] = convert_function [[GLOBAL_C]]
64-
// CHECK: apply {{.*}}([[CVT]], [[X]])
63+
// CHECK: apply {{.*}}([[GLOBAL_C]], [[X]])
6564
calls(global, x)
6665

6766
// CHECK: [[LOCAL_C:%.*]] = function_ref @$s19c_function_pointers0B19_to_swift_functionsyySiF5localL_yS2iFTo
68-
// CHECK: [[CVT:%.*]] = convert_function [[LOCAL_C]]
69-
// CHECK: apply {{.*}}([[CVT]], [[X]])
67+
// CHECK: apply {{.*}}([[LOCAL_C]], [[X]])
7068
calls(local, x)
7169

72-
// CHECK: [[CLOSURE_C:%.*]] = function_ref @$s19c_function_pointers0B19_to_swift_functionsyySiFS2iXEfU_To
73-
// CHECK: [[CVT:%.*]] = convert_function [[CLOSURE_C]]
74-
// CHECK: apply {{.*}}([[CVT]], [[X]])
70+
// CHECK: [[CLOSURE_C:%.*]] = function_ref @$s19c_function_pointers0B19_to_swift_functionsyySiFS2icfU_To
71+
// CHECK: apply {{.*}}([[CLOSURE_C]], [[X]])
7572
calls({ $0 + 1 }, x)
7673

7774
calls_no_args(no_args)
7875
// CHECK: [[NO_ARGS_C:%.*]] = function_ref @$s19c_function_pointers7no_argsSiyFTo
79-
// CHECK: [[CVT:%.*]] = convert_function [[NO_ARGS_C]]
80-
// CHECK: apply {{.*}}([[CVT]])
76+
// CHECK: apply {{.*}}([[NO_ARGS_C]])
8177
}
8278

8379
func unsupported(_ a: Any) -> Int { return 0 }

test/SILGen/objc_blocks_bridging.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import Foundation
6666
}
6767

6868
// CHECK-LABEL: sil hidden [thunk] [ossa] @$s20objc_blocks_bridging3FooC16cFunctionPointer{{[_0-9a-zA-Z]*}}FTo
69-
// CHECK: bb0([[F:%.*]] : $@convention(c) @noescape (Int) -> Int, [[X:%.*]] : $Int, [[SELF:%.*]] : @unowned $Foo):
69+
// CHECK: bb0([[F:%.*]] : $@convention(c) (Int) -> Int, [[X:%.*]] : $Int, [[SELF:%.*]] : @unowned $Foo):
7070
// CHECK: [[SELF_COPY:%.*]] = copy_value [[SELF]]
7171
// CHECK: [[BORROWED_SELF_COPY:%.*]] = begin_borrow [[SELF_COPY]]
7272
// CHECK: [[NATIVE:%.*]] = function_ref @$s20objc_blocks_bridging3FooC16cFunctionPointer{{[_0-9a-zA-Z]*}}F

test/SILGen/without_actually_escaping.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ func withoutActuallyEscapingConflict() {
102102
}
103103

104104
// CHECK-LABEL: sil [ossa] @$s25without_actually_escaping0A25ActuallyEscapingCFunction8functionyyyXC_tF
105-
// CHECK: bb0([[ARG:%.*]] : $@convention(c) @noescape () -> ()):
106-
// CHECK: [[E:%.*]] = convert_function [[ARG]] : $@convention(c) @noescape () -> () to [without_actually_escaping] $@convention(c) () -> ()
105+
// CHECK: bb0([[ARG:%.*]] : $@convention(c) () -> ()):
106+
// CHECK: [[E:%.*]] = convert_function [[ARG]] : $@convention(c) () -> () to [without_actually_escaping] $@convention(c) () -> ()
107107
// CHECK: [[F:%.*]] = function_ref @$s25without_actually_escaping0A25ActuallyEscapingCFunction8functionyyyXC_tFyyyXCXEfU_ : $@convention(thin) (@convention(c) () -> ()) -> ()
108108
// CHECK: apply [[F]]([[E]]) : $@convention(thin) (@convention(c) () -> ()) -> ()
109109
public func withoutActuallyEscapingCFunction(function: (@convention(c) () -> Void)) {

0 commit comments

Comments
 (0)