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

Mirror reflection returns zero children for nested single-case enum

    XMLWordPrintable

    Details

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

      Swift 5.2

      Description

      If you nest a single-case enum in another enum and try to reflect on the outer enum case, you cannot reflect on this inner value. For example, the following prints an empty array:

      enum Inner

      { case innermost }

      enum Outer

      { case inner(Inner) }
      print(Array(Mirror(reflecting: Outer.inner(.innermost)).children))
      // prints "[]"

      On the other hand, adding a case to the single-case enum restores the ability to reflect on it:

      enum Inner { case innermost, innermost2 }
      enum Outer { case inner(Inner) }

      print(Array(Mirror(reflecting: Outer.inner(.innermost)).children))
      // prints "[(label: Optional("inner"), value: Inner.innermost)]"

      My current workaround involves checking the memory layout size of Inner and if it's zero then I unsafely bitcast a value of Void.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            stephencelis Stephen Celis
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: