@@ -72,6 +72,9 @@ class ResultBuilderTransform
7272 DeclContext *dc;
7373 ResultBuilder builder;
7474
75+ // / The source range of the body.
76+ SourceRange bodyRange;
77+
7578 // / The result type of this result builder body.
7679 Type ResultType;
7780
@@ -80,9 +83,9 @@ class ResultBuilderTransform
8083
8184public:
8285 ResultBuilderTransform (ConstraintSystem &cs, DeclContext *dc,
83- Type builderType, Type resultTy)
86+ SourceRange bodyRange, Type builderType, Type resultTy)
8487 : ctx(cs.getASTContext()), dc(dc), builder(cs, dc, builderType),
85- ResultType (resultTy) {}
88+ bodyRange (bodyRange), ResultType(resultTy) {}
8689
8790 UnsupportedElt getUnsupportedElement () const { return FirstUnsupported; }
8891
@@ -238,12 +241,14 @@ class ResultBuilderTransform
238241 // buildBlock higher.
239242 buildBlockArguments.push_back (expr);
240243 } else if (ctx.CompletionCallback && expr->getSourceRange ().isValid () &&
244+ containsIDEInspectionTarget (bodyRange, ctx.SourceMgr ) &&
241245 !containsIDEInspectionTarget (expr->getSourceRange (),
242246 ctx.SourceMgr )) {
243- // A statement that doesn't contain the code completion expression can't
244- // influence the type of the code completion expression. Add a variable
245- // for it that we can put into the buildBlock call but don't add the
246- // expression itself into the transformed body to improve performance.
247+ // A top-level expression that doesn't contain the code completion
248+ // expression can't influence the type of the code completion expression
249+ // if they're in the same result builder. Add a variable for it that we
250+ // can put into the buildBlock call but don't add the expression itself
251+ // into the transformed body to improve performance.
247252 auto *resultVar = buildPlaceholderVar (expr->getStartLoc (), newBody);
248253 buildBlockArguments.push_back (
249254 builder.buildVarRef (resultVar, expr->getStartLoc ()));
@@ -1176,6 +1181,7 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11761181 // let's do it and cache the result.
11771182 if (!transformedBody) {
11781183 ResultBuilderTransform transform (*this , fn.getAsDeclContext (),
1184+ fn.getBody ()->getSourceRange (),
11791185 builderType, bodyResultType);
11801186 auto *body = transform.apply (fn.getBody ());
11811187
0 commit comments