Skip to content

[CSApply] Fixed type-checked subexpression output bug for multi-state… #59260

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 2 commits into from
Jun 18, 2022
Merged
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
45 changes: 43 additions & 2 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8152,6 +8152,11 @@ namespace {
return false;
}

/// Check if there are any closures or tap expressions left to process separately.
bool hasDelayedTasks() {
return !ClosuresToTypeCheck.empty() || !TapsToTypeCheck.empty();
}

/// Process delayed closure bodies and `Tap` expressions.
///
/// \returns true if any part of the processing fails.
Expand Down Expand Up @@ -8993,8 +8998,31 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
result.setExpr(resultExpr);

if (cs.isDebugMode()) {
// If target is a multi-statement closure or
// a tap expression, expression will not be fully
// type checked until these expressions are visited in
// processDelayed().
bool isPartial = false;
resultExpr->forEachChildExpr([&](Expr *child) -> Expr * {
if (auto *closure = dyn_cast<ClosureExpr>(child)) {
if (!closure->hasSingleExpressionBody()) {
isPartial = true;
return nullptr;
}
}
if (isa<TapExpr>(child)) {
isPartial = true;
return nullptr;
}
return child;
});

auto &log = llvm::errs();
log << "---Type-checked expression---\n";
if (isPartial) {
log << "---Partially type-checked expression---\n";
} else {
log << "---Type-checked expression---\n";
}
resultExpr->dump(log);
log << "\n";
}
Expand Down Expand Up @@ -9047,6 +9075,8 @@ Optional<SolutionApplicationTarget> ConstraintSystem::applySolution(
if (!resultTarget)
return None;

auto needsPostProcessing = walker.hasDelayedTasks();

// Visit closures that have non-single expression bodies, tap expressions,
// and possibly other types of AST nodes which could only be processed
// after contextual expression.
Expand All @@ -9055,7 +9085,18 @@ Optional<SolutionApplicationTarget> ConstraintSystem::applySolution(
// If any of them failed to type check, bail.
if (hadError)
return None;


if (isDebugMode()) {
// If we had partially type-checked expressions, lets print
// fully type-checked expression after processDelayed is done.
if (needsPostProcessing) {
auto &log = llvm::errs();
log << "---Fully type-checked expression---\n";
resultTarget->getAsExpr()->dump(log);
log << "\n";
}
}

rewriter.finalize();

return resultTarget;
Expand Down