swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.59k stars 10.37k forks source link

[SR-3326] Crash on master "SIL verification failed: substituted callee type does not match substitutions" #45914

Closed swift-ci closed 7 years ago

swift-ci commented 7 years ago
Previous ID SR-3326
Radar None
Original Reporter Hendrik (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment Occured on current master (commit 420f94c) With installed Xcode-beta 8.2 (8C30a)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash, SILGen | |Assignee | @slavapestov | |Priority | Medium | md5: 38f4f902de9d9e5bcab5c66b30f138b2

relates to:

Issue Description:

Occured on current master (commit 420f94c).
The following code

extension Collection where Self.Iterator.Element: Equatable {
    func count<T: Equatable>(of element: T) -> Int where T == Self.Iterator.Element {
        return self.reduce(0) {
            sum, e in
            let isSame: Int = (e == element ? 1 : 0)
            return sum + isSame
        }
    }
}

produces the following stack-trace:

SIL verification failed: substituted callee type does not match substitutions
  $@convention(thin) (@in Int, @in τ_1_0, @owned @callee_owned (Int, @in τ_1_0) -> (Int, @error Error)) -> (@out Int, @error Error)
  $@convention(thin) (@in Int, @in Self.Iterator.Element, @owned @callee_owned (Int, @in Self.Iterator.Element) -> (Int, @error Error)) -> (@out Int, @error Error)
Verifying instruction:
     %13 = convert_function %12 : $@callee_owned (Int, @in Self.Iterator.Element) -> Int to $@callee_owned (Int, @in Self.Iterator.Element) -> (Int, @error Error), loc "/tmp/swiftcrash.swift":3:25, scope 0 // user: %15
     // function_ref thunk
  %14 = function_ref @_TTRG__Rxs10Collectiond__zWx8Iterator7Element_WxS0_S1__s9EquatablerXFo_dSiiqd___dSizoPs5Error__XFo_iSiiqd___iSizoPS3___ : $@convention(thin) <τ_0_0 where τ_0_0 : Collection, τ_0_0.Iterator.Element : Equatable><τ_1_0 where τ_1_0 == τ_0_0.Iterator.Element> (@in Int, @in τ_1_0, @owned @callee_owned (Int, @in τ_1_0) -> (Int, @error Error)) -> (@out Int, @error Error), loc "/tmp/swiftcrash.swift":3:25, scope 0 // user: %15
->   %15 = partial_apply %14<Self, Self.Iterator.Element>(%13) : $@convention(thin) <τ_0_0 where τ_0_0 : Collection, τ_0_0.Iterator.Element : Equatable><τ_1_0 where τ_1_0 == τ_0_0.Iterator.Element> (@in Int, @in τ_1_0, @owned @callee_owned (Int, @in τ_1_0) -> (Int, @error Error)) -> (@out Int, @error Error), loc "/tmp/swiftcrash.swift":3:25, scope 0 // user: %17
     try_apply %4<Self, Int>(%16, %9, %15, %1) : $@convention(method) <τ_0_0 where τ_0_0 : Sequence><τ_1_0> (@in τ_1_0, @owned @callee_owned (@in τ_1_0, @in τ_0_0.Iterator.Element) -> (@out τ_1_0, @error Error), @in_guaranteed τ_0_0) -> (@out τ_1_0, @error Error), normal bb1, error bb2, loc "/tmp/swiftcrash.swift":7:3, scope 0 // id: %17
In function:
sil_scope 1 { loc "/tmp/swiftcrash.swift":2:7 parent @_TFe10swiftcrashRxs10CollectionWx8Iterator7Element_s9EquatablerS0_5countuRd__zWxS1_S2__rfT2ofqd___Si : $@convention(method) <τ_0_0 where τ_0_0 : Collection, τ_0_0.Iterator.Element : Equatable><τ_1_0 where τ_1_0 == τ_0_0.Iterator.Element> (@in τ_0_0.Iterator.Element, @in_guaranteed τ_0_0) -> Int }
sil_scope 2 { loc "/tmp/swiftcrash.swift":8:2 parent 1 }

// Collection<A where ...>.count<A where ...> (of : A1) -> Int
sil hidden @_TFe10swiftcrashRxs10CollectionWx8Iterator7Element_s9EquatablerS0_5countuRd__zWxS1_S2__rfT2ofqd___Si : $@convention(method) <Self where Self : Collection, Self.Iterator.Element : Equatable><T where T == Self.Iterator.Element> (@in Self.Iterator.Element, @in_guaranteed Self) -> Int {
// %0                                             // users: %22, %12, %2
// %1                                             // users: %17, %3
bb0(%0 : $*Self.Iterator.Element, %1 : $*Self):
  debug_value_addr %0 : $*Self.Iterator.Element, let, name "element", argno 1, loc "/tmp/swiftcrash.swift":2:30, scope 1 // id: %2
  debug_value_addr %1 : $*Self, let, name "self", argno 2, loc "/tmp/swiftcrash.swift":2:7, scope 1 // id: %3
  // function_ref Sequence.reduce<A> (A1, (A1, A.Iterator.Element) throws -> A1) throws -> A1
  %4 = function_ref @_TFEsPs8Sequence6reduceurfzTqd__FzTqd__Wx8Iterator7Element__qd___qd__ : $@convention(method) <τ_0_0 where τ_0_0 : Sequence><τ_1_0> (@in τ_1_0, @owned @callee_owned (@in τ_1_0, @in τ_0_0.Iterator.Element) -> (@out τ_1_0, @error Error), @in_guaranteed τ_0_0) -> (@out τ_1_0, @error Error), loc "/tmp/swiftcrash.swift":3:15, scope 2 // user: %17
  // function_ref Int.init(_builtinIntegerLiteral : Builtin.Int2048) -> Int
  %5 = function_ref @_TFSiCfT22_builtinIntegerLiteralBi2048__Si : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int, loc "/tmp/swiftcrash.swift":3:22, scope 2 // user: %8
  %6 = metatype $@thin Int.Type, loc "/tmp/swiftcrash.swift":3:22, scope 2 // user: %8
  %7 = integer_literal $Builtin.Int2048, 0, loc "/tmp/swiftcrash.swift":3:22, scope 2 // user: %8
  %8 = apply %5(%7, %6) : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int, loc "/tmp/swiftcrash.swift":3:22, scope 2 // user: %10
  %9 = alloc_stack $Int, loc "/tmp/swiftcrash.swift":3:22, scope 2 // users: %21, %17, %10
  store %8 to [trivial] %9 : $*Int, loc "/tmp/swiftcrash.swift":3:22, scope 2 // id: %10
  // function_ref Collection<A where ...>.(count<A where ...> (of : A1) -> Int).(closure #&#8203;1)
  %11 = function_ref @_TFFe10swiftcrashRxs10CollectionWx8Iterator7Element_s9EquatablerS0_5countuRd__zWxS1_S2__rFT2ofqd___SiU_FTSiqd___Si : $@convention(thin) <τ_0_0 where τ_0_0 : Collection, τ_0_0.Iterator.Element : Equatable><τ_1_0 where τ_1_0 == τ_0_0.Iterator.Element> (Int, @in τ_0_0.Iterator.Element, @inout_aliasable τ_0_0.Iterator.Element) -> Int, loc "/tmp/swiftcrash.swift":3:25, scope 2 // user: %12
  %12 = partial_apply %11<Self, Self.Iterator.Element>(%0) : $@convention(thin) <τ_0_0 where τ_0_0 : Collection, τ_0_0.Iterator.Element : Equatable><τ_1_0 where τ_1_0 == τ_0_0.Iterator.Element> (Int, @in τ_0_0.Iterator.Element, @inout_aliasable τ_0_0.Iterator.Element) -> Int, loc "/tmp/swiftcrash.swift":3:25, scope 2 // user: %13
  %13 = convert_function %12 : $@callee_owned (Int, @in Self.Iterator.Element) -> Int to $@callee_owned (Int, @in Self.Iterator.Element) -> (Int, @error Error), loc "/tmp/swiftcrash.swift":3:25, scope 2 // user: %15
  // function_ref thunk
  %14 = function_ref @_TTRG__Rxs10Collectiond__zWx8Iterator7Element_WxS0_S1__s9EquatablerXFo_dSiiqd___dSizoPs5Error__XFo_iSiiqd___iSizoPS3___ : $@convention(thin) <τ_0_0 where τ_0_0 : Collection, τ_0_0.Iterator.Element : Equatable><τ_1_0 where τ_1_0 == τ_0_0.Iterator.Element> (@in Int, @in τ_1_0, @owned @callee_owned (Int, @in τ_1_0) -> (Int, @error Error)) -> (@out Int, @error Error), loc "/tmp/swiftcrash.swift":3:25, scope 2 // user: %15
  %15 = partial_apply %14<Self, Self.Iterator.Element>(%13) : $@convention(thin) <τ_0_0 where τ_0_0 : Collection, τ_0_0.Iterator.Element : Equatable><τ_1_0 where τ_1_0 == τ_0_0.Iterator.Element> (@in Int, @in τ_1_0, @owned @callee_owned (Int, @in τ_1_0) -> (Int, @error Error)) -> (@out Int, @error Error), loc "/tmp/swiftcrash.swift":3:25, scope 2 // user: %17
  %16 = alloc_stack $Int, loc "/tmp/swiftcrash.swift":7:3, scope 2 // users: %20, %19, %17
  try_apply %4<Self, Int>(%16, %9, %15, %1) : $@convention(method) <τ_0_0 where τ_0_0 : Sequence><τ_1_0> (@in τ_1_0, @owned @callee_owned (@in τ_1_0, @in τ_0_0.Iterator.Element) -> (@out τ_1_0, @error Error), @in_guaranteed τ_0_0) -> (@out τ_1_0, @error Error), normal bb1, error bb2, loc "/tmp/swiftcrash.swift":7:3, scope 2 // id: %17

bb1(%18 : $()):                                   // Preds: bb0
  %19 = load [trivial] %16 : $*Int, loc "/tmp/swiftcrash.swift":7:3, scope 2 // user: %23
  dealloc_stack %16 : $*Int, loc "/tmp/swiftcrash.swift":7:3, scope 2 // id: %20
  dealloc_stack %9 : $*Int, loc "/tmp/swiftcrash.swift":7:3, scope 2 // id: %21
  destroy_addr %0 : $*Self.Iterator.Element, loc "/tmp/swiftcrash.swift":8:2, scope 2 // id: %22
  return %19 : $Int, loc "/tmp/swiftcrash.swift":3:3, scope 2 // id: %23

bb2(%24 : $Error):                                // Preds: bb0
  unreachable , loc "/tmp/swiftcrash.swift":7:3, scope 2 // id: %25
} // end sil function '_TFe10swiftcrashRxs10CollectionWx8Iterator7Element_s9EquatablerS0_5countuRd__zWxS1_S2__rfT2ofqd___Si'

0  swift                    0x0000000112f2c325 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 101
1  swift                    0x0000000112f2c999 PrintStackTraceSignalHandler(void*) + 25
2  swift                    0x0000000112f28599 llvm::sys::RunSignalHandlers() + 425
3  swift                    0x0000000112f2cea2 SignalHandler(int) + 354
4  libsystem_platform.dylib 0x00007fff8e4ec52a _sigtramp + 26
Stack dump:
0.  Program arguments: /Users/hendrikvonprince/develop/thirdparty/swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift -frontend -interpret /tmp/swiftcrash.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -module-name swiftcrash 
1.  While emitting SIL for 'count' at /tmp/swiftcrash.swift:2:2
2.  While verifying SIL function @_TFe10swiftcrashRxs10CollectionWx8Iterator7Element_s9EquatablerS0_5countuRd__zWxS1_S2__rfT2ofqd___Si for 'count' at /tmp/swiftcrash.swift:2:2
belkadan commented 7 years ago

Workaround: just use Self.Iterator.Element instead of introducing a new parameter.

slavapestov commented 7 years ago

https://github.com/apple/swift/pull/6546