Skip to content

Commit 5cb8604

Browse files
committed
[ConstraintSystem] De-prioritize solutions with Swift -> C pointer conversions
Just like other implicit conversions - always prefer solutions with the lowest possible number of them.
1 parent f368e5a commit 5cb8604

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11385,6 +11385,10 @@ ConstraintSystem::SolutionKind
1138511385
ConstraintSystem::simplifyPointerToCPointerRestriction(
1138611386
Type type1, Type type2, TypeMatchOptions flags,
1138711387
ConstraintLocatorBuilder locator) {
11388+
// Make sure that solutions with implicit pointer conversions
11389+
// are always worse than the ones without them.
11390+
increaseScore(SK_ImplicitValueConversion);
11391+
1138811392
auto &ctx = getASTContext();
1138911393

1139011394
PointerTypeKind swiftPtrKind, cPtrKind;

test/Constraints/swift_to_c_pointer_conversions.swift.gyb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -import-objc-header %S/Inputs/overloaded_async_method.h %s -typecheck -verify
2-
31
// RUN: %empty-directory(%t)
42

53
// RUN: %gyb %s -o %t/swift_to_c_pointer_conversions.swift
@@ -236,3 +234,12 @@ func test_raw_ptr_optional_to_optional_conversion(
236234
% end
237235
% end
238236
}
237+
238+
// Check that implicit conversions don't make expressions ambiguous
239+
func test_overloaded_ref_is_not_ambiguous() {
240+
func overloaded_func() -> UnsafeRawPointer { fatalError() }
241+
func overloaded_func() -> UnsafePointer<Int8> { fatalError() }
242+
243+
const_char_ptr_func(overloaded_func()) // Ok
244+
const_opt_char_ptr_func(overloaded_func()) // Ok
245+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -import-objc-header %S/Inputs/c_pointer_conversions.h %s -emit-sil -verify | %FileCheck %s
2+
3+
// REQUIRES: objc_interop
4+
5+
6+
// Check that implicit conversions don't make expressions ambiguous
7+
func test_overloaded_ref_is_not_ambiguous() {
8+
func overloaded_func() -> UnsafeRawPointer { fatalError() }
9+
func overloaded_func() -> UnsafePointer<Int8> { fatalError() }
10+
11+
func overloaded_mutable_func() -> UnsafeMutableRawPointer { fatalError() }
12+
func overloaded_mutable_func() -> UnsafeMutablePointer<Int8> { fatalError() }
13+
14+
// CHECK: function_ref @$s34swift_to_c_pointer_conversions_sil36test_overloaded_ref_is_not_ambiguousyyF0G5_funcL0_SPys4Int8VGyF
15+
// CHECK-NEXT: apply
16+
const_char_ptr_func(overloaded_func()) // Ok
17+
// CHECK: function_ref @$s34swift_to_c_pointer_conversions_sil36test_overloaded_ref_is_not_ambiguousyyF0G5_funcL0_SPys4Int8VGyF
18+
// CHECK-NEXT: apply
19+
const_opt_char_ptr_func(overloaded_func()) // Ok
20+
21+
// CHECK: function_ref @$s34swift_to_c_pointer_conversions_sil36test_overloaded_ref_is_not_ambiguousyyF0G13_mutable_funcL0_Spys4Int8VGyF
22+
// CHECK-NEXT: apply
23+
char_ptr_func(overloaded_mutable_func()) // Ok
24+
25+
// CHECK: function_ref @$s34swift_to_c_pointer_conversions_sil36test_overloaded_ref_is_not_ambiguousyyF0G13_mutable_funcL0_Spys4Int8VGyF
26+
// CHECK-NEXT: apply
27+
const_char_ptr_func(overloaded_mutable_func()) // Ok (picks UnsafeMutablePointer using implicit conversion)
28+
29+
// CHECK: function_ref @$s34swift_to_c_pointer_conversions_sil36test_overloaded_ref_is_not_ambiguousyyF0G13_mutable_funcL0_Spys4Int8VGyF
30+
// CHECK-NEXT: apply
31+
opt_char_ptr_func(overloaded_mutable_func()) // Ok
32+
33+
// CHECK: function_ref @$s34swift_to_c_pointer_conversions_sil36test_overloaded_ref_is_not_ambiguousyyF0G13_mutable_funcL0_Spys4Int8VGyF
34+
// CHECK-NEXT: apply
35+
const_opt_char_ptr_func(overloaded_mutable_func()) // Ok
36+
}

0 commit comments

Comments
 (0)