Skip to content

Commit 5f5c9dd

Browse files
authored
Merge pull request swiftlang#77284 from hamishknight/for-in
[CS] Fix source range for `for` loop result builder transform
2 parents cb545ed + 2bcffc5 commit 5f5c9dd

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,18 +693,20 @@ class ResultBuilderTransform
693693
return failTransform(forEachStmt);
694694

695695
SmallVector<ASTNode, 4> doBody;
696+
SourceLoc startLoc = forEachStmt->getStartLoc();
696697
SourceLoc endLoc = forEachStmt->getEndLoc();
697698

698699
// Build a variable that is going to hold array of results produced
699-
// by each iteration of the loop.
700+
// by each iteration of the loop. Note we need to give it the start loc of
701+
// the for loop to ensure the implicit 'do' has a correct source range.
700702
//
701703
// Not that it's not going to be initialized here, that would happen
702704
// only when a solution is found.
703705
VarDecl *arrayVar = buildPlaceholderVar(
704-
forEachStmt->getEndLoc(), doBody,
706+
startLoc, doBody,
705707
ArraySliceType::get(PlaceholderType::get(ctx, forEachVar.get())),
706-
ArrayExpr::create(ctx, /*LBrace=*/endLoc, /*Elements=*/{},
707-
/*Commas=*/{}, /*RBrace=*/endLoc));
708+
ArrayExpr::create(ctx, /*LBrace=*/startLoc, /*Elements=*/{},
709+
/*Commas=*/{}, /*RBrace=*/startLoc));
708710

709711
NullablePtr<Expr> bodyVarRef;
710712
std::optional<UnsupportedElt> unsupported;

test/IDE/complete_in_result_builder.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,34 @@ func testOverloadedCallArgs() {
379379
}
380380

381381
}
382+
383+
// https://github.com/swiftlang/swift/issues/77283
384+
func testInForLoop(_ x: [Int]) {
385+
@resultBuilder
386+
struct Builder {
387+
static func buildBlock<T>(_ components: T...) -> T {
388+
components.first!
389+
}
390+
static func buildArray<T>(_ components: [T]) -> T {
391+
components.first!
392+
}
393+
}
394+
struct S {
395+
init() {}
396+
func baz() -> Int { 0 }
397+
}
398+
struct R {
399+
init<T>(@Builder _ x: () -> T) {}
400+
}
401+
_ = R {
402+
for _ in x {
403+
S().#^IN_FOR_LOOP^#
404+
// IN_FOR_LOOP: Decl[InstanceMethod]/CurrNominal: baz()[#Int#]; name=baz()
405+
}
406+
}
407+
_ = R {
408+
for _ in S().#^IN_FOR_LOOP_SEQ^# {
409+
// IN_FOR_LOOP_SEQ: Decl[InstanceMethod]/CurrNominal: baz()[#Int#]; name=baz()
410+
}
411+
}
412+
}

0 commit comments

Comments
 (0)