Uploaded image for project: 'Swift'
  1. Swift
  2. SR-8697

Xcode 10 missing type specifier when converting Swift to objc

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
    • Environment:

      Xcode 10 beta 6, using Swift 4

      Description

      Apologies in advance if this doesn't belong here, but I wanted to create the issue just in case. I also added the 'Tooling' flag to this but i presume that's wrong.

      When creating a Swift class with a function annotated with @objc, and that function receives as a parameter a generic associated type that will translate into an objc primitive, the conversion in the *-Swift.h file has nullability specifiers that prevent it from compiling.

      Steps to Reproduce:

      Given the following setup:

      public struct Identifier<T, RawIdentifier> : Hashable, RawRepresentable, Codable where RawIdentifier : Decodable, RawIdentifier : Encodable, RawIdentifier : Hashable {
          /// Identifier's raw value.
          public let rawValue: RawIdentifier 
          /// Creates a new `Identifier`.
          public init(rawValue: RawIdentifier) {
              self.rawValue = rawValue
          }
      }
      
      public protocol Identifiable {
          associatedtype RawIdentifier: Hashable & Codable = UInt64
          var id: Identifier<Self, RawIdentifier> { get }
      }
      
      public struct Member: Identifiable {
          public var id: Identifier<Member, UInt64>
      }
      
      public typealias MemberID = Identifier<Member, UInt64>

      When we create a class that exposes an @objc convenience initializer like:

      class Tab {}
      
      extension Tab {
          /// Convenience ObjC initializer.
          @objc public convenience init(memberID: MemberID.RawValue, title: String) {
              // Implementation of this method
          }
      }

      The generated *-Swift.h file contains the following method signature:

      - (nonnull instancetype)initWithMemberID:( _Nonnull)memberID title:(NSString * _Nonnull)title;

      Which obviously doesn't compile because it's missing the type specifier:

       

      warning: type specifier missing, defaults to 'int'
      

      and that won't compile either because you can't apply _Nonnull to a primitive type.

      Expected Results: The method will be translated to objc successfully (in this case it would be something like:

       

      - (nonnull instancetype)initWithMemberID:(int64_t)memberID title:(NSString * _Nonnull)title;
      

      Actual Results:

      The generated *-Swift.h file contains the following method signature:

       

      - (nonnull instancetype)initWithMemberID:( _Nonnull)memberID title:(NSString * _Nonnull)title; 
      

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              kerrmarin Kerr Marin Miller
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: