Skip to content

Commit 1e012fe

Browse files
committed
[CS] Avoid bailing on result builder return for completion
Make sure we continue to solve the body normally rather than bailing early.
1 parent fef7f53 commit 1e012fe

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,8 +1132,9 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11321132
if (fn.bodyHasExplicitReturnStmt()) {
11331133
// Diagnostic mode means that solver couldn't reach any viable
11341134
// solution, so let's diagnose presence of a `return` statement
1135-
// in the closure body.
1136-
if (shouldAttemptFixes()) {
1135+
// in the closure body. Avoid doing this for completion since we need to
1136+
// continue solving the body.
1137+
if (shouldAttemptFixes() && !isForCodeCompletion()) {
11371138
if (recordFix(IgnoreResultBuilderWithReturnStmts::create(
11381139
*this, builderType,
11391140
getConstraintLocator(fn.getAbstractClosureExpr()))))

test/IDE/complete_in_result_builder.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ struct TupleBuilder<T> {
1616
}
1717
}
1818

19-
func buildStringTuple<Result>(@TupleBuilder<String> x: () -> Result) {}
19+
@discardableResult
20+
func buildStringTuple<Result>(@TupleBuilder<String> x: () -> Result) -> Result {
21+
x()
22+
}
2023

2124
enum StringFactory {
2225
static func makeString(x: String) -> String { return x }
@@ -98,6 +101,21 @@ struct FooStruct {
98101
func intGen() -> Int { return 1 }
99102
}
100103
104+
func testReturn() {
105+
// Make sure we can still complete even with return.
106+
buildStringTuple {
107+
StringFactory.#^COMPLETE_STATIC_MEMBER_WITH_RETURN^#
108+
// COMPLETE_STATIC_MEMBER_WITH_RETURN: Decl[StaticMethod]/CurrNominal: makeString({#x: String#})[#String#];
109+
return ""
110+
}
111+
112+
buildStringTuple {
113+
""
114+
return FooStruct()
115+
}.#^COMPLETE_INSTANCE_MEMBER_ON_RETURN_BUILDER^#
116+
// COMPLETE_INSTANCE_MEMBER_ON_RETURN_BUILDER: Decl[InstanceVar]/CurrNominal: instanceVar[#Int#]; name=instanceVar
117+
}
118+
101119
func testPatternMatching() {
102120
@TupleBuilder<String> var x1 {
103121
let x = Letters.b

0 commit comments

Comments
 (0)