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

PropertyWrappers: Compiler crashes (trap 6) when used with initial value + default param

    XMLWordPrintable

    Details

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

      Xcode 11 GM Seeds 1 and 2

      Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
      Target: x86_64-apple-darwin18.7.0

      Description

      Given the following Property Wrapper

      import Foundation
      
      @propertyWrapper
      struct UserDefault<T> {
          let key: String
          let defaultValue: T
          let suite: UserDefaults
      
          var wrappedValue: T {
              get { suite.object(forKey: key) as? T ?? defaultValue }
              nonmutating set { suite.set(newValue, forKey: key) }
          }
      
          init(wrappedValue: T, key: String, suite: UserDefaults = .standard) {
              self.suite = .standard
              self.key = key
              self.defaultValue = wrappedValue
          }
      }
      

      There are theorically multiple ways to declare and initialise a property annotated with such a wrapper. The first 3 work great, but the last one make the compiler crash:

      class User {
          @UserDefault(wrappedValue: "default1", key: "key1", suite: .standard)
          var value1: String
      
          @UserDefault(wrappedValue: "default2", key: "key2")
          var value2: String
      
          @UserDefault(key: "key3", suite: .standard)
          var value3: String = "default3"
      
          @UserDefault(key: "key4") // trap: 6. Commenting out that line to stop crashing
          var value4: String = "default4"
      }
      

      When compiling this code with Xcode 11GM, the 4th variant – which takes advantage both of having the = "default4" to be translated into a wrappedValue: "default4" first parameter to the init, and omits the last parameter in hope of using the default value – makes the compiler crash with the following dump:

      $ DEVELOPER_DIR=/Applications/Xcode-11GM.app swift Contents.swift 
      swift(83722,0x124d8c5c0) malloc: *** error for object 0x7ffedfd769a0: pointer being realloc'd was not allocated
      swift(83722,0x124d8c5c0) malloc: *** set a breakpoint in malloc_error_break to debug
      Stack dump:
      0.	Program arguments: /Applications/Xcode-11GM.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret Contents.swift -enable-objc-interop -sdk /Applications/Xcode-11GM.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name Contents 
      1.	While emitting IR SIL function "@$s8Contents4UserCACycfc".
       for 'init()' (at Contents.swift:23:7)
      0  swift                    0x00000001142cbeb3 PrintStackTraceSignalHandler(void*) + 51
      1  swift                    0x00000001142cb686 SignalHandler(int) + 358
      2  libsystem_platform.dylib 0x00007fff69a9db5d _sigtramp + 29
      3  libsystem_platform.dylib 0x00007ffedfd75598 _sigtramp + 1982691928
      4  libsystem_c.dylib        0x00007fff699576a6 abort + 127
      5  libsystem_malloc.dylib   0x00007fff69a66077 malloc_vreport + 545
      6  libsystem_malloc.dylib   0x00007fff69a65e38 malloc_report + 151
      7  libsystem_malloc.dylib   0x00007fff69a629b6 realloc + 313
      8  swift                    0x000000010fff182d swift::irgen::SingleScalarTypeInfo<(anonymous namespace)::ClassTypeInfo, swift::irgen::ReferenceTypeInfo>::getSchema(swift::irgen::ExplosionSchema&) const + 157
      9  swift                    0x00000001101659be swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 35646
      10 swift                    0x000000011015a30a swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 9866
      11 swift                    0x00000001100096f7 swift::irgen::IRGenerator::emitLazyDefinitions() + 9303
      12 swift                    0x0000000110137100 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1344
      13 swift                    0x0000000110134809 swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::GlobalVariable**) + 825
      14 swift                    0x000000010ff238fb performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 37051
      15 swift                    0x000000010ff17034 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
      16 swift                    0x000000010fea45a3 main + 1219
      17 libdyld.dylib            0x00007fff698b23d5 start + 1
      [1]    83722 abort      DEVELOPER_DIR=/Applications/Xcode-11GM.app swift Contents.swift
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                AliSoftware Olivier Halligon
              • Votes:
                1 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: