Skip to content

Commit 94bf392

Browse files
committed
[CSGen] Abort constraint generation on error only if closure participates in type-check
If reference collection discovered at least `ErrorExpr` in the body of a closure, let's fail constraint generation only if it's a single-statement closure, decision about multi-statement closures should be delayed until body is opened. This helps code completion because `ErrorExpr` could belong to a statement unrelated to a completion, so it wouldn't affect its correctness in any way.
1 parent c0bc8ec commit 94bf392

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/Sema/CSGen.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2784,10 +2784,17 @@ namespace {
27842784
return { true, expr };
27852785
}
27862786
} collectVarRefs(CS);
2787+
27872788
closure->walk(collectVarRefs);
27882789

2789-
if (collectVarRefs.hasErrorExprs)
2790+
// If walker discovered error expressions, let's fail constraint
2791+
// genreation only if closure is going to participate
2792+
// in the type-check. This allows us to delay validation of
2793+
// multi-statement closures until body is opened.
2794+
if (shouldTypeCheckInEnclosingExpression(closure) &&
2795+
collectVarRefs.hasErrorExprs) {
27902796
return Type();
2797+
}
27912798

27922799
auto inferredType = inferClosureType(closure);
27932800
if (!inferredType || inferredType->hasError())

test/expr/closure/closures.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func anonymousClosureArgsInClosureWithArgs() {
141141
var a5 = { (_: [Int], w: [Int]) in
142142
f($0.count) // expected-error {{anonymous closure arguments cannot be used inside a closure that has explicit arguments}}
143143
f($1.count) // expected-error {{anonymous closure arguments cannot be used inside a closure that has explicit arguments; did you mean 'w'?}} {{7-9=w}}
144+
// expected-error@-1 {{cannot convert value of type 'Int' to expected argument type 'String'}}
144145
}
145146
}
146147

0 commit comments

Comments
 (0)