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-8549] Generic method fails to return type alias #51067

Open
swift-ci opened this issue Aug 16, 2018 · 4 comments
Open

[SR-8549] Generic method fails to return type alias #51067

swift-ci opened this issue Aug 16, 2018 · 4 comments
Assignees
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

Previous ID SR-8549
Radar rdar://problem/43388576
Original Reporter tomquist (JIRA User)
Type Bug
Environment

Xcode Version 9.4.1 (9F2000)
Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0

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

md5: 898e1db3a7ddbaf8770f56bbd4657549

Issue Description:

The following code fails to compile because the generic method returns an object of type "SomeClass" instead of "SomeAlias". This seems to be a bug because the block passed into the method returns "SomeAlias" and hence "genericMethod" should also return "SomeAlias".

protocol SomeProtocol {}
class SomeClass {}
class SomeSubClass : SomeClass, SomeProtocol {}
typealias SomeAlias = SomeClass & SomeProtocol
typealias SomeAliasFactory = () -> SomeAlias

func genericMethod<T: SomeClass>(block: () -> T) -> T {
    return block()
}

let factory: SomeAliasFactory = SomeSubClass.init
let object: SomeAlias = genericMethod(block: factory)

Compiler error:
12:25: Value of type 'SomeClass' does not conform to specified type 'SomeAlias' (aka 'SomeClass & SomeProtocol')

@belkadan
Copy link
Contributor

That error message isn't very good, but it's true that SomeClass & SomeProtocol doesn't have a representation compatible with T: SomeClass. In the former case, the details of how the type conforms to SomeProtocol need to be stored with the value, and the implementation of genericMethod won't have been compiled to do that. (@slavapestov, is that essentially correct?)

Keeping this open to improve the diagnostic (cc @xedin)

@xedin
Copy link
Member

xedin commented Aug 16, 2018

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Tom Quist (JIRA)

@belkadan I'm not sure whether I could follow your explanation. From a type theory point of view "SomeClass & SomeProtocol" is a more concrete representation of "SomeClass" so I don't get why this is not be compatible with "T: SomeClass". Is this a technical limitation of the current implementation or am I missing something? Would that be something which could be made possible in future, e.g. by a swift-evolution proposal?

@belkadan
Copy link
Contributor

I picked the word "representation" carefully. SomeClass & SomeProtocol is a more specific type, yes, but it also requires two pointers' worth of storage rather than just one. I think it would be possible for the compiler to generate a wrapper function around genericMethod to handle this, but then we'd have a difference between generic functions (which can get this kind of wrapper) and generic types (which can't). Still, that could be worth it.

(Or we could give up the optimizations that rely on T: SomeClass being known to have a single pointer representation. I'm not sure if that's a realistic option, though—that is, I don't know how often these optimizations kick in.)

@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

3 participants