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-10592] ExpressibleByArrayLiteral won't compile on type nested in a generic type. #52992

Open
swift-ci opened this issue May 1, 2019 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 1, 2019

Previous ID SR-10592
Radar None
Original Reporter Jaap (JIRA User)
Type Bug
Environment

MacOS 10.14.4

Xcode and command line tools 10.2.1

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

md5: f27798e55c6f49438c30d3b4a0ac8307

Issue Description:

I'm not able to use the ExpressibleByArrayLiteral Protocol when applying it to a nested type that has a parent type that works with a generic type. Some example code:

struct Dimension {
    var value: Int
}


extension Dimension: ExpressibleByIntegerLiteral {
    init(integerLiteral value: Int) {
        self.value = value
    }
}


struct Tensor<Element> {
    struct Shape {
        var dimensions: [Dimension]
    }
}


extension Tensor.Shape: ExpressibleByArrayLiteral {
    init(arrayLiteral elements: Dimension...) {
        self.dimensions = elements
    }
}


let shape: Tensor.Shape = [1,1,1]

The last line won't compile, giving the error:

Cannot convert value of type '[Int]' to specified type 'Tensor.Shape'

When removing the generic type <Element> from the Tensor struct, everything compiles.

Could be that I'm missing something, but I think this should just work right?

@belkadan
Copy link
Contributor

belkadan commented May 1, 2019

It's the wrong error message, but the problem is that you haven't specified the type of Element for shape. Is it a Tensor<Int>.Shape or a Tensor<Float>.Shape?

Keeping this for the error message.

@belkadan
Copy link
Contributor

belkadan commented May 1, 2019

cc @xedin

@swift-ci
Copy link
Collaborator Author

swift-ci commented May 1, 2019

Comment by Jaap Wijnen (JIRA)

But the type of Element, isn’t needed for the initialization of the shape object as Dimension is just a wrapper around an Int. Any way to circumvent this then?

@belkadan
Copy link
Contributor

belkadan commented May 1, 2019

That's not how generics work in Swift. Tensor<Int>.Shape and Tensor<Float>.Shape are different types, and the compiler needs to know which one you're trying to make here. That's what it means to nest a type inside a generic type; if you don't want that behavior, you have to not nest it.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants