[CS] Fix source range for for loop result builder transform

Ensure the implicit `do` statement has a source
range that covers the `for` loop by changing the
source location for the initial binding. This ensures
we correctly detect the code completion child and
avoid skipping it.
This commit is contained in:
Hamish Knight
2024-10-29 20:35:40 +00:00
parent b4b99e9d28
commit 2bcffc56d0
2 changed files with 37 additions and 4 deletions

View File

@@ -693,18 +693,20 @@ protected:
return failTransform(forEachStmt);
SmallVector<ASTNode, 4> doBody;
SourceLoc startLoc = forEachStmt->getStartLoc();
SourceLoc endLoc = forEachStmt->getEndLoc();
// Build a variable that is going to hold array of results produced
// by each iteration of the loop.
// by each iteration of the loop. Note we need to give it the start loc of
// the for loop to ensure the implicit 'do' has a correct source range.
//
// Not that it's not going to be initialized here, that would happen
// only when a solution is found.
VarDecl *arrayVar = buildPlaceholderVar(
forEachStmt->getEndLoc(), doBody,
startLoc, doBody,
ArraySliceType::get(PlaceholderType::get(ctx, forEachVar.get())),
ArrayExpr::create(ctx, /*LBrace=*/endLoc, /*Elements=*/{},
/*Commas=*/{}, /*RBrace=*/endLoc));
ArrayExpr::create(ctx, /*LBrace=*/startLoc, /*Elements=*/{},
/*Commas=*/{}, /*RBrace=*/startLoc));
NullablePtr<Expr> bodyVarRef;
std::optional<UnsupportedElt> unsupported;