Skip to content

Commit ae89cf0

Browse files
authored
Merge pull request #14499 from DougGregor/solver-favor-more-specialized
[Constraint solver] Favor more-specialized overload among two generics.
2 parents 6bca669 + 774bee2 commit ae89cf0

File tree

22 files changed

+107
-602
lines changed

22 files changed

+107
-602
lines changed

CMakeLists.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ set_property(GLOBAL PROPERTY JOB_POOLS local_jobs=${localhost_logical_cores})
1111
# Put linking in that category
1212
set_property(GLOBAL PROPERTY JOB_POOL_LINK local_jobs)
1313

14-
ENABLE_LANGUAGE(C)
15-
1614
# First include general CMake utilities.
1715
include(SwiftUtils)
1816
include(CheckSymbolExists)
@@ -750,10 +748,10 @@ if(swift_build_android AND NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
750748

751749
set(SWIFT_ANDROID_PREBUILT_PATH
752750
"${SWIFT_ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${_swift_android_prebuilt_suffix}")
753-
751+
754752
# Resolve the correct linker based on the file name of CMAKE_LINKER (being 'ld' or 'ld.gold' the options)
755753
get_filename_component(SWIFT_ANDROID_LINKER_NAME "${CMAKE_LINKER}" NAME)
756-
set(SWIFT_SDK_ANDROID_ARCH_armv7_LINKER
754+
set(SWIFT_SDK_ANDROID_ARCH_armv7_LINKER
757755
"${SWIFT_ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${_swift_android_prebuilt_suffix}/bin/arm-linux-androideabi-${SWIFT_ANDROID_LINKER_NAME}")
758756

759757
configure_sdk_unix(ANDROID "Android" "android" "android" "armv7" "armv7-none-linux-androideabi" "${SWIFT_ANDROID_SDK_PATH}")
@@ -881,7 +879,7 @@ endif()
881879
###############
882880
#
883881
# We have to include stdlib/ before tools/.
884-
# Do not move add_subdirectory(stdlib) after add_subdirectory(tools)!
882+
# Do not move add_subdirectory(stdlib) after add_subdirectory(tools)!
885883
#
886884
# We must include stdlib/ before tools/ because stdlib/CMakeLists.txt
887885
# declares the swift-stdlib-* set of targets. These targets will then
@@ -900,7 +898,7 @@ add_subdirectory(stdlib)
900898
if(SWIFT_INCLUDE_TOOLS)
901899
add_subdirectory(include)
902900
add_subdirectory(lib)
903-
901+
904902
# Always include this after including stdlib/!
905903
# Refer to the large comment above the add_subdirectory(stdlib) call.
906904
# https://bugs.swift.org/browse/SR-5975

cmake/modules/AddSwift.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ function(_add_variant_c_compile_flags)
255255
else()
256256
list(APPEND result "-DNDEBUG")
257257
endif()
258-
258+
259259
if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
260260
list(APPEND result "-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
261261
endif()
@@ -320,7 +320,7 @@ function(_add_variant_swift_compile_flags
320320
if (SWIFT_ENABLE_GUARANTEED_NORMAL_ARGUMENTS)
321321
list(APPEND result "-Xfrontend" "-enable-guaranteed-normal-arguments")
322322
endif()
323-
323+
324324
if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
325325
list(APPEND result "-D" "SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
326326
endif()
@@ -1605,7 +1605,7 @@ function(add_swift_library name)
16051605
if(SWIFTLIB_IS_SDK_OVERLAY)
16061606
list(APPEND swiftlib_swift_compile_flags_all "-Fsystem" "${SWIFT_SDK_${sdk}_PATH}/System/Library/PrivateFrameworks/")
16071607
endif()
1608-
1608+
16091609
if("${sdk}" STREQUAL "IOS_SIMULATOR")
16101610
if("${name}" STREQUAL "swiftMediaPlayer")
16111611
message("DISABLING AUTOLINK FOR swiftMediaPlayer")
@@ -1854,7 +1854,7 @@ function(add_swift_library name)
18541854
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
18551855
RUNTIME DESTINATION bin)
18561856
swift_is_installing_component(dev is_installing)
1857-
1857+
18581858
if(NOT is_installing)
18591859
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${name})
18601860
else()
@@ -2177,7 +2177,7 @@ function(add_swift_host_tool executable)
21772177

21782178
swift_is_installing_component(${ADDSWIFTHOSTTOOL_SWIFT_COMPONENT}
21792179
is_installing)
2180-
2180+
21812181
if(NOT is_installing)
21822182
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${executable})
21832183
else()

lib/Sema/ConstraintSystem.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,29 @@ void ConstraintSystem::addOverloadSet(Type boundType,
13891389
return;
13901390
}
13911391

1392+
// Performance hack: if there are two generic overloads, and one is
1393+
// more specialized than the other, prefer the more-specialized one.
1394+
if (!favoredChoice && choices.size() == 2 &&
1395+
choices[0].isDecl() && choices[1].isDecl() &&
1396+
isa<AbstractFunctionDecl>(choices[0].getDecl()) &&
1397+
cast<AbstractFunctionDecl>(choices[0].getDecl())->isGeneric() &&
1398+
isa<AbstractFunctionDecl>(choices[1].getDecl()) &&
1399+
cast<AbstractFunctionDecl>(choices[1].getDecl())->isGeneric()) {
1400+
switch (TC.compareDeclarations(DC, choices[0].getDecl(),
1401+
choices[1].getDecl())) {
1402+
case Comparison::Better:
1403+
favoredChoice = const_cast<OverloadChoice *>(&choices[0]);
1404+
break;
1405+
1406+
case Comparison::Worse:
1407+
favoredChoice = const_cast<OverloadChoice *>(&choices[1]);
1408+
break;
1409+
1410+
case Comparison::Unordered:
1411+
break;
1412+
}
1413+
}
1414+
13921415
SmallVector<Constraint *, 4> overloads;
13931416

13941417
// As we do for other favored constraints, if a favored overload has been

test/Compatibility/enum_cases.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ enum G_E<T> {
1919
let arr: [String] = []
2020
let _ = arr.map(E.foo) // Ok
2121
let _ = arr.map(E.bar) // Ok
22-
let _ = arr.map(E.two) // expected-error {{cannot convert value of type '(Int, Int) -> E' to expected argument type '(String) -> _'}}
23-
let _ = arr.map(E.tuple) // expected-error {{cannot convert value of type '((x: Int, y: Int)) -> E' to expected argument type '(String) -> _'}}
22+
let _ = arr.map(E.two) // expected-error {{cannot invoke 'map' with an argument list of type '((Int, Int) -> E)'}}
23+
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
24+
let _ = arr.map(E.tuple) // expected-error {{cannot invoke 'map' with an argument list of type '(((x: Int, y: Int)) -> E)'}}
25+
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
2426

2527
let _ = arr.map(G_E<String>.foo) // Ok
2628
let _ = arr.map(G_E<String>.bar) // Ok
27-
let _ = arr.map(G_E<String>.two) // expected-error {{cannot convert value of type '(String, String) -> G_E<String>' to expected argument type '(String) -> _'}}
28-
let _ = arr.map(G_E<Int>.tuple) // expected-error {{cannot convert value of type '((x: Int, y: Int)) -> G_E<Int>' to expected argument type '(String) -> _'}}
29+
let _ = arr.map(G_E<String>.two) // expected-error {{cannot invoke 'map' with an argument list of type '((String, String) -> G_E<String>)'}}
30+
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
31+
32+
let _ = arr.map(G_E<Int>.tuple) // expected-error {{cannot invoke 'map' with an argument list of type '(((x: Int, y: Int)) -> G_E<Int>)'}}
33+
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
2934

3035
let _ = E.foo("hello") // expected-error {{missing argument label 'bar:' in call}}
3136
let _ = E.bar("hello") // Ok

test/Constraints/enum_cases.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ enum G_E<T> {
1919
let arr: [String] = []
2020
let _ = arr.map(E.foo) // Ok
2121
let _ = arr.map(E.bar) // Ok
22-
let _ = arr.map(E.two) // expected-error {{cannot convert value of type '(Int, Int) -> E' to expected argument type '(String) -> _'}}
23-
let _ = arr.map(E.tuple) // expected-error {{cannot convert value of type '((x: Int, y: Int)) -> E' to expected argument type '(String) -> _'}}
22+
let _ = arr.map(E.two) // expected-error {{cannot invoke 'map' with an argument list of type '((Int, Int) -> E)'}}
23+
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
24+
25+
let _ = arr.map(E.tuple) // expected-error {{cannot invoke 'map' with an argument list of type '(((x: Int, y: Int)) -> E)'}}
26+
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
2427

2528
let _ = arr.map(G_E<String>.foo) // Ok
2629
let _ = arr.map(G_E<String>.bar) // Ok
27-
let _ = arr.map(G_E<String>.two) // expected-error {{cannot convert value of type '(String, String) -> G_E<String>' to expected argument type '(String) -> _'}}
28-
let _ = arr.map(G_E<Int>.tuple) // expected-error {{cannot convert value of type '((x: Int, y: Int)) -> G_E<Int>' to expected argument type '(String) -> _'}}
30+
let _ = arr.map(G_E<String>.two) // expected-error {{cannot invoke 'map' with an argument list of type '((String, String) -> G_E<String>)'}}
31+
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
32+
let _ = arr.map(G_E<Int>.tuple) // expected-error {{cannot invoke 'map' with an argument list of type '(((x: Int, y: Int)) -> G_E<Int>)'}}
33+
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
2934

3035
let _ = E.foo("hello") // expected-error {{missing argument label 'bar:' in call}}
3136
let _ = E.bar("hello") // Ok

test/Constraints/optional.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,22 @@ func test8(_ x : AnyObject?) {
8181

8282

8383
// Partial ordering with optionals
84-
func test9_helper<T>(_ x: T) -> Int { }
85-
func test9_helper<T>(_ x: T?) -> Double { }
84+
func test9_helper<T: P>(_ x: T) -> Int { }
85+
func test9_helper<T: P>(_ x: T?) -> Double { }
86+
87+
func test9_helper2<T>(_ x: T) -> Int { }
88+
func test9_helper2<T>(_ x: T?) -> Double { }
8689

8790
func test9(_ i: Int, io: Int?) {
8891
let result = test9_helper(i)
8992
var _: Int = result
9093
let result2 = test9_helper(io)
9194
let _: Double = result2
95+
96+
let result3 = test9_helper2(i)
97+
var _: Double = result3
98+
let result4 = test9_helper2(io)
99+
let _: Double = result4
92100
}
93101

94102
protocol P { }

test/SwiftSyntax/DeserializeFile.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import StdlibUnittest
77
import Foundation
88
import SwiftSyntax
9-
import SwiftLang
109

1110
func getInput(_ file: String) -> URL {
1211
var result = URL(fileURLWithPath: #file)
@@ -20,7 +19,7 @@ var DecodeTests = TestSuite("DecodeSyntax")
2019

2120
DecodeTests.test("Basic") {
2221
expectDoesNotThrow({
23-
let content = try SwiftLang.parse(getInput("visitor.swift"))
22+
let content = try SourceFileSyntax.encodeSourceFileSyntax(getInput("visitor.swift"))
2423
let source = try String(contentsOf: getInput("visitor.swift"))
2524
let parsed = try SourceFileSyntax.decodeSourceFileSyntax(content)
2625
expectEqual("\(parsed)", source)

test/SwiftSyntax/ParseFile.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// REQUIRES: executable_test
33
// REQUIRES: OS=macosx
44
// REQUIRES: objc_interop
5+
// REQUIRES: rdar36740859
56

67
import Foundation
78
import StdlibUnittest
89
import SwiftSyntax
9-
import SwiftLang
1010

1111
var ParseFile = TestSuite("ParseFile")
1212

@@ -31,8 +31,7 @@ ParseFile.test("ParseSingleFile") {
3131
let currentFile = URL(fileURLWithPath: #file)
3232
expectDoesNotThrow({
3333
let currentFileContents = try String(contentsOf: currentFile)
34-
let parsed = try SourceFileSyntax.decodeSourceFileSyntax(try
35-
SwiftLang.parse(currentFile))
34+
let parsed = try SourceFileSyntax.parse(currentFile)
3635
expectEqual("\(parsed)", currentFileContents)
3736
})
3837
}

test/SwiftSyntax/VisitorTest.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// REQUIRES: OS=macosx
44
// REQUIRES: objc_interop
55

6+
// FIXME: This test fails occassionally in CI with invalid json.
7+
// REQUIRES: disabled
8+
69
import StdlibUnittest
710
import Foundation
811
import SwiftSyntax
9-
import SwiftLang
1012

1113
func getInput(_ file: String) -> URL {
1214
var result = URL(fileURLWithPath: #file)
@@ -27,8 +29,7 @@ VisitorTests.test("Basic") {
2729
}
2830
}
2931
expectDoesNotThrow({
30-
let parsed = try SourceFileSyntax.decodeSourceFileSyntax(try
31-
SwiftLang.parse(getInput("visitor.swift")))
32+
let parsed = try SourceFileSyntax.parse(getInput("visitor.swift"))
3233
let counter = FuncCounter()
3334
let hashBefore = parsed.hashValue
3435
counter.visit(parsed)

test/lit.cfg

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ else:
286286
test_resource_dir = os.path.join(config.swift_lib_dir, 'swift')
287287
resource_dir_opt = ""
288288
stdlib_resource_dir_opt = resource_dir_opt
289-
sourcekitd_framework_dir = config.swift_lib_dir
290289
lit_config.note('Using resource dir: ' + test_resource_dir)
291290

292291
# Parse the variant triple.
@@ -671,13 +670,12 @@ if run_vendor == 'apple':
671670
(run_cpu, run_os, run_vers, clang_mcp_opt))
672671

673672
config.target_build_swift = (
674-
"%s %s %s -F %s -Xlinker -rpath -Xlinker %s %s %s %s %s -F %s -Xlinker -rpath -Xlinker %s"
673+
"%s %s %s -F %s -Xlinker -rpath -Xlinker %s %s %s %s %s"
675674
% (xcrun_prefix, config.swiftc, target_options,
676675
extra_frameworks_dir, extra_frameworks_dir,
677676
sdk_overlay_linker_opt, config.swift_test_options,
678677
config.swift_driver_test_options,
679-
swift_execution_tests_extra_flags, sourcekitd_framework_dir,
680-
sourcekitd_framework_dir))
678+
swift_execution_tests_extra_flags))
681679
config.target_run = ""
682680

683681
if 'interpret' in lit_config.params:

tools/CMakeLists.txt

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
include(CheckIncludeFiles)
2-
check_include_files("xpc/xpc.h" HAVE_XPC_H)
3-
4-
swift_is_installing_component(sourcekit-inproc SOURCEKIT_INSTALLING_INPROC)
5-
6-
if(HAVE_XPC_H AND SWIFT_BUILD_SOURCEKIT AND NOT SOURCEKIT_INSTALLING_INPROC)
7-
set(BUILD_SOURCEKIT_XPC_SERVICE TRUE)
8-
else()
9-
set(BUILD_SOURCEKIT_XPC_SERVICE FALSE)
10-
endif()
11-
121
# Add generated libSyntax headers to global dependencies.
132
list(APPEND LLVM_COMMON_DEPENDS swift-syntax-generated-headers)
143

@@ -27,27 +16,19 @@ add_swift_tool_subdirectory(swift-api-digester)
2716
add_swift_tool_subdirectory(swift-syntax-test)
2817
add_swift_tool_subdirectory(swift-refactor)
2918

30-
if(SWIFT_BUILD_STDLIB AND SWIFT_BUILD_SDK_OVERLAY)
31-
set(BUILD_FOUNDATION TRUE)
32-
else()
33-
set(BUILD_FOUNDATION FALSE)
34-
endif()
35-
3619
if(SWIFT_BUILD_SOURCEKIT)
3720
add_swift_tool_subdirectory(SourceKit)
38-
if(BUILD_SOURCEKIT_XPC_SERVICE AND BUILD_FOUNDATION)
39-
add_subdirectory(SwiftSourceKitClient)
40-
endif()
4121
endif()
4222

23+
4324
if(SWIFT_HOST_VARIANT STREQUAL "macosx")
4425
# Only build Darwin-specific tools when deploying to OS X.
4526
add_swift_tool_subdirectory(swift-stdlib-tool)
4627

4728
# SwiftSyntax depends on both the standard library (because it's a
4829
# Swift module), and the SDK overlays (because it depends on Foundation).
4930
# Ensure we only build SwiftSyntax when we're building both.
50-
if(BUILD_FOUNDATION)
31+
if(SWIFT_BUILD_STDLIB AND SWIFT_BUILD_SDK_OVERLAY)
5132
add_subdirectory(SwiftSyntax)
5233
endif()
5334
endif()

tools/SourceKit/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ configure_file(
2424
set(SOURCEKIT_DEPLOYMENT_OS "${SWIFT_HOST_VARIANT}")
2525
set(SOURCEKIT_DEPLOYMENT_TARGET "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
2626

27+
swift_is_installing_component(sourcekit-inproc SOURCEKIT_INSTALLING_INPROC)
28+
2729
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND NOT CMAKE_CROSSCOMPILING)
2830
set(CMAKE_OSX_SYSROOT "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_PATH}")
2931
set(CMAKE_OSX_ARCHITECTURES "${SWIFT_HOST_VARIANT_ARCH}")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
add_subdirectory(InProc)
2-
if(HAVE_XPC_H)
2+
if (HAVE_XPC_H)
33
add_subdirectory(XPC)
44
endif()

tools/SourceKit/tools/sourcekitd/lib/API/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
include(CheckIncludeFiles)
2+
3+
check_include_files("xpc/xpc.h" HAVE_XPC_H)
4+
15
# If we were going to build for APPLE but don't have XPC, just build inproc.
26
if(APPLE AND NOT HAVE_XPC_H)
37
set(SWIFT_SOURCEKIT_USE_INPROC_LIBRARY TRUE)

tools/SwiftSourceKitClient/CMakeLists.txt

Lines changed: 0 additions & 18 deletions
This file was deleted.

tools/SwiftSourceKitClient/SourceKitdClient.swift

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)