Skip to content

Commit fddfd6d

Browse files
committed
[BuilderTransform] Suppress diagnostics produced by preCheckExpression
1 parent 7b1a43b commit fddfd6d

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,12 @@ namespace swift {
10561056
}
10571057
}
10581058

1059+
bool hasDiagnostics() const {
1060+
return std::distance(Engine.TentativeDiagnostics.begin() +
1061+
PrevDiagnostics,
1062+
Engine.TentativeDiagnostics.end()) > 0;
1063+
}
1064+
10591065
/// Abort and close this transaction and erase all diagnostics
10601066
/// record while it was open.
10611067
void abort() {

lib/Sema/BuilderTransform.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ class PreCheckFunctionBuilderApplication : public ASTWalker {
17121712

17131713
public:
17141714
PreCheckFunctionBuilderApplication(AnyFunctionRef fn, bool skipPrecheck)
1715-
: Fn(fn), SkipPrecheck(skipPrecheck) {}
1715+
: Fn(fn), SkipPrecheck(skipPrecheck) {}
17161716

17171717
const std::vector<ReturnStmt *> getReturnStmts() const { return ReturnStmts; }
17181718

@@ -1736,16 +1736,25 @@ class PreCheckFunctionBuilderApplication : public ASTWalker {
17361736
}
17371737

17381738
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
1739+
if (SkipPrecheck)
1740+
return std::make_pair(false, E);
1741+
17391742
// Pre-check the expression. If this fails, abort the walk immediately.
17401743
// Otherwise, replace the expression with the result of pre-checking.
17411744
// In either case, don't recurse into the expression.
1742-
if (!SkipPrecheck &&
1743-
ConstraintSystem::preCheckExpression(E, /*DC*/ Fn.getAsDeclContext())) {
1744-
HasError = true;
1745-
return std::make_pair(false, nullptr);
1746-
}
1745+
{
1746+
auto *DC = Fn.getAsDeclContext();
1747+
auto &diagEngine = DC->getASTContext().Diags;
17471748

1748-
return std::make_pair(false, E);
1749+
// Suppress any diangostics which could be produced by this expression.
1750+
DiagnosticTransaction transaction(diagEngine);
1751+
1752+
HasError |= ConstraintSystem::preCheckExpression(E, DC);
1753+
HasError |= transaction.hasDiagnostics();
1754+
1755+
transaction.abort();
1756+
return std::make_pair(false, HasError ? nullptr : E);
1757+
}
17491758
}
17501759

17511760
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override {

0 commit comments

Comments
 (0)