Skip to content
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

Result Builder is ignored when calling a protocol's static member with dot expression #70649

Open
omochi opened this issue Dec 29, 2023 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@omochi
Copy link
Collaborator

omochi commented Dec 29, 2023

Description

The Result Builder is being ignored when invoking a protocol's static member using dot expression.

Reproduction

Below is the reproduction code. First, the setup:

protocol P {}

extension Int: P {}

func useP(_ p: some P) {}

struct S: P {
    init(@SBuilder builder: () -> [any P]) {
        self.ps = builder()
    }

    var ps: [any P]
}

@resultBuilder
struct SBuilder {
    static func buildBlock(_ components: (any P)...) -> [any P] {
        components
    }
}

extension P where Self == S {
    static func s(@SBuilder builder: () -> [any P]) -> Self {
        S(builder: builder)
    }
}

At this point, the following code should work:

// OK
useP(
    S.s {
        1
        2
        3
    }
)

However, when using dot expression as shown below, it results in a compilation error.

// compile error
useP(
    .s {
        1
        2
        3
    }
)

The error message is as follows:

/Users/omochi/work/Sandbox/main.swift:48:9: warning: integer literal is unused
        1
        ^
/Users/omochi/work/Sandbox/main.swift:49:9: warning: integer literal is unused
        2
        ^
/Users/omochi/work/Sandbox/main.swift:50:9: warning: integer literal is unused
        3
        ^
/Users/omochi/work/Sandbox/main.swift:51:5: error: missing return in closure expected to return '[any P]'
    }
    ^

It seems that the @SBuilder Result Builder specified for the builder parameter in function s is being ignored and is being treated as a regular closure.

Expected behavior

When writing .s, it should compile the same way as if S.s was written.

Environment

macOS: Sonoma 14.1(23B74)
Xcode: 15.2 beta (15C5500c)
swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0

Additional information

No response

@omochi omochi added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Dec 29, 2023
@xedin
Copy link
Member

xedin commented Dec 29, 2023

I think we might have a dupe for this already, I remember seeing something very similar I will double-check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants