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

With optimization on, type(of:) returns protocol instead of concrete type

    XMLWordPrintable

    Details

      Description

      1. The following code gives different results depending on whether optimization is turned on:
      protocol P { }
      class C: P { }
      
      struct S<T> {
          init(val: T) {
              print(type(of: val))
              print(type(of: val) is AnyObject.Type)
          }
      }
      
      _ = S<P>(val: C())
      

      With an unoptimized build, it prints:

      C
      true
      

      However, in an optimized build (release configuration in Xcode), it prints:

      P
      false
      

      The optimization discrepancy is definitely a bug. I think the unoptimized output is the correct one — type(of:​) should always return a concrete type, never a protocol … right?

      (Apologies if this is a duplicate. I found it a difficult one to search for.)

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              pcantrell Paul Cantrell
              Reporter:
              pcantrell Paul Cantrell
              Votes:
              2 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: