Skip to content

Commit 697dfba

Browse files
committed
[TypeChecker] Enable result builder AST transform by default
1 parent ed630db commit 697dfba

File tree

14 files changed

+53
-116
lines changed

14 files changed

+53
-116
lines changed

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ LANGUAGE_FEATURE(BuiltinCopy, 0, "Builtin.copy()", true)
8484
LANGUAGE_FEATURE(BuiltinStackAlloc, 0, "Builtin.stackAlloc", true)
8585
LANGUAGE_FEATURE(BuiltinTaskRunInline, 0, "Builtin.taskRunInline", true)
8686
LANGUAGE_FEATURE(BuiltinUnprotectedAddressOf, 0, "Builtin.unprotectedAddressOf", true)
87+
LANGUAGE_FEATURE(ResultBuilderASTTransform, 0, "result builders as AST transform", true)
8788
SUPPRESSIBLE_LANGUAGE_FEATURE(SpecializeAttributeWithAvailability, 0, "@_specialize attribute with availability", true)
8889
LANGUAGE_FEATURE(BuiltinAssumeAlignment, 0, "Builtin.assumeAlignment", true)
8990
SUPPRESSIBLE_LANGUAGE_FEATURE(UnsafeInheritExecutor, 0, "@_unsafeInheritExecutor", true)
@@ -104,7 +105,6 @@ EXPERIMENTAL_FEATURE(FlowSensitiveConcurrencyCaptures, false)
104105
EXPERIMENTAL_FEATURE(MoveOnly, false)
105106
EXPERIMENTAL_FEATURE(OneWayClosureParameters, false)
106107
EXPERIMENTAL_FEATURE(TypeWitnessSystemInference, false)
107-
EXPERIMENTAL_FEATURE(ResultBuilderASTTransform, true)
108108
EXPERIMENTAL_FEATURE(LayoutPrespecialization, false)
109109
EXPERIMENTAL_FEATURE(ModuleInterfaceExportAs, true)
110110

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
708708
if (Args.hasArg(OPT_enable_experimental_opaque_type_erasure))
709709
Opts.Features.insert(Feature::OpaqueTypeErasure);
710710

711+
Opts.Features.insert(Feature::ResultBuilderASTTransform);
712+
711713
Opts.EnableAppExtensionRestrictions |= Args.hasArg(OPT_enable_app_extension);
712714

713715
Opts.EnableSwift3ObjCInference =

test/Constraints/result_builder_diags.swift

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct TupleBuilderWithoutIf { // expected-note 3{{struct 'TupleBuilderWithoutIf
7878
static func buildDo<T>(_ value: T) -> T { return value }
7979
}
8080

81-
func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) { // expected-note {{in call to function 'tuplify(_:body:)'}}
81+
func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) {
8282
print(body(cond))
8383
}
8484

