Skip to content

Commit f8de43b

Browse files
authored
Merge pull request swiftlang#30251 from hborla/invalid-optional-chaining
[ConstraintSystem] Don't assume the lhs type is for a generic parameter in simplifyConformsToConstraint.
2 parents 55782b2 + 6898fbb commit f8de43b

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5175,11 +5175,12 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
51755175
// collection type couldn't be determined without unification to
51765176
// `Any` and `+` failing for all numeric overloads is just a consequence.
51775177
if (typeVar && type->isAny()) {
5178-
auto *GP = typeVar->getImpl().getGenericParameter();
5179-
if (auto *GPD = GP->getDecl()) {
5180-
auto *DC = GPD->getDeclContext();
5181-
if (DC->isTypeContext() && DC->getSelfInterfaceType()->isEqual(GP))
5182-
return SolutionKind::Error;
5178+
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
5179+
if (auto *GPD = GP->getDecl()) {
5180+
auto *DC = GPD->getDeclContext();
5181+
if (DC->isTypeContext() && DC->getSelfInterfaceType()->isEqual(GP))
5182+
return SolutionKind::Error;
5183+
}
51835184
}
51845185
}
51855186

test/Constraints/optional.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,10 @@ func test_force_unwrap_not_being_too_eager() {
420420
if let _ = obj.delegate?.window { // Ok
421421
}
422422
}
423+
424+
// rdar://problem/57097401
425+
func invalidOptionalChaining(a: Any) {
426+
a == "="? // expected-error {{cannot use optional chaining on non-optional value of type 'String'}}
427+
// expected-error@-1 {{value of protocol type 'Any' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols}}
428+
// expected-note@-2 {{requirement from conditional conformance of 'Any?' to 'Equatable'}}
429+
}

0 commit comments

Comments
 (0)