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

Runtime crash when calling generic structs with `unowned` properties

    XMLWordPrintable

    Details

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

      Xcode 10.2 beta 3 (10P99q)
      Swift compiler swiftlang-1001.0.63.8 clang-1001.0.44

      Description

      The following code reliably crashes at runtime:

      protocol BarProtocol {}
      
      extension BarProtocol {
          var str: String { return "str" }
      }
      
      struct Foo<T: AnyObject>: BarProtocol {
          unowned let bar: T
      }
      
      class Bar {
          let str2 = "str2"
      
          var foo: BarProtocol {
              return Foo(bar: self)
          }
      }
      
      _ = Bar().foo.str
      

      with the following stack trace:

      0  swift                    0x000000010f514efa PrintStackTraceSignalHandler(void*) + 42
      1  swift                    0x000000010f5146dc SignalHandler(int) + 348
      2  libsystem_platform.dylib 0x00007fff7c49cb3d _sigtramp + 29
      3  libsystem_platform.dylib 0x0000000000000001 _sigtramp + 2209756385
      4  libobjc.A.dylib          0x00007fff7b1dcfaa objc_copyWeak + 18
      5  libswiftCore.dylib       0x000000011202be3c swift_unknownObjectUnownedCopyInit + 76
      6  libswiftCore.dylib       0x0000000110c94365 swift_unknownObjectUnownedCopyInit + 4274423157
      7  libswiftCore.dylib       0x0000000110c940a3 swift_unknownObjectUnownedCopyInit + 4274422451
      8  swift                    0x000000010bd9d13d llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 365
      9  swift                    0x000000010bda37fc llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, char const* const*) + 1004
      10 swift                    0x000000010b3867b1 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 54977
      11 swift                    0x000000010b375ac7 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6823
      12 swift                    0x000000010b31590d main + 1261
      13 libdyld.dylib            0x00007fff7c2b1ed9 start + 1
      

      Ways to make the crash go away:

      • Remove `Bar.str2` (the entire property)
      • Make the struct non-generic
      • Change the struct to a class
      • Removed `unowned` (to weak or strong)

      We have seen a handful of reports of this happening with current tooling (Xcode 10.1) as well, but can't reliably reproduce.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            scott Scott Berrevoets
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated: