Skip to content

[SR-7412] Swift crashes when a class conforms to a protocol with associated type as AnyObject #49955

@swift-ci

Description

@swift-ci
Previous ID SR-7412
Radar None
Original Reporter iulian.corcoja (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

System Version: macOS 10.13.4 (17E199)

Model Name: MacBook Pro

Model Identifier: MacBookPro11,5

Xcode 9.2:

Version: 9.2

Obtained from: Apple

Xcode:

Version: 9.3

Obtained from: Mac App Store

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 88a96e0fa13427206c7a2339f609d60d

duplicates:

  • SR-55 non-@objc protocol existentials do not conform to their own protocol type

Issue 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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions