Skip to content

[SILGenFunction] emitBridgeErrorForForeignError hits assertion error when an @objc function throws(ObjCError) #80974

@DataCorrupted

Description

@DataCorrupted

Description

I noticed that swiftc can't bridge Error types between Swift and ObjC when SILGen-ing a function that explicitly throws an error bridged to ObjC. The the reproduction for an example. More specifically, the compiler crashed on the assertion:

assert(nativeErrorType == SGF.SGM.getASTContext().getErrorExistentialType());

When that assertion is hit, nativeErrorType is user defined ObjCError, while the other end is the parent classError. However, since ObjCError inherits Error, the code generated (on non-assert build) seems correct somehow.

I'd love to help fix this, if I can get some guidance in terms of how.

Reproduction

  • Follow the doc to build the swift frontend first.
  • Build the following code with the command:
echo "
import Foundation

@objc public enum ObjCError: Int, Error {
    case ObjCError
}

@objc class ObjCClass: NSObject {
    @objc func throwableFunc() throws(ObjCError) { return }
}
" > main.swift

../build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/bin/swiftc -emit-irgen main.swift  -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/

Stack dump

Assertion failed: (nativeErrorType == SGF.SGM.getASTContext().getErrorExistentialType()), function emitBridged, file SILGenForeignError.cpp, line 136.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.      Program arguments: /Users/peterrong/swift-project/build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/bin/swift-frontend -frontend -emit-irgen -primary-file main.swift -target arm64-apple-macosx15.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -color-diagnostics -Xcc -fcolor-diagnostics -empty-abi-descriptor -no-auto-bridging-header-chaining -module-name main -in-process-plugin-server-path /Users/peterrong/swift-project/build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Users/peterrong/swift-project/build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/lib/swift/host/plugins -plugin-path /Users/peterrong/swift-project/build/Ninja-RelWithDebInfoAssert/swift-macosx-arm64/local/lib/swift/host/plugins -target-sdk-version 15.4 -target-sdk-name macosx15.4 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -o -
1.      Swift version 6.2-dev (LLVM c447872d2bf1381, Swift e9253b749ff7cd4)
2.      Compiling with effective version 5.10
3.      While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "main.swift")
4.      While silgen emitNativeToForeignThunk SIL function "@$s4main9ObjCClassC13throwableFunc4condACSi_tAA0B6CErrorOYKFTo".
 for 'throwableFunc(cond:)' (at main.swift:18:18)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x000000010aaac8d4 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010aaaade0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010aaacf2c SignalHandler(int, __siginfo*, void*) + 312
3  libsystem_platform.dylib 0x0000000190153624 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000019011988c pthread_kill + 296
5  libsystem_c.dylib        0x0000000190022c60 abort + 124
6  libsystem_c.dylib        0x0000000190021eec err + 0
7  swift-frontend           0x000000010aae2f44 swift::autodiff::JVPCloner::Implementation::emitTangentForDestroyValueInst(swift::DestroyValueInst*) (.cold.1) + 0
8  swift-frontend           0x000000010584c614 (anonymous namespace)::EpilogErrorSource::emitRelease(swift::Lowering::SILGenFunction&, swift::SILLocation) const + 0
9  swift-frontend           0x000000010584b3c4 emitStoreToForeignErrorSlot(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::SILValue, (anonymous namespace)::BridgedErrorSource const&) + 600
10 swift-frontend           0x000000010584b26c emitStoreToForeignErrorSlot(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::SILValue, (anonymous namespace)::BridgedErrorSource const&) + 256
11 swift-frontend           0x000000010584b088 swift::Lowering::SILGenFunction::emitBridgeErrorForForeignError(swift::SILLocation, swift::SILValue, swift::SILType, swift::SILValue, swift::ForeignErrorConvention const&) + 116
12 swift-frontend           0x00000001057ee4e4 swift::Lowering::SILGenFunction::emitNativeToForeignThunk(swift::SILDeclRef) + 6720
13 swift-frontend           0x00000001057bd10c swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 1020
14 swift-frontend           0x00000001058bfed8 swift::Lowering::SILGenModule::emitNativeToForeignThunk(swift::SILDeclRef) + 100
15 swift-frontend           0x00000001057c0e08 swift::Lowering::SILGenModule::emitObjCMethodThunk(swift::FuncDecl*) + 156
16 swift-frontend           0x00000001058ca7e8 (anonymous namespace)::SILGenType::emitType() + 260
17 swift-frontend           0x00000001058ca6d8 swift::Lowering::SILGenModule::visitNominalTypeDecl(swift::NominalTypeDecl*) + 24
18 swift-frontend           0x00000001057c1774 swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 164
19 swift-frontend           0x00000001057c1d04 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 608
20 swift-frontend           0x00000001058b8950 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)17>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 136
21 swift-frontend           0x00000001057c5644 swift::ASTLoweringRequest::OutputType swift::Evaluator::getResultUncached<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 332
22 swift-frontend           0x00000001057c22a8 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 88
23 swift-frontend           0x000000010519720c swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 392
24 swift-frontend           0x00000001051a33ec withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 164
25 swift-frontend           0x0000000105198f48 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 536
26 swift-frontend           0x00000001051987c4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2324
27 swift-frontend           0x0000000104f46cb4 swift::mainEntry(int, char const**) + 3108
28 dyld                     0x000000018fd7ab4c start + 6000
Abort trap: 6

Expected behavior

With no assert build, the compiler finished nicely. No miscompile observed.

Environment

Swift version 6.2-dev (LLVM c447872d2bf1381, Swift e9253b749ff7cd4)
Target: arm64-apple-macosx15.0
Build config: +assertions

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwaretriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions