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

[SR-12259] Function Builders with types conforming to ExpressibleBy... #54687

Closed
swift-ci opened this issue Feb 24, 2020 · 5 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself result builders Feature: Result builders type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12259
Radar rdar://problem/59740177
Original Reporter mkao (JIRA User)
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Environment

Xcode 11.3 Playground / Swift 5.1.3

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, FunctionBuilder, TypeChecker
Assignee None
Priority Medium

md5: f3685517f91c2b32760fd3296922b4f3

Issue Description:

When using a type conforming to ExpressibleByStringLiteral within the FunctionBuilder closure, the compiler seems to have issues.

Given the following type:

public enum Foo: ExpressibleByStringLiteral {
    case string(String)
    case other

    public init(stringLiteral value: String) {
        self = .string(value)
    }
}

And using the function builder:

@_functionBuilder
public class SomeBuilder {
    public static func buildBlock(_ values: Foo...) -> [Foo] {
        values
    }
}

public func call(@SomeBuilder values: () -> [Foo]) -> [Foo] {
    values()
}

public func call(@SomeBuilder values: () -> Foo) -> [Foo] {
    [values()]
}

I am able to get this working:

call {
    "foo"
}

But when adding another String, the compiler throws an error:

call {
    "foo"
    "bar" // Cannot invoke 'call' with an argument list of type '(@escaping () -> ())'
}

I could reproduce the same behavior when using ExpressibleByIntegerLiteral.

@typesanitizer
Copy link

@swift-ci create

@theblixguy
Copy link
Collaborator

On master:

/Users/suyashsrijan/Desktop/test.swift:26:3: error: cannot convert value of type 'String' to expected argument type 'Foo'
  "foo"
  ^
/Users/suyashsrijan/Desktop/test.swift:27:3: error: cannot convert value of type 'String' to expected argument type 'Foo'
  "bar"
  ^

@theblixguy
Copy link
Collaborator

The error seems to go away when you insert a cast:

call { 
  "foo" as Foo
  "bar" as Foo
}

@swift-ci
Copy link
Collaborator Author

swift-ci commented Mar 1, 2020

Comment by Michael Kao (JIRA)

Ah ok, haven't tried that. But isn't that something the compiler should be able to figure out?

@DougGregor
Copy link
Member

This is intended behavior. Add

static func buildExpression(_ value: Foo) -> Foo { value }

to your function builder to affect how the expressions are type checked. See https://github.com/DougGregor/swift-evolution/blob/function-builders/proposals/XXXX-function-builders.md for more information.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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. compiler The Swift compiler in itself result builders Feature: Result builders type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants