Your bug or my bug? @generics, closures, numerics…

While playing around (just for fun) with generics and closures in a Playground, I encountered an error message, which I think I haven’t seen like this before. It’s a runtime error, but if it is an error in the code, it should at least be a compile time error, I think.

Using Xcode 9.4 beta 2 (9Q1019a) default toolchain in a playground.

Code:

func mult<T: Numeric>(_ x: T, _ y: T) -> T {
    return x * y
}

enum TestEnum {
    
    static func add<T: Numeric>(_ x: T, _ y: T) -> T {
        return x + y
    }
    
    static func div<T: BinaryInteger>(_ x: T, _ y: T) -> Double {
        
        guard
            let a = Double(exactly: x),
            let b = Double(exactly: y) else {
                preconditionFailure("Input not convertible to Double") }
        return a / b
    }
}

func foo<T>(a: T, b: T, _ predicate: (T, T) throws -> T) rethrows -> T {
    return try predicate(a, b)
}

func foo<T, U>(a: T, b: T, _ predicate: (T, T) throws -> U) rethrows -> U {
    return try predicate(a, b)
}

foo(a: 1, b: 2, <) // true
foo(a: 1, b: 2, -) // -1
foo(a: 2, b: 3, ==) // false
foo(a: 3, b: 3, mult) // 9
foo(a: 2, b: 2, TestEnum.add) // 4
foo(a: 3, b: 2, TestEnum.div) // ERROR!

Error message:

Fatal error: file /BuildRoot/Library/Caches/com.apple.xbs/Binaries/swiftlang/install/TempContent/Objects/BNI_assert_lightweight/swift-macosx-x86_64/stdlib/public/core/8/FloatingPointTypes.swift, line 3119

It looks like this is Double(exactly:) getting confused by the generics, or something similar. I see the problem with Xcode 9.3 with the following, but I can’t reproduce it with a recent build from master or with the latest snapshot (2018-04-25 at the time of writing):

func div<T: BinaryInteger>(_ x: T)  {
    _ = Double(exactly: x)
}
div(3)

I also see some extra problems: there’s a crash with the == too:

func foo<T>(a: T, b: T, _ predicate: (T, T) -> T) {
    _ = predicate(a, b)
}

foo(a: 2, b: 3, ==)
With Xcode 9.3
Invalid bitcast
  %8 = bitcast i1 %7 to %swift.opaque*
<unknown>:0: error: fatal error encountered during compilation; please file a bug report with your project and the crash log
<unknown>:0: note: Broken function found, compilation aborted!
0  swift                    0x0000000105179ffa PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x00000001051793b6 SignalHandler(int) + 966
2  libsystem_platform.dylib 0x00007fff50cd8f5a _sigtramp + 26
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2939318464
4  libsystem_c.dylib        0x00007fff50a761ae abort + 127
5  swift                    0x00000001017d08e7 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_0::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 519
6  swift                    0x0000000105131658 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
7  swift                    0x0000000105131539 llvm::report_fatal_error(char const*, bool) + 41
8  swift                    0x00000001050f9ca6 (anonymous namespace)::VerifierLegacyPass::runOnFunction(llvm::Function&) + 54
9  swift                    0x00000001050a17d1 llvm::FPPassManager::runOnFunction(llvm::Function&) + 449
10 swift                    0x00000001050a9703 llvm::legacy::FunctionPassManagerImpl::run(llvm::Function&) + 115
11 swift                    0x00000001050a95fc llvm::legacy::FunctionPassManager::run(llvm::Function&) + 396
12 swift                    0x0000000101979a18 swift::performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine*, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, swift::version::Version const&, llvm::StringRef, swift::UnifiedStatsReporter*) + 5096
13 swift                    0x00000001017db77d performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 44685
14 swift                    0x00000001017cee64 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7908
15 swift                    0x00000001017838b5 main + 18917
16 libdyld.dylib            0x00007fff509ca015 start + 1
17 libdyld.dylib            0x000000000000000e start + 2942525434
Stack dump:
0.	Program arguments: /Applications/Older_Xcodes/Xcode_2018-04-17_11-14.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file torquato.swift -target x86_64-apple-darwin17.6.0 -enable-objc-interop -sdk /Applications/Older_Xcodes/Xcode_2018-04-17_11-14.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -module-name torquato -o /var/folders/3k/4q7m7bkn2k750rpr0jm8s9fr0000gn/T/torquato-dd0111.o 
1.	Running pass 'Module Verifier' on function '@_T0s11AnyHashableVABSbIegiid_A3BIgiir_TR'
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)
With an asserts build of master
TYPE MISMATCH IN ARGUMENT 0 OF APPLY AT <<debugloc at "<compiler-generated>":0:0>>  argument value:   %4 = apply %3(%1, %2) : $@callee_guaranteed (@in_guaranteed AnyHashable, @in_guaranteed AnyHashable) -> Bool
  parameter type: $*Bool
0  swift                    0x0000000107f40d88 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x0000000107f41496 SignalHandler(int) + 694
2  libsystem_platform.dylib 0x00007fff50cd8f5a _sigtramp + 26
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2939318464
4  libsystem_c.dylib        0x00007fff50a761ae abort + 127
5  swift                    0x0000000104ed12b3 swift::Lowering::SILGenFunction::emitApply(std::__1::unique_ptr<swift::Lowering::ResultPlan, std::__1::default_delete<swift::Lowering::ResultPlan> >&&, swift::Lowering::ArgumentScope&&, swift::SILLocation, swift::Lowering::ManagedValue, llvm::ArrayRef<swift::Substitution>, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::Lowering::CalleeTypeInfo const&, swift::Lowering::ApplyOptions, swift::Lowering::SGFContext) + 4499
6  swift                    0x0000000104ed640f swift::Lowering::SILGenFunction::emitApplyOfLibraryIntrinsic(swift::SILLocation, swift::FuncDecl*, swift::SubstitutionMap const&, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::Lowering::SGFContext) + 1615
7  swift                    0x0000000104f2a171 swift::Lowering::SILGenFunction::emitAnyHashableErasure(swift::SILLocation, swift::Lowering::ManagedValue, swift::Type, swift::ProtocolConformanceRef, swift::Lowering::SGFContext) + 193
8  swift                    0x0000000104f823c0 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::SGFContext) + 10240
9  swift                    0x0000000104f8f578 (anonymous namespace)::ResultPlanner::execute(llvm::ArrayRef<swift::SILValue>, llvm::SmallVectorImpl<swift::SILValue>&)::$_10::operator()((anonymous namespace)::ResultPlanner::Operation&, bool, bool) const + 328
10 swift                    0x0000000104f864bf (anonymous namespace)::ResultPlanner::execute(swift::SILValue) + 2127
11 swift                    0x0000000104f88d01 createThunk(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeLowering const&) + 2193
12 swift                    0x0000000104f80791 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::SGFContext) + 3025
13 swift                    0x0000000104f7fb6b swift::Lowering::SILGenFunction::emitTransformedValue(swift::SILLocation, swift::Lowering::ManagedValue, swift::CanType, swift::CanType, swift::Lowering::SGFContext) + 347
14 swift                    0x0000000104f3b4b6 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 21238
15 swift                    0x0000000104f2b8a9 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 57
16 swift                    0x0000000104ee7edb (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 2891
17 swift                    0x0000000104ee8c98 (anonymous namespace)::ArgEmitter::emitExpanded(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 1352
18 swift                    0x0000000104ee7443 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 179
19 swift                    0x0000000104ee6925 (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus) && + 453
20 swift                    0x0000000104ee61b7 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::CanTypeWrapper<swift::FunctionType>&, swift::Lowering::AbstractionPattern&, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&, swift::CanTypeWrapper<swift::FunctionType>&) + 1975
21 swift                    0x0000000104ed3b8d (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3405
22 swift                    0x0000000104ed2d24 swift::Lowering::SILGenFunction::emitApplyExpr(swift::Expr*, swift::Lowering::SGFContext) + 1476
23 swift                    0x0000000104f36210 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 80
24 swift                    0x0000000104f2bfba swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 1258
25 swift                    0x0000000104eca160 swift::Lowering::SILGenModule::visitTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 368
26 swift                    0x0000000104eca96b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 827
27 swift                    0x0000000104ecb6d0 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 352
28 swift                    0x0000000104ecbcdf swift::performSILGeneration(swift::FileUnit&, swift::SILOptions&, llvm::Optional<unsigned int>) + 95
29 swift                    0x00000001047ae5dd performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 8621
30 swift                    0x00000001047ab426 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3318
31 swift                    0x000000010476815d main + 2317
32 libdyld.dylib            0x00007fff509ca015 start + 1
33 libdyld.dylib            0x000000000000000c start + 2942525432
Stack dump:
0.	Program arguments: /Users/huon/projects/swift4/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swift -frontend -c -primary-file torquato.swift -target x86_64-apple-darwin17.6.0 -enable-objc-interop -module-name torquato -o /var/folders/3k/4q7m7bkn2k750rpr0jm8s9fr0000gn/T/torquato-67978d.o 
1.	While emitting reabstraction thunk in SIL function "@$Ss11AnyHashableVABSbIegnnd_A3BIegnnr_TR".
 for <<debugloc at "<compiler-generated>":0:0>><unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)

Do you think you could file a bug about these? Or maybe just the second one, if you can verify that it’s fixed.

1 Like