Skip to content

[CSBindings] Prefer conjunctions over closure variables without bindings #67441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2023
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
7 changes: 7 additions & 0 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,13 @@ bool BindingSet::favoredOverConjunction(Constraint *conjunction) const {
if (locator->directlyAt<ClosureExpr>()) {
auto *closure = castToExpr<ClosureExpr>(locator->getAnchor());

// If there are no bindings for the closure yet we cannot prioritize
// it because that runs into risk of missing a result builder transform.
if (TypeVar->getImpl().isClosureType()) {
if (Bindings.empty())
return false;
}

if (auto transform = CS.getAppliedResultBuilderTransform(closure)) {
// Conjunctions that represent closures with result builder transformed
// bodies could be attempted right after their resolution if they meet
Expand Down
85 changes: 85 additions & 0 deletions test/Constraints/issue67363.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// RUN: %target-typecheck-verify-swift -disable-availability-checking

// https://github.com/apple/swift/issues/67363

protocol UIView {
init()
}

class UILabel : UIView {
required init() {}
}

class UIStackView : UIView {
required init() {}
}

protocol ViewRepresentable {
associatedtype View: UIView
func configure(view: View)
}

struct StyledString: ViewRepresentable {
let content: String
func configure(view: UILabel) {}
}

class StackViewOne<First: UIView>: UIStackView {
var first = First()
}

struct Stack {
struct One<First: ViewRepresentable>: ViewRepresentable {
let first: First
func configure(view: StackViewOne<First.View>) {
first.configure(view: view.first)
}
}

@resultBuilder
enum Builder {
static func buildBlock<First: ViewRepresentable>(_ first: First) -> Stack.One<First> {
Stack.One(first: first)
}
}

static func vertical<StackType: ViewRepresentable>(@Builder build builder: () -> StackType) -> StackType {
builder()
}
}

struct ListItem {
let body: any ViewRepresentable
}

@resultBuilder
enum ListBuilder {
static func buildExpression<View: ViewRepresentable>(_ expression: View?) -> [ListItem?] {
[expression.map { .init(body: $0) }]
}

static func buildBlock(_ components: [ListItem?]...) -> [ListItem] {
components.flatMap { $0.compactMap { $0 } }
}
}

struct WithFooter<T: ViewRepresentable>: ViewRepresentable {
let body: T
let footer: () -> [ListItem]
func configure(view: T.View) {}
}

extension ViewRepresentable {
func withFooter(@ListBuilder build: @escaping () -> [ListItem]) -> WithFooter<Self> {
.init(body: self, footer: build)
}
}

func testThatResultBuilderIsAppliedToWithFooterArgument() -> some ViewRepresentable {
Stack.vertical() {
StyledString(content: "vertical")
}
.withFooter {
StyledString(content: "footer")
}
}
24 changes: 24 additions & 0 deletions test/expr/closure/multi_statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,27 @@ func test_recursive_var_reference_in_multistatement_closure() {
}
}
}

// https://github.com/apple/swift/issues/67363
func test_result_builder_in_member_chaining() {
@resultBuilder
struct Builder {
static func buildBlock<T>(_: T) -> Int { 42 }
}

struct Test {
static func test<T>(fn: () -> T) -> T {
fn()
}

func builder(@Builder _: () -> Int) {}
}

Test.test {
let test = Test()
return test
}.builder { // Ok
let result = ""
result
}
}