Skip to content
Merged
Show file tree
Hide file tree
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
32 changes: 24 additions & 8 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,30 @@ class BuilderClosureVisitor
// to get diagnostics if something about this builder call fails,
// e.g. if there isn't a matching overload for `buildBlock`.
// But we can only do this if there isn't a type variable in the type.
TypeLoc typeLoc(nullptr,
builderType->hasTypeVariable() ? Type() : builderType);
TypeLoc typeLoc;
if (!builderType->hasTypeVariable()) {
typeLoc = TypeLoc(new (ctx) FixedTypeRepr(builderType, loc), builderType);
}

auto typeExpr = new (ctx) TypeExpr(typeLoc);
if (cs) {
cs->setType(typeExpr, MetatypeType::get(builderType));
cs->setType(&typeExpr->getTypeLoc(), builderType);
}

SmallVector<SourceLoc, 4> argLabelLocs;
for (auto i : indices(argLabels)) {
argLabelLocs.push_back(args[i]->getStartLoc());
}

typeExpr->setImplicit();
auto memberRef = new (ctx) UnresolvedDotExpr(
typeExpr, loc, fnName, DeclNameLoc(loc), /*implicit=*/true);
return CallExpr::createImplicit(ctx, memberRef, args, argLabels);
SourceLoc openLoc = args.empty() ? loc : args.front()->getStartLoc();
SourceLoc closeLoc = args.empty() ? loc : args.back()->getEndLoc();
return CallExpr::create(ctx, memberRef, openLoc, args,
argLabels, argLabelLocs, closeLoc,
/*trailing closure*/ nullptr, /*implicit*/true);
}

/// Check whether the builder supports the given operation.
Expand Down Expand Up @@ -400,18 +411,21 @@ class BuilderClosureVisitor
auto optionalDecl = ctx.getOptionalDecl();
auto optionalType = optionalDecl->getDeclaredType();

auto optionalTypeExpr = TypeExpr::createImplicit(optionalType, ctx);
auto loc = arg->getStartLoc();
auto optionalTypeExpr =
TypeExpr::createImplicitHack(loc, optionalType, ctx);
auto someRef = new (ctx) UnresolvedDotExpr(
optionalTypeExpr, SourceLoc(), ctx.getIdentifier("some"),
DeclNameLoc(), /*implicit=*/true);
optionalTypeExpr, loc, ctx.getIdentifier("some"),
DeclNameLoc(loc), /*implicit=*/true);
return CallExpr::createImplicit(ctx, someRef, arg, { });
}

Expr *buildNoneExpr(SourceLoc endLoc) {
auto optionalDecl = ctx.getOptionalDecl();
auto optionalType = optionalDecl->getDeclaredType();

auto optionalTypeExpr = TypeExpr::createImplicit(optionalType, ctx);
auto optionalTypeExpr =
TypeExpr::createImplicitHack(endLoc, optionalType, ctx);
return new (ctx) UnresolvedDotExpr(
optionalTypeExpr, endLoc, ctx.getIdentifier("none"),
DeclNameLoc(endLoc), /*implicit=*/true);
Expand Down Expand Up @@ -511,7 +525,9 @@ bool TypeChecker::typeCheckFunctionBuilderFuncBody(FuncDecl *FD,
return true;
assert(returnExprType->isEqual(returnType));

auto returnStmt = new (Context) ReturnStmt(SourceLoc(), returnExpr);
auto loc = returnExpr->getStartLoc();
auto returnStmt =
new (Context) ReturnStmt(loc, returnExpr, /*implicit*/ true);
auto origBody = FD->getBody();
auto fakeBody = BraceStmt::create(Context, origBody->getLBraceLoc(),
{ returnStmt },
Expand Down
6 changes: 2 additions & 4 deletions test/IDE/complete_function_builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=IN_CLOSURE_COLOR_CONTEXT | %FileCheck %s -check-prefix=IN_CLOSURE_COLOR_CONTEXT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=IN_CLOSURE_COLOR_CONTEXT_DOT | %FileCheck %s -check-prefix=IN_CLOSURE_COLOR_CONTEXT_DOT

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONTEXTUAL_TYPE_1 | %FileCheck %s -check-prefix=CONTEXTUAL_TYPE_INVALID
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONTEXTUAL_TYPE_2 | %FileCheck %s -check-prefix=CONTEXTUAL_TYPE_INVALID
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONTEXTUAL_TYPE_1 | %FileCheck %s -check-prefix=CONTEXTUAL_TYPE_VALID
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONTEXTUAL_TYPE_2 | %FileCheck %s -check-prefix=CONTEXTUAL_TYPE_VALID
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONTEXTUAL_TYPE_3 | %FileCheck %s -check-prefix=CONTEXTUAL_TYPE_VALID
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONTEXTUAL_TYPE_4 | %FileCheck %s -check-prefix=CONTEXTUAL_TYPE_VALID
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CONTEXTUAL_TYPE_5 | %FileCheck %s -check-prefix=CONTEXTUAL_TYPE_INVALID
Expand Down Expand Up @@ -123,11 +123,9 @@ func acceptBuilder(@EnumToVoidBuilder body: () -> Void) {}

func testContextualType() {
acceptBuilder {
// FIXME: This should suggest enum values.
.#^CONTEXTUAL_TYPE_1^#
}
acceptBuilder {
// FIXME: This should suggest enum values.
.#^CONTEXTUAL_TYPE_2^#;
.north;
}
Expand Down
31 changes: 31 additions & 0 deletions test/Profiler/coverage_function_builder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -profile-generate -profile-coverage-mapping -emit-sorted-sil -emit-sil -module-name coverage_functon_builder %s | %FileCheck %s

@_functionBuilder
struct Summer {
static func buildBlock(_ x: Int...) -> Int {
return x.reduce(0, +)
}
static func buildIf(_ x: Int?) -> Int {
return x ?? 0
}
}

// CHECK-LABEL: sil_coverage_map {{.*}} "$s24coverage_functon_builder5test0SiyF"
@Summer
func test0() -> Int {
// CHECK: [[@LINE-1]]:21 -> [[@LINE+3]]:2 : 0
18
12
}

// CHECK-LABEL: sil_coverage_map {{.*}} "$s24coverage_functon_builder5test1SiyF"
@Summer
func test1() -> Int {
// CHECK: [[@LINE-1]]:21 -> [[@LINE+7]]:2 : 0
18
12
if 7 < 23 {
11
8
}
}