@@ -194,7 +194,7 @@ struct TupleP<U> : P {
194194

195195
@resultBuilder
196196
struct Builder {
197-
static func buildBlock<S0, S1>(_ stmt1: S0, _ stmt2: S1) // expected-note {{required by static method 'buildBlock' where 'S1' = 'Label<_>.Type'}}
197+
static func buildBlock<S0, S1>(_ stmt1: S0, _ stmt2: S1)
198198
-> TupleP<(S0, S1)> where S0: P, S1: P {
199199
return TupleP((stmt1, stmt2))
200200
}
@@ -216,7 +216,7 @@ struct Label<L> : P where L : P { // expected-note 2 {{'L' declared as parameter
216216
}
217217

218218
func test_51167632() -> some P {
219-
AnyP(G { // expected-error {{type 'Label<_>.Type' cannot conform to 'P'}} expected-note {{only concrete types such as structs, enums and classes can conform to protocols}}
219+
AnyP(G {
220220
Text("hello")
221221
Label // expected-error {{generic parameter 'L' could not be inferred}}
222222
// expected-note@-1 {{explicitly specify the generic arguments to fix this issue}} {{10-10=<<#L: P#>>}}
@@ -509,7 +509,7 @@ enum E3 {
509509
}
510510

511511
func testCaseMutabilityMismatches(e: E3) {
512-
tuplify(true) { c in // expected-error {{generic parameter 'T' could not be inferred}}
512+
tuplify(true) { c in
513513
"testSwitch"
514514
switch e {
515515
case .a(let x, var y),
@@ -527,12 +527,13 @@ func testCaseMutabilityMismatches(e: E3) {
527527

528528
// Check for type equivalence among different case variables with the same name.
529529
func testCaseVarTypes(e: E3) {
530-
// FIXME: Terrible diagnostic
531-
tuplify(true) { c in // expected-error{{type of expression is ambiguous without more context}}
530+
tuplify(true) { c in
532531
"testSwitch"
533532
switch e {
534533
case .a(let x, let y),
535534
.c(let x, let y):
535+
// expected-error@-1 {{pattern variable bound to type 'String', expected type 'Int'}}
536+
// expected-error@-2 {{pattern variable bound to type 'Int', expected type 'String'}}
536537
x
537538
y + "a"
538539
}
@@ -659,14 +660,16 @@ struct MyView {
659660
}
660661

661662
@TupleBuilder var invalidCaseWithoutDot: some P {
663+
// expected-error@-1 {{return type of property 'invalidCaseWithoutDot' requires that 'Either<Int, Int>' conform to 'P'}}
664+
// expected-note@-2 {{opaque return type declared here}}
662665
switch Optional.some(1) {
663666
case none: 42 // expected-error {{cannot find 'none' in scope}}
664667
case .some(let x):
665668
0
666669
}
667670
}
668671

669-
@TupleBuilder var invalidConversion: Int { // expected-error {{cannot convert value of type 'String' to specified type 'Int'}}
672+
@TupleBuilder var invalidConversion: Int { // expected-error {{cannot convert return expression of type 'String' to return type 'Int'}}
670673
""
671674
}
672675
}
@@ -684,7 +687,7 @@ do {
684687
}
685688

686689
struct TuplifiedStructWithInvalidClosure {
687-
var condition: Bool
690+
var condition: Bool?
688691

689692
@TupleBuilder var unknownParameter: some Any {
690693
if let cond = condition {
@@ -697,7 +700,7 @@ struct TuplifiedStructWithInvalidClosure {
697700
}
698701

699702
@TupleBuilder var unknownResult: some Any {
700-
if let cond = condition {
703+
if let _ = condition {
701704
let _ = { () -> UnknownType in // expected-error {{cannot find type 'UnknownType' in scope}}
702705
}
703706
42
@@ -707,7 +710,7 @@ struct TuplifiedStructWithInvalidClosure {
707710
}
708711

709712
@TupleBuilder var multipleLevelsDeep: some Any {
710-
if let cond = condition {
713+
if let _ = condition {
711714
switch MyError.boom {
712715
case .boom:
713716
let _ = { () -> UnknownType in // expected-error {{cannot find type 'UnknownType' in scope}}
@@ -721,7 +724,7 @@ struct TuplifiedStructWithInvalidClosure {
721724
}
722725

723726
@TupleBuilder var errorsDiagnosedByParser: some Any {
724-
if let cond = condition {
727+
if let _ = condition {
725728
tuplify { _ in
726729
self. // expected-error {{expected member name following '.'}}
727730
}
@@ -759,9 +762,10 @@ func test_rdar65667992() {
759762
var entry: E
760763

761764
@Builder var body: S {
762-
switch entry { // expected-error {{type 'E' has no member 'unset'}}
765+
switch entry {
763766
case .set(_, _): S()
764-
case .unset(_): S() // expected-error {{'_' can only appear in a pattern or on the left side of an assignment}}
767+
case .unset(_): S() // expected-error {{type 'E' has no member 'unset'}}
768+
// expected-error@-1 {{'_' can only appear in a pattern or on the left side of an assignment}}
765769
default: S()
766770
}
767771
}
@@ -796,7 +800,9 @@ func test_missing_member_in_optional_context() {
796800
if let prop = test?.prop { // expected-error {{value of type 'Test' has no member 'prop'}}
797801
0
798802
}
803+
}
799804

805+
tuplify(true) { c in
800806
if let method = test?.method() { // expected-error {{value of type 'Test' has no member 'method'}}
801807
1
802808
}
@@ -850,10 +856,10 @@ func test_invalid_result_is_diagnosed() {
850856
}
851857
}
852858

853-
struct S<T> {} // expected-note {{arguments to generic parameter 'T' ('Int' and 'String') are expected to be equal}}
859+
struct S<T> {}
854860

855861
@MyBuilder
856-
func test() -> S<String> { // expected-error {{cannot convert result builder result type 'S<Int>' to return type 'S<String>}}
862+
func test() -> S<String> { // expected-error {{conflicting arguments to generic parameter 'T1' ('S<Int>' vs. 'S<String>')}}
857863
S<Int>()
858864
}
859865
}
@@ -887,6 +893,15 @@ func test_associated_values_dont_block_solver_when_unresolved() {
887893
// expected-note@-1 {{chain the optional using '?' to access member 'kind' only for non-'nil' base values}}
888894
// expected-note@-2 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
889895

896+
switch v.kind {
897+
case .a(_): "a"
898+
case .b: "b"
899+
}
900+
}
901+
902+
@Builder var switchError: String {
903+
let v = container.prop!.kind
904+
890905
switch v.kind { // expected-error {{value of type 'Value.Kind' has no member 'kind'}}
891906
case .a(_): "a"
892907
case .b: "b"

test/Constraints/result_builder_one_way.swift

Lines changed: 0 additions & 80 deletions
This file was deleted.

test/IDE/complete_in_result_builder.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func testGlobalLookup() {
3636
@TupleBuilder<String> var x1 {
3737
#^GLOBAL_LOOKUP^#
3838
// GLOBAL_LOOKUP: Begin completions
39-
// GLOBAL_LOOKUP: Decl[GlobalVar]/CurrModule/TypeRelation[Convertible]: MyConstantString[#String#];
39+
// GLOBAL_LOOKUP: Decl[GlobalVar]/CurrModule/TypeRelation[Convertible]: MyConstantString[#String#]; name=MyConstantString
4040
// GLOBAL_LOOKUP: End completions
4141
}
4242

@@ -81,6 +81,8 @@ func testStaticMemberLookup() {
8181
@TupleBuilder<String> var x1 {
8282
StringFactory.#^COMPLETE_STATIC_MEMBER^#
8383
// COMPLETE_STATIC_MEMBER: Begin completions
84+
// COMPLETE_STATIC_MEMBER: Keyword[self]/CurrNominal: self[#StringFactory.Type#]; name=self
85+
// COMPLETE_STATIC_MEMBER: Keyword/CurrNominal: Type[#StringFactory.Type#]; name=Type
8486
// COMPLETE_STATIC_MEMBER: Decl[StaticMethod]/CurrNominal/TypeRelation[Convertible]: makeString({#x: String#})[#String#];
8587
// COMPLETE_STATIC_MEMBER: End completions
8688
}

test/Sema/type_eraser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class TestResultBuilder {
8686
// CHECK: closure_expr type='() -> ConcreteP'
8787
takesBuilder {
8888
// CHECK: return_stmt
89-
// CHECK-NEXT: load_expr implicit type='ConcreteP'
89+
// CHECK-NEXT: call_expr implicit type='ConcreteP'
9090
ConcreteP()
9191
}
9292
}

test/StringProcessing/Sema/regex_builder_already_imported.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import RegexBuilder
44

55
extension Regex where Output == Substring {
6-
init(_ x: String) {} // expected-note {{'init(_:)' declared here}}
6+
init(_ x: String) {}
77
}
88

99
func foo() {
10-
// FIXME: This diagnostic could probably be better, it's not clear we should
11-
// be resolving to init(_ x: String) vs the result builder API and diagnosing
12-
// the fact that Int isn't a RegexComponent.
13-
_ = Regex { // expected-error {{trailing closure passed to parameter of type 'String' that does not accept a closure}}
14-
0
10+
_ = Regex {
11+
0 // expected-error {{static method 'buildExpression' requires that 'Int' conform to 'RegexComponent'}}
1512
}
1613
}

validation-test/IDE/issues_fixed/issue-57041.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ struct SplitView: View2 {
7272
}
7373

7474
// COMPLETE: Begin completions
75-
// COMPLETE-DAG: Decl[InstanceMethod]/Super: frame()[#Never#]; name=frame()
76-
// COMPLETE-DAG: Decl[InstanceMethod]/Super: frame({#width: Int?#}, {#height: Int?#})[#Never#]; name=frame(width:height:)
75+
// COMPLETE-DAG: Decl[InstanceMethod]/Super/TypeRelation[Convertible]: frame()[#Never#]; name=frame()
76+
// COMPLETE-DAG: Decl[InstanceMethod]/Super/TypeRelation[Convertible]: frame({#width: Int?#}, {#height: Int?#})[#Never#]; name=frame(width:height:)
7777
// COMPLETE: End completions

validation-test/Sema/SwiftUI/issue-56479.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ struct ContentView: View {
1616
DatePicker("Enter a date", selection: $date, displayedComponents: .date, in: Date())
1717
// expected-error@-1 {{argument 'in' must precede argument 'displayedComponents'}} {{78-90=}} {{52-52=in: Date(), }}
1818
DatePicker("Enter a date", selection: $date, displayedComponents: .date, in: Date() ... Date().addingTimeInterval(100))
19-
// expected-error@-1 {{argument 'in' must precede argument 'displayedComponents'}} {{78-125=}} {{52-52=in: Date() ... Date().addingTimeInterval(100), }}
2019
}
2120
}
2221
}

validation-test/Sema/SwiftUI/issue-56591.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import SwiftUI
99

1010
struct Experiment: View {
1111
var body: some View {
12-
HStack { // expected-error {{generic parameter 'Content' could not be inferred}} expected-note {{explicitly specify the generic arguments to fix this issue}}emacs
12+
HStack {
1313
Slider(value: <#T##Binding<BinaryFloatingPoint>#>, in: <#T##ClosedRange<BinaryFloatingPoint>#>, label: <#T##() -> _#>) // expected-error 3 {{editor placeholder in source file}}
1414
// expected-error@-1 {{type 'any BinaryFloatingPoint' cannot conform to 'Comparable'}}
1515
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
16+
// expected-error@-3 {{type 'any BinaryFloatingPoint' cannot conform to 'BinaryFloatingPoint'}}
17+
// expected-note@-4 {{only concrete types such as structs, enums and classes can conform to protocols}}
18+
// expected-note@-5 {{required by initializer 'init(value:in:label:onEditingChanged:)' where 'V' = 'any BinaryFloatingPoint'}}
1619
}
1720
}
1821
}

0 commit comments

Comments
 (0)