From 0f194f4fcd4851ecd64aa70cb017b4d851b30043 Mon Sep 17 00:00:00 2001 From: Pavel Yaskevich Date: Mon, 23 Oct 2023 09:43:43 -0700 Subject: [PATCH] [CSBindings] Don't priorize variables without direct bindings over disjunctions It's possible that a disjunction could introduce new bindings to the set. --- lib/Sema/CSBindings.cpp | 4 ++++ test/Constraints/casts.swift | 6 +++--- test/SILGen/collection_downcast.swift | 11 ----------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/Sema/CSBindings.cpp b/lib/Sema/CSBindings.cpp index 80ef61bc4bf81..066fa953b1428 100644 --- a/lib/Sema/CSBindings.cpp +++ b/lib/Sema/CSBindings.cpp @@ -1080,6 +1080,10 @@ bool BindingSet::favoredOverDisjunction(Constraint *disjunction) const { return boundType->lookThroughAllOptionalTypes()->is(); } + // Don't prioritize type variables that don't have any direct bindings. + if (Bindings.empty()) + return false; + return !involvesTypeVariables(); } diff --git a/test/Constraints/casts.swift b/test/Constraints/casts.swift index c617a85f16fb1..978977a97dc9b 100644 --- a/test/Constraints/casts.swift +++ b/test/Constraints/casts.swift @@ -349,8 +349,8 @@ func test_compatibility_coercions(_ arr: [Int], _ optArr: [Int]?, _ dict: [Strin _ = [i: stringAnyDict] as [String: Any] // expected-error {{cannot convert value of type 'Int' to expected dictionary key type 'String'}} // These are currently not peepholed. - _ = [i].self as Magic as [String] // expected-warning {{coercion from '[Int]' to '[String]' may fail; use 'as?' or 'as!' instead}} - _ = (try [i]) as Magic as [String] // expected-warning {{coercion from '[Int]' to '[String]' may fail; use 'as?' or 'as!' instead}} + _ = [i].self as Magic as [String] // expected-error {{cannot convert value of type 'Int' to expected element type 'String'}} + _ = (try [i]) as Magic as [String] // expected-error {{cannot convert value of type 'Int' to expected element type 'String'}} // expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}} // These are wrong, but make sure we don't warn about the value cast always succeeding. @@ -750,4 +750,4 @@ do { _ = a is Issue68825 // expected-error {{cannot find type 'Issue68825' in scope}} _ = a is String // OK } -} \ No newline at end of file +} diff --git a/test/SILGen/collection_downcast.swift b/test/SILGen/collection_downcast.swift index 6fcdb017312a4..9a604fdbbc488 100644 --- a/test/SILGen/collection_downcast.swift +++ b/test/SILGen/collection_downcast.swift @@ -297,15 +297,4 @@ func testCollectionCompatibilityCoercions(_ arr: [Int], _ optArr: [Any]?, _ set: // CHECK: [[CAST_FN:%.+]] = function_ref @$ss17_dictionaryUpCastySDyq0_q1_GSDyxq_GSHRzSHR0_r2_lF : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2, τ_0_3 where τ_0_0 : Hashable, τ_0_2 : Hashable> (@guaranteed Dictionary<τ_0_0, τ_0_1>) -> @owned Dictionary<τ_0_2, τ_0_3> // CHECK: apply [[CAST_FN]]([[DICT]]) : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2, τ_0_3 where τ_0_0 : Hashable, τ_0_2 : Hashable> (@guaranteed Dictionary<τ_0_0, τ_0_1>) -> @owned Dictionary<τ_0_2, τ_0_3> _ = promote(promote(promote(dict))) as [Int: String] - - typealias Magic = T - - // These are currently not peepholed. - // CHECK: [[CAST_FN:%.+]] = function_ref @$ss15_arrayForceCastySayq_GSayxGr0_lF : $@convention(thin) <τ_0_0, τ_0_1> (@guaranteed Array<τ_0_0>) -> @owned Array<τ_0_1> - // CHECK: apply [[CAST_FN]] - [i].self as Magic as [String] - - // CHECK: [[CAST_FN:%.+]] = function_ref @$ss15_arrayForceCastySayq_GSayxGr0_lF : $@convention(thin) <τ_0_0, τ_0_1> (@guaranteed Array<τ_0_0>) -> @owned Array<τ_0_1> - // CHECK: apply [[CAST_FN]] - (try [i]) as Magic as [String] }