Skip to content

Commit 5208049

Browse files
committed
[CSBindings] Form bindings correctly when they come from 'OptionalObject' constraint
Fixes a bug in `getPotentialBindings` when the source of the bindings is 'OptionalObject' constraint and type variable is on the left-hand side of that constraint, that makes such type variable always have an optional type since right-hand side of 'OptionalObject' is its 'object' type. Resolves: rdar://problem/37508855
1 parent f2c3870 commit 5208049

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,15 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
402402
if (type->hasError())
403403
continue;
404404

405+
// If the source of the binding is 'OptionalObject' constraint
406+
// and type variable is on the left-hand side, that means
407+
// that it _has_ to be of optional type, since the right-hand
408+
// side of the constraint is object type of the optional.
409+
if (constraint->getKind() == ConstraintKind::OptionalObject &&
410+
kind == AllowedBindingKind::Subtypes) {
411+
type = OptionalType::get(type);
412+
}
413+
405414
// If the type we'd be binding to is a dependent member, don't try to
406415
// resolve this type variable yet.
407416
if (type->is<DependentMemberType>()) {

test/Constraints/diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ func SR_6272_c() {
981981
struct SR_6272_D: ExpressibleByIntegerLiteral {
982982
typealias IntegerLiteralType = Int
983983
init(integerLiteral: Int) {}
984-
static func +(lhs: SR_6272_D, rhs: Int) -> Float { return 42.0 }
984+
static func +(lhs: SR_6272_D, rhs: Int) -> Float { return 42.0 } // expected-note {{found this candidate}}
985985
}
986986

987987
func SR_6272_d() {
@@ -1167,7 +1167,7 @@ func rdar17170728() {
11671167

11681168
let _ = [i, j, k].reduce(0 as Int?) {
11691169
$0 && $1 ? $0! + $1! : ($0 ? $0! : ($1 ? $1! : nil))
1170-
// expected-error@-1 {{type of expression is ambiguous without more context}}
1170+
// expected-error@-1 {{ambiguous use of operator '+'}}
11711171
}
11721172
}
11731173

test/Constraints/optional.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,8 @@ class Bar {
267267
let _: Int = result // expected-error{{cannot convert value of type 'X?' to specified type 'Int'}}
268268
}
269269
}
270+
271+
// rdar://problem/37508855
272+
func rdar37508855(_ e1: X?, _ e2: X?) -> [X] {
273+
return [e1, e2].filter { $0 == nil }.map { $0! }
274+
}

0 commit comments

Comments
 (0)