Skip to content

Commit 92ad107

Browse files
authored
Merge pull request #63845 from LucianoPAlmeida/variable-question-condition
[Sema] Fix variable invalid optional chaining in if context diagnostic
2 parents 314a652 + 1e4fa6a commit 92ad107

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5920,7 +5920,7 @@ bool ConstraintSystem::repairFailures(
59205920

59215921
if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, conversionsOrFixes,
59225922
locator))
5923-
break;
5923+
return true;
59245924

59255925
// Let's wait until both sides are of the same optionality before
59265926
// attempting `.rawValue` fix.
@@ -5967,7 +5967,7 @@ bool ConstraintSystem::repairFailures(
59675967

59685968
if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind,
59695969
conversionsOrFixes, locator))
5970-
break;
5970+
return true;
59715971

59725972
conversionsOrFixes.push_back(
59735973
IgnoreContextualType::create(*this, lhs, rhs, locator));
@@ -6193,7 +6193,7 @@ bool ConstraintSystem::repairFailures(
61936193
case ConstraintLocator::Condition: {
61946194
if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, conversionsOrFixes,
61956195
locator))
6196-
break;
6196+
return true;
61976197

61986198
conversionsOrFixes.push_back(IgnoreContextualType::create(
61996199
*this, lhs, rhs, getConstraintLocator(locator)));
@@ -6218,7 +6218,7 @@ bool ConstraintSystem::repairFailures(
62186218

62196219
if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, conversionsOrFixes,
62206220
locator))
6221-
break;
6221+
return true;
62226222

62236223
if (repairByTreatingRValueAsLValue(lhs, rhs))
62246224
break;

test/Constraints/if_expr.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,9 @@ func builderInClosure() {
567567
}
568568
}
569569
}
570+
571+
// https://github.com/apple/swift/issues/63796
572+
func testInvalidOptionalChainingInIfContext() {
573+
let v63796 = 1
574+
if v63796? {} // expected-error{{cannot use optional chaining on non-optional value of type 'Int'}}
575+
}

0 commit comments

Comments
 (0)