Skip to content

Commit 15e18e9

Browse files
authored
Merge pull request swiftlang#36358 from xedin/rdar-74696023
[Diagnostics] Verify that optional evaluation got type from context b…
2 parents 0e69285 + dd70528 commit 15e18e9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3793,7 +3793,9 @@ bool ConstraintSystem::repairFailures(
37933793
// Because result type of the optional evaluation is supposed to
37943794
// represent the type of its sub-expression with added level of
37953795
// optionality if needed.
3796-
if (!lhs->getOptionalObjectType() && !lhs->hasTypeVariable()) {
3796+
auto contextualTy = simplifyType(rhs)->getOptionalObjectType();
3797+
if (!lhs->getOptionalObjectType() && !lhs->hasTypeVariable() &&
3798+
!contextualTy->isTypeVariableOrMember()) {
37973799
conversionsOrFixes.push_back(IgnoreContextualType::create(
37983800
*this, lhs, rhs, getConstraintLocator(OEE->getSubExpr())));
37993801
return true;

test/Constraints/diagnostics.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,3 +1360,17 @@ SR5688_1!.count // expected-error {{function 'SR5688_1' was used as a property;
13601360

13611361
func SR5688_2() -> Int? { 0 }
13621362
let _: Int = SR5688_2! // expected-error {{function 'SR5688_2' was used as a property; add () to call it}} {{22-22=()}}
1363+
1364+
1365+
// rdar://74696023 - Fallback error when passing incorrect optional type to `==` operator
1366+
func rdar74696023() {
1367+
struct MyError {
1368+
var code: Int = 0
1369+
}
1370+
1371+
func test(error: MyError?, code: Int32) {
1372+
guard error?.code == code else { fatalError() } // expected-error {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
1373+
// expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
1374+
// expected-note@-2 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
1375+
}
1376+
}

0 commit comments

Comments
 (0)