Skip to content

Commit c6ff7b4

Browse files
committed
[CSBindings] Look through optional types when trying to validate l-valueness of the new bindings
When bindings are picked for particular type variable, right-hand side of the binding might be another type variable wrapped into optional type, when trying to determine if both sides of the binding have the same l-valueness it's imperative to look throught optional type of the right-hand side. Otherwise new binding might be effectively unsolvable. Resolves: rdar://problem/37291371
1 parent d9067d9 commit c6ff7b4

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
447447

448448
// Make sure we aren't trying to equate type variables with different
449449
// lvalue-binding rules.
450-
if (auto otherTypeVar = type->getAs<TypeVariableType>()) {
450+
if (auto otherTypeVar =
451+
type->lookThroughAllOptionalTypes()->getAs<TypeVariableType>()) {
451452
if (typeVar->getImpl().canBindToLValue() !=
452453
otherTypeVar->getImpl().canBindToLValue())
453454
continue;

test/Constraints/rdar37291371.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
extension Collection where Element: Numeric {
4+
var v: Element {
5+
return self.reduce(0, +)
6+
}
7+
}
8+
9+
struct R<T> {}
10+
func ==<T: Equatable>(lhs: R<T>, rhs: T?) {}
11+
12+
func foo<T>(_ e: @autoclosure @escaping () throws -> T?) -> R<T> {
13+
return R<T>()
14+
}
15+
16+
func bar<T>(_ e: T?) -> R<T> {
17+
return R<T>()
18+
}
19+
20+
foo([Double(1.0)].v) == Double(1.0)
21+
bar([Double(1.0)].v) == Double(1.0)

validation-test/compiler_crashers/28625-destoptionals-size-destextraoptionals-srcoptionals-size.swift renamed to validation-test/compiler_crashers_fixed/28625-destoptionals-size-destextraoptionals-srcoptionals-size.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9-
// REQUIRES: asserts
8+
// RUN: not %target-swift-frontend %s -emit-ir
9+
1010
nil?as?Int??
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
[.a
1010
[Int?as?Int
1111
nil?

0 commit comments

Comments
 (0)