Skip to content

Commit

Permalink
[SE-0289] Continue printing @_functionBuilder in .swiftinterfaces.
Browse files Browse the repository at this point in the history
Maintain the ability for older Swift compilers to read .swiftinterfaces
that make use of result builders by always emitting @_functionBuilder
rather than the newer @resultBuilder.
  • Loading branch information
DougGregor committed Oct 21, 2020
1 parent 6d41524 commit b6c0145
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/AST/Attr.cpp
Expand Up @@ -771,6 +771,10 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
case DAK_Optimize:
if (DeclAttribute::isDeclModifier(getKind())) {
Printer.printKeyword(getAttrName(), Options);
} else if (Options.IsForSwiftInterface && getKind() == DAK_ResultBuilder) {
// Use @_functionBuilder in Swift interfaces to maintain backward
// compatibility.
Printer.printSimpleAttr("_functionBuilder", /*needAt=*/true);
} else {
Printer.printSimpleAttr(getAttrName(), /*needAt=*/true);
}
Expand Down
2 changes: 1 addition & 1 deletion test/ModuleInterface/Inputs/result_builders_client.swift
@@ -1,4 +1,4 @@
import FunctionBuilders
import ResultBuilders

let name = "dsl"
tuplify(true) {
Expand Down
16 changes: 9 additions & 7 deletions test/ModuleInterface/result_builders.swift
@@ -1,9 +1,11 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -typecheck -module-name FunctionBuilders -emit-module-interface-path %t/FunctionBuilders.swiftinterface %s
// RUN: %FileCheck %s < %t/FunctionBuilders.swiftinterface
// RUN: %target-swift-frontend -typecheck -module-name ResultBuilders -emit-module-interface-path %t/ResultBuilders.swiftinterface %s
// RUN: %FileCheck %s < %t/ResultBuilders.swiftinterface
// RUN: %target-swift-frontend -I %t -typecheck -verify %S/Inputs/result_builders_client.swift
// RUN: %target-swift-frontend -compile-module-from-interface %t/FunctionBuilders.swiftinterface -o %t/FunctionBuilders.swiftmodule
// RUN: %target-swift-frontend -compile-module-from-interface %t/ResultBuilders.swiftinterface -o %t/ResultBuilders.swiftmodule
// RUN: %FileCheck %s < %t/ResultBuilders.swiftinterface

// CHECK: @_functionBuilder public struct TupleBuilder
@resultBuilder
public struct TupleBuilder {
public static func buildBlock<T1, T2>(_ t1: T1, _ t2: T2) -> (T1, T2) {
Expand Down Expand Up @@ -38,7 +40,7 @@ public struct TupleBuilder {
public static func buildIf<T>(_ value: T?) -> T? { return value }
}

// CHECK-LABEL: public func tuplify<T>(_ cond: Swift.Bool, @FunctionBuilders.TupleBuilder body: (Swift.Bool) -> T)
// CHECK-LABEL: public func tuplify<T>(_ cond: Swift.Bool, @ResultBuilders.TupleBuilder body: (Swift.Bool) -> T)
public func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) {
print(body(cond))
}
Expand All @@ -52,13 +54,13 @@ public struct UsesBuilderProperty {
"goodbye"
}

// CHECK: public func myFunc(@FunctionBuilders.TupleBuilder fn: () -> ())
// CHECK: public func myFunc(@ResultBuilders.TupleBuilder fn: () -> ())
public func myFunc(@TupleBuilder fn: () -> ()) {}
}

public protocol ProtocolWithBuilderProperty {
associatedtype Assoc
// CHECK: @FunctionBuilders.TupleBuilder var myVar: Self.Assoc { get }

// CHECK: @ResultBuilders.TupleBuilder var myVar: Self.Assoc { get }
@TupleBuilder var myVar: Assoc { get }
}

0 comments on commit b6c0145

Please sign in to comment.