swiftlang / swift

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

[SR-3007] Assertion `!outerOrigType.isTuple()' failed in swift/lib/SILGen/SILGenPoly.cpp:1831 #45597

Open swift-ci opened 7 years ago

swift-ci commented 7 years ago
Previous ID SR-3007
Radar None
Original Reporter chsu (JIRA User)
Type Bug
Environment Target: s390x-ibm-linux Swift version 3.0-dev checked out at 2016-10-12 (LLVM 2cb28b32ee, Clang 5d885a53fc, Swift 785b4ebf48)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash, TypeChecker | |Assignee | @slavapestov | |Priority | Medium | md5: 55d6cc0cbd36882675eb06eb26c8026a

relates to:

Issue Description:

Assertion failed in Swift compiler for the following program:

func test(_ data: UnsafePointer<Int8>?, len: Int) {
  data?.withMemoryRebound(to: UInt8.self, capacity: len) {
    data in
    for i in 0..<len {
      print("\(data[i])")
    }
  }
}
~/work/swift$ swiftc t.swift
swift: /home/chsu/work/swift/swift/lib/SILGen/SILGenPoly.cpp:1831: void (anonymous namespace)::ResultPlanner::planTupleIntoDirectResult(swift::Lowering::AbstractionPattern, CanTupleType, swift::Lowering::AbstractionPattern, swift::CanType, (anonymous namespace)::ResultPlanner::PlanData &, swift::SILResultInfo): Assertion `!outerOrigType.isTuple()' failed.
#&#8203;0 0x0000000082a7b0b0 llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/chsu/work/swift/llvm/lib/Support/Unix/Signals.inc:402:13
Stack dump:
0.      Program arguments: /home/chsu/work/swift/install/usr/bin/swift -frontend -c -primary-file t.swift -target s390x-ibm-linux -disable-objc-interop -color-diagnostics -module-name t -o /tmp/t-b78b7b.o
1.      While emitting SIL for 'test' at t.swift:1:1
2.      While emitting reabstraction thunk in SIL function @_TTRXFo_dGSPVs5UInt8___XFo_dGSPS___dGSqT__zoPs5Error__<unknown>:0: error: unable to execute command: Aborted
<unknown>:0: error: compile command failed due to signal (use -v to see invocation)

But if we unwrap data before calling withMemoryRebound(), the assertion error is gone:

func test(_ data: UnsafePointer<Int8>?, len: Int) {
  if let ptr = data {
    ptr.withMemoryRebound(to: UInt8.self, capacity: len) {
      ptr in
      for i in 0..<len {
        print("\(ptr[i])")
      }
    }
  }
}
belkadan commented 7 years ago

Fails on macOS too (as expected, no reason for this to be platform-specific).

It looks like the type-checker is incorrectly marking the call as producing a Void? result instead of Void and then wrapping it in an Optional.

      (optional_evaluation_expr implicit type='()?' location=<stdin>:2:9 range=[<stdin>:2:3 - line:7:3]
        (call_expr type='()?' location=<stdin>:2:9 range=[<stdin>:2:3 - line:7:3] nothrow  arg_labels=to:capacity:_:
          (dot_syntax_call_expr type='(UInt8.Type, Int, (UnsafePointer<UInt8>) throws -> ()?) throws -> ()?' location=<stdin>:2:9 range=[<stdin>:2:3 - line:2:9] nothrow

Over to Mark.