Skip to content

[Sema] Don’t fail constraint generation if a closure contains an ErrorExpr #62833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4926,7 +4926,9 @@ bool MissingArgumentsFailure::diagnoseClosure(const ClosureExpr *closure) {

auto *locator = getLocator();
if (locator->isForContextualType()) {
funcType = getContextualType(locator->getAnchor())->getAs<FunctionType>();
if (auto contextualType = getContextualType(locator->getAnchor())) {
funcType = contextualType->getAs<FunctionType>();
}
} else if (auto info = getFunctionArgApplyInfo(locator)) {
auto paramType = info->getParamType();
// Drop a single layer of optionality because argument could get injected
Expand Down
15 changes: 0 additions & 15 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2901,18 +2901,12 @@ namespace {
struct CollectVarRefs : public ASTWalker {
ConstraintSystem &cs;
llvm::SmallPtrSet<TypeVariableType *, 4> varRefs;
bool hasErrorExprs = false;

CollectVarRefs(ConstraintSystem &cs) : cs(cs) { }

bool shouldWalkCaptureInitializerExpressions() override { return true; }

PreWalkResult<Expr *> walkToExprPre(Expr *expr) override {
// If there are any error expressions in this closure
// it wouldn't be possible to infer its type.
if (isa<ErrorExpr>(expr))
hasErrorExprs = true;

// Retrieve type variables from references to var decls.
if (auto *declRef = dyn_cast<DeclRefExpr>(expr)) {
if (auto *varDecl = dyn_cast<VarDecl>(declRef->getDecl())) {
Expand Down Expand Up @@ -2954,15 +2948,6 @@ namespace {

closure->walk(collectVarRefs);

// If walker discovered error expressions, let's fail constraint
// generation only if closure is going to participate
// in the type-check. This allows us to delay validation of
// multi-statement closures until body is opened.
if (CS.participatesInInference(closure) &&
collectVarRefs.hasErrorExprs) {
return Type();
}

auto inferredType = inferClosureType(closure);
if (!inferredType || inferredType->hasError())
return Type();
Expand Down
6 changes: 3 additions & 3 deletions test/Constraints/result_builder_diags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct TupleBuilderWithoutIf { // expected-note 3{{struct 'TupleBuilderWithoutIf
static func buildDo<T>(_ value: T) -> T { return value }
}

func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) {
func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) { // expected-note 2{{in call to function 'tuplify(_:body:)'}}
print(body(cond))
}

Expand All @@ -88,7 +88,7 @@ func tuplifyWithoutIf<T>(_ cond: Bool, @TupleBuilderWithoutIf body: (Bool) -> T)

func testDiags() {
// For loop
tuplify(true) { _ in
tuplify(true) { _ in // expected-error {{generic parameter 'T' could not be inferred}}
17
for c in name {
// expected-error@-1 {{cannot find 'name' in scope}}
Expand Down Expand Up @@ -464,7 +464,7 @@ struct TestConstraintGenerationErrors {
}

func buildTupleClosure() {
tuplify(true) { _ in
tuplify(true) { _ in // expected-error {{generic parameter 'T' could not be inferred}}
let a = nothing // expected-error {{cannot find 'nothing' in scope}}
String(nothing) // expected-error {{cannot find 'nothing' in scope}}
}
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/recovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ class r22240342 {
lazy var xx: Int = {
foo { // expected-error {{cannot find 'foo' in scope}}
let issueView = 42
issueView.delegate = 12
issueView.delegate = 12 // expected-error {{value of type 'Int' has no member 'delegate'}}

}
return 42
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ Regex { // expected-error {{regex builder requires the 'RegexBuilder' module be
/g(h)(i)/
}

// FIXME: Unfortunately we bail from CSGen if we end up with an ErrorExpr, so
// don't get a chance to diagnose. We ought to try solving with holes.
// For now at least, this error should at least hopefully nudge users into
// realizing they have a missing import.
Regex {
Regex { // expected-error {{regex builder requires the 'RegexBuilder' module be imported'}}
Capture { // expected-error {{cannot find 'Capture' in scope}}
/abc/
}
Expand Down
2 changes: 1 addition & 1 deletion test/stmt/statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func matching_pattern_recursion(zs: [Int]) { // expected-note {{'zs' declared he
}

switch 42 {
case {
case { // expected-error {{expression pattern of type '() -> ()' cannot match values of type 'Int'}}
for i in ws { // expected-error {{cannot find 'ws' in scope; did you mean 'zs'?}}
}
}: break
Expand Down