Is this a `typealias` issue?

Consider the following code:

public typealias Something = Int
public func something() -> Something {
  return 42
}

public typealias Test<T> = T
public func test() -> Test<Int> {
  return 42
}

When viewing the imported module interface the result will become:

public typealias Something = Int
public func something() -> Something

public typealias Test<T> = T
public func test() -> Int 

However I did expect public func test() -> Test<Int>, because the typealias is just a visual marker in that case.

1 Like

At one point we didn't have the information necessary to properly preserve generic typealiases post-type-checking, but I think we do now. @Slava_Pestov?

1 Like

I cannot test it on my current machine, but on the other one with Xcode 10 beta 6 it was still the same. So I guess this has not landed in the Swift 4.2 branch, if it was already fixed?

If the issue is unknown then I can file a bug.

I tested on Xcode 10 beta 6 and I see the following output:

public typealias Something = Int
public func something() -> Something
public typealias Test<T> = T
public func test() -> Test<Int>

The steps I followed:

  • Create a new Cocoa project
  • Paste your code in AppDelegate.swift
  • Switch to the generated interface view

I also checked and @Douglas_Gregor's changes landed in the swift-4.2-branch -- https://github.com/apple/swift/commit/e82e7ee908ed5ceb3c3dcd36a36563aa4b307a23

It is possible that something else is going wrong here. Can you share more details about what you're doing?

Ah, the generated interface view doesn't go through serialization.

I didn't necessarily mean that we'd fixed the issue yet, just that we had the pieces in place to do so. I don't think we actually changed Serialization to not look through bound generic typealiases.

I took a quick look at Serialization.cpp and we do serialize NameAliasType.

Turns out the problem was in deserialization. I have a fix: Serialization: Fix deserialization of generic typealiases by slavapestov · Pull Request #19253 · apple/swift · GitHub

4 Likes

Here are the steps where I can always reproduce it in Xcode 10 beta 6 (it uses the default toolchain):

  • create a playground project
  • add a new swift file to Sources
  • add this short code
    public typealias Test<T> = T
    public func test() -> Test<Int> {
      return 42
    }
    
  • go back to the main playground page
  • type <playground name>_Sources
  • cmd + left click on that identifier
  • click on Jump to Definition
  • the result will be:
    public typealias Test<T> = T
    public func test() -> Int
    

Thank you for the quick help and the blazing fast PR. Hopefully it will make into Swift 4.2.x if not in 4.2 which is probably too late.

The fix is not going into swift-4.2-branch, sorry. It changes the serialization format and that in itself introduces a bit of risk.