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

Crash only on iOS when setting subscript for non-final class in other SwiftPM module

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Environment:

      Description

      I've hit an issue where a custom subscript I defined on a type in one of my Swift packages is causing a crash when running on iOS, but not on macOS (either with Catalyst or not).

      I've attached a minimal reproduction package, which only shows the issue when running the tests in Xcode on an iOS simulator or device. In my real use case, this issue reproduces in both test cases and when using the package from an app.

      I've reduced it down to a class shaped something like this:

      public class Crashy<Friend> {
          public init() {}
      
          private var foo: String = "bar"
      
          var data: [String: Any] = [:]
          public subscript(key: String) -> Any? {
              get { data[key] }
              set {
                  data[key] = newValue
              }
          }
      }
      

      The crash is triggered by creating a new instance and setting the subscript with any key/value.

      The following details I've found to be important (removing any of them makes the crash go away):

      • The class must be generic
      • The class must have a private variable
      • The class must be non-final
      • The class must live in a module of a Swift package (the same use case with a class in an iOS framework linked by an app does not reproduce the issue)

      I've seen the crash on a stack frame like this:

      #0 0x00000001060a1099 in method descriptor for UserActions.Context.subscript.modify

      In the attached test case, it's much weirder, as it's referencing a different type than the one the subscript is being called on:

      #0 0x0000000102a3aba8 in direct field offset for NotCrashy.data ()

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                mjm Matt Moriarity
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated: