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

Domain of NSErrors created from Swift ErrorTypes sometimes don't match synthesized Domain constant

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
    • Environment:

      Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81)
      Target: x86_64-apple-darwin15.3.0

      Apple Swift version 2.2 (swiftlang-703.0.6.5 clang-703.0.21)
      Target: x86_64-apple-macosx10.9

      Description

      When writing a Swift enum like

      @objc enum MyError: Int, ErrorType {
          case A, B
      }
      

      Given a module name "Foo", Swift synthesizes the following ObjC declaration:

      typedef SWIFT_ENUM(NSInteger, MyError) {
          MyErrorA = 0,
          MyErrorB = 1,
      }
      static NSString * _Nonnull const MyErrorDomain = @"Foo.MyError";
      

      In an Xcode project, this works perfectly fine. Casting MyError.A to NSError and looking at the domain property yields the string "Foo.MyError".

      However, for some reason, when testing this at the command-line with a manual invocation of swiftc, the bridged NSError loses the module name in the domain string, so it just has "MyError". I've tested this both in an executable context (e.g. swiftc -module-name Foo -emit-objc-header foo.swift main.swift where foo.swift defines the error and main.swift prints it out) and in a library context (e.g. compiling foo.swift as a library, and then linking to it from a separate compilation of main.swift that imports the Foo module). I don't know what's different about an Xcode project that causes the error domain to work.


      // foo.swift
      import Foundation
      
      @objc enum MyError: Int, ErrorType {
          case A, B
      }
      
      // main.swift
      import Foundation
      
      print("type: \(MyError.self)")
      print("debugType: \(String(reflecting: MyError.self))")
      print("error.domain: \((MyError.A as NSError).domain)")
      

      Reproduction:

      > swiftc -module-name Foo -emit-objc-header foo.swift main.swift
      > ./Foo
      type: MyError
      debugType: Foo.MyError
      error.domain: MyError
      > grep MyErrorDomain Foo.h
      static NSString * _Nonnull const MyErrorDomain = @"Foo.MyError";
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              lily Lily Ballard
              Reporter:
              lily Lily Ballard
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: