Skip to content

Commit 595f85f

Browse files
committed
[Diagnostics] Properly diagnose missing optional unwrap(s) related to closure result
1 parent e0e13aa commit 595f85f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3287,6 +3287,10 @@ bool ConstraintSystem::repairFailures(
32873287
}
32883288

32893289
case ConstraintLocator::ClosureResult: {
3290+
if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, conversionsOrFixes,
3291+
locator))
3292+
return true;
3293+
32903294
// If we could record a generic arguments mismatch instead of this fix,
32913295
// don't record a ContextualMismatch here.
32923296
if (hasConversionOrRestriction(ConversionRestrictionKind::DeepEquality))
@@ -3391,9 +3395,15 @@ bool ConstraintSystem::repairFailures(
33913395
loc->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
33923396
auto *argExpr = simplifyLocatorToAnchor(loc);
33933397
if (argExpr && isa<ClosureExpr>(argExpr)) {
3394-
conversionsOrFixes.push_back(ContextualMismatch::create(
3395-
*this, lhs, rhs,
3396-
getConstraintLocator(argExpr, ConstraintLocator::ClosureResult)));
3398+
auto *locator =
3399+
getConstraintLocator(argExpr, ConstraintLocator::ClosureResult);
3400+
3401+
if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind,
3402+
conversionsOrFixes, locator))
3403+
break;
3404+
3405+
conversionsOrFixes.push_back(
3406+
IgnoreContextualType::create(*this, lhs, rhs, locator));
33973407
break;
33983408
}
33993409
}

test/Constraints/fixes.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ struct S1116 {
167167
}
168168

169169
let a1116: [S1116] = []
170-
var s1116 = Set(1...10).subtracting(a1116.map({ $0.s })) // expected-error {{cannot convert value of type '[Int?]' to expected argument type 'Set<Int>'}}
171-
170+
var s1116 = Set(1...10).subtracting(a1116.map({ $0.s })) // expected-error {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
171+
// expected-note@-1{{coalesce using '??' to provide a default when the optional value contains 'nil'}} {{49-49=(}} {{53-53= ?? <#default value#>)}}
172+
// expected-note@-2{{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{53-53=!}}
172173

173174
func moreComplexUnwrapFixes() {
174175
struct S {

0 commit comments

Comments
 (0)