Skip to content

[Diagnostics] Allow result build to detect ErrorExprs in the body #36854

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
Apr 12, 2021
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
7 changes: 7 additions & 0 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,13 @@ class PreCheckResultBuilderApplication : public ASTWalker {
E, DC, /*replaceInvalidRefsWithErrors=*/false);
HasError |= transaction.hasErrors();

if (!HasError) {
E->forEachChildExpr([&](Expr *expr) {
HasError |= isa<ErrorExpr>(expr);
return HasError ? nullptr : expr;
});
}

if (SuppressDiagnostics)
transaction.abort();

Expand Down
25 changes: 24 additions & 1 deletion lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1734,14 +1734,26 @@ bool IgnoreInvalidResultBuilderBody::diagnose(const Solution &solution,
class PreCheckWalker : public ASTWalker {
DeclContext *DC;
DiagnosticTransaction Transaction;
// Check whether expression(s) in the body of the
// result builder had any `ErrorExpr`s before pre-check.
bool FoundErrorExpr = false;

public:
PreCheckWalker(DeclContext *dc)
: DC(dc), Transaction(dc->getASTContext().Diags) {}

std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
// This has to be checked before `preCheckExpression`
// because pre-check would convert invalid references
// into `ErrorExpr`s.
E->forEachChildExpr([&](Expr *expr) {
FoundErrorExpr |= isa<ErrorExpr>(expr);
return FoundErrorExpr ? nullptr : expr;
});

auto hasError = ConstraintSystem::preCheckExpression(
E, DC, /*replaceInvalidRefsWithErrors=*/true);

return std::make_pair(false, hasError ? nullptr : E);
}

Expand All @@ -1755,7 +1767,18 @@ bool IgnoreInvalidResultBuilderBody::diagnose(const Solution &solution,
}

bool diagnosed() const {
return Transaction.hasErrors();
// pre-check produced an error.
if (Transaction.hasErrors())
return true;

// If there were `ErrorExpr`s before pre-check
// they should have been diagnosed already by parser.
if (FoundErrorExpr) {
auto &DE = DC->getASTContext().Diags;
return DE.hadAnyError();
}

return false;
}
};

Expand Down
9 changes: 9 additions & 0 deletions test/Constraints/result_builder_diags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -712,4 +712,13 @@ struct TuplifiedStructWithInvalidClosure {
0
}
}

@TupleBuilder var errorsDiagnosedByParser: some Any {
if let cond = condition {
tuplify { _ in
self. // expected-error {{expected member name following '.'}}
}
42
}
}
}