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

Swift crashes when a class conforms to a protocol with associated type as `AnyObject`

    XMLWordPrintable

    Details

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

      Description

      Summary

      Within Xcode (or Playground), when creating a new class which conforms to a generic protocol with associatedtype with a type constraint, defining the typealias with a protocol composition causes the compiler to crash.

      I couldn't reproduce this in REPL (see attached screenshot).

      How to reproduce?

      Tested with Xcode 9.2, i. e. toolchain for Swift 4.0.3 Release

      protocol Observable {
      	associatedtype Observer: AnyObject
      
      	func addObserver(_ observer: Observer)
      }
      
      // MARK: - Default Implementation
      
      extension Observable {
      
      	func addObserver(_ observer: Observer) {
      		print("Observer added!")
      	}
      }
      
      // MARK: - Some Delegate
      
      protocol SomeDelegate: class {
      
      	func someFunc(_ someClass: SomeClass)
      }
      
      // MARK: - Some Class
      
      class SomeClass: NSObject, Observable {
      
      	typealias Observer = SomeDelegate & AnyObject
      }
      

      Error

      Command failed due to signal: Abort trap: 6

      Stack Trace

      Assertion failed: (expectedSchema.size() == IGF.IGM.getExplosionSize(applyContextArchetypes(IGF, substTy))), function reemitAsUnsubstituted, file /Users/buildnode/jenkins/workspace/oss-swift-4.0-package-osx/swift/lib/IRGen/GenPoly.cpp, line 66.
      0  swift                    0x000000010f527f38 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
      1  swift                    0x000000010f526e96 llvm::sys::RunSignalHandlers() + 86
      2  swift                    0x000000010f5284fe SignalHandler(int) + 366
      3  libsystem_platform.dylib 0x00007fff65999f5a _sigtramp + 26
      4  libsystem_platform.dylib 000000000000000000 _sigtramp + 2590400704
      5  libsystem_c.dylib        0x00007fff657371ae abort + 127
      6  libsystem_c.dylib        0x00007fff656ff1ac basename_r + 0
      7  swift                    0x000000010c5b7962 swift::irgen::reemitAsUnsubstituted(swift::irgen::IRGenFunction&, swift::SILType, swift::SILType, swift::irgen::Explosion&, swift::irgen::Explosion&) + 658
      8  swift                    0x000000010c62ebd6 emitApplyArgument((anonymous namespace)::IRGenSILFunction&, swift::SILValue, swift::SILType, swift::irgen::Explosion&) + 294
      9  swift                    0x000000010c62dfa3 (anonymous namespace)::IRGenSILFunction::visitFullApplySite(swift::FullApplySite) + 2035
      10 swift                    0x000000010c616e92 (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 7330
      11 swift                    0x000000010c614cfe swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1358
      12 swift                    0x000000010c5306bb swift::irgen::IRGenerator::emitLazyDefinitions() + 987
      13 swift                    0x000000010c5f25d6 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**, unsigned int) + 1350
      14 swift                    0x000000010c5f2b46 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, llvm::LLVMContext&, unsigned int, llvm::GlobalVariable**) + 86
      15 swift                    0x000000010c4b6825 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 12533
      16 swift                    0x000000010c4b2845 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3493
      17 swift                    0x000000010c474950 main + 3312
      18 libdyld.dylib            0x00007fff6568b015 start + 1
      
      1.	While emitting IR SIL function "@_T07TestBug9SomeClassCAA10ObservableA2aDP11addObservery0G0QzFTW".
       for 'addObserver(_:)' at /Users/iuliancorcoja/Desktop/TestBug/TestBug/TestBug/main.swift:19:2
      

      The exact same code though, doesn't cause the compiler to crash in the latest Xcode (Xcode 9.3, Swift 4.1 Release). Instead, I get the following error when compiling:

      Error

      Type 'SomeClass' does not conform to protocol 'Observable'
      Possibly intended match 'SomeClass.Observer' (aka 'SomeDelegate & AnyObject') does not conform to 'AnyObject'

      It doesn't really make sense since SomeClass.Observer does conform to AnyObject.


      Remark

      Changing the parameter in the addObserver(_:) method of the Observable protocol from Observer to anything else, e. g. AnyObject, doesn't throw any errors in Swift 4.0.3, but throws the same error as above in Swift 4.1

      Error

      Type 'SomeClass' does not conform to protocol 'Observable'
      Possibly intended match 'SomeClass.Observer' (aka 'SomeDelegate & AnyObject') does not conform to 'AnyObject'

      Code

      protocol Observable {
      	associatedtype Observer: AnyObject
      
      	func addObserver(_ observer: AnyObject)
      }
      
      extension Observable {
      
      	func addObserver(_ observer: AnyObject) {
      		print("Observer added!")
      	}
      }
      
      protocol SomeDelegate: class {
      
      	func someFunc(_ someClass: SomeClass)
      }
      
      class SomeClass: NSObject, Observable {
      
      	typealias Observer = SomeDelegate & AnyObject
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              iulian.corcoja Iulian Corcoja
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: