Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ interop - Crash on defining a function ptr that takes a std string #74588

Closed
ronald-baldonado opened this issue Jun 20, 2024 · 1 comment
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ c++ to swift Feature → c++ interop: c++ to swift compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software function types Feature → types: function types SILGen Area → compiler: The SIL generation stage swift 5.10 types Feature: types

Comments

@ronald-baldonado
Copy link

Description

The Swift Compiler crashes when I try to define a function pointer declared from C++ in swift code. The function takes in a std::string and returns void.

Reproduction

Swift Code

var callback : printCallback = { (s1: std.string) -> Void in
    
    let swiftStr = String(s1);
    print(swiftStr)
}

C++ Code

#include <string>

typedef void (*printCallback)(std::string);

Stack dump

0  swift-frontend           0x0000000103011abc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000105c77cb0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001059e1054 SignalHandler(int) + 352
3  libsystem_platform.dylib 0x0000000193f37584 _sigtramp + 56
4  swift-frontend           0x00000001022e8244 (anonymous namespace)::DestructureInputs::visit(swift::ValueOwnership, bool, swift::Lowering::AbstractionPattern, swift::CanType, bool) + 732
5  swift-frontend           0x0000000103f61cd8 (anonymous namespace)::DestructureInputs::visit(swift::Lowering::AbstractionPattern, swift::AnyFunctionType::Param, bool) + 268
6  swift-frontend           0x0000000103f65768 void llvm::function_ref<void (swift::Lowering::FunctionParamGenerator&)>::callback_fn<(anonymous namespace)::DestructureInputs::visitTopLevelParams(swift::Lowering::AbstractionPattern, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, swift::SILExtInfoBuilder, bool&)::'lambda'(swift::Lowering::FunctionParamGenerator&)>(long, swift::Lowering::FunctionParamGenerator&) + 672
7  swift-frontend           0x0000000102245e00 swift::Lowering::AbstractionPattern::forEachFunctionParam(swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, bool, llvm::function_ref<void (swift::Lowering::FunctionParamGenerator&)>) const + 208
8  swift-frontend           0x00000001022e7838 (anonymous namespace)::DestructureInputs::destructure(swift::Lowering::AbstractionPattern, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, swift::SILExtInfoBuilder, bool&) + 380
9  swift-frontend           0x0000000103f5d6c0 getSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILExtInfoBuilder, (anonymous namespace)::Conventions const&, swift::ForeignInfo const&, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SubstitutionMap>, swift::ProtocolConformanceRef) + 2672
10 swift-frontend           0x0000000103f49890 getNativeSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILExtInfoBuilder, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SubstitutionMap>, swift::ProtocolConformanceRef) (.llvm.13406110992970554804) + 2028
11 swift-frontend           0x0000000102428350 swift::Lowering::TypeConverter::computeLoweredRValueType(swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanType)::LoweredRValueTypeVisitor::visitAnyFunctionType(swift::CanTypeWrapper<swift::AnyFunctionType>) + 812
12 swift-frontend           0x00000001023efc34 swift::Lowering::TypeConverter::getTypeLowering(swift::Lowering::AbstractionPattern, swift::Type, swift::TypeExpansionContext) + 556
13 swift-frontend           0x00000001036e778c swift::Lowering::SILGenModule::getSILGlobalVariable(swift::VarDecl*, swift::ForDefinition_t) + 948
14 swift-frontend           0x0000000100b36f58 swift::Lowering::SILGenFunction::emitInitializationForVarDecl(swift::VarDecl*, bool, bool) + 1056
15 swift-frontend           0x0000000101148e30 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 260
16 swift-frontend           0x00000001011e69b0 swift::Lowering::SILGenFunction::emitLazyGlobalInitializer(swift::PatternBindingDecl*, unsigned int) + 480
17 swift-frontend           0x00000001033499c8 swift::Lowering::SILGenModule::emitLazyGlobalInitializer(llvm::StringRef, swift::PatternBindingDecl*, unsigned int) + 916
18 swift-frontend           0x00000001036ea0dc swift::Lowering::SILGenModule::emitGlobalInitialization(swift::PatternBindingDecl*, unsigned int) + 716
19 swift-frontend           0x000000010332e77c swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 424
20 swift-frontend           0x0000000103353600 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 2908
21 swift-frontend           0x0000000103a22f98 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 200
22 swift-frontend           0x00000001033799ac llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 584
23 swift-frontend           0x00000001055549c4 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1680
24 swift-frontend           0x0000000105558854 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4568
25 swift-frontend           0x00000001055bfd44 swift::mainEntry(int, char const**) + 4408
26 dyld                     0x0000000193b7e0e0 start + 2360

Expected behavior

I expected the compiler to parse cpp-defined function ptrs using a Swift closure.

Environment

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)

Additional information

No response

@ronald-baldonado ronald-baldonado added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Jun 20, 2024
@hamishknight
Copy link
Contributor

This no longer crashes on 6.0, looks like it was fixed by #73193

@AnthonyLatsis AnthonyLatsis added c++ interop Feature: Interoperability with C++ compiler The Swift compiler itself SILGen Area → compiler: The SIL generation stage c++ to swift Feature → c++ interop: c++ to swift swift 5.10 function types Feature → types: function types types Feature: types and removed triage needed This issue needs more specific labels labels Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ c++ to swift Feature → c++ interop: c++ to swift compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software function types Feature → types: function types SILGen Area → compiler: The SIL generation stage swift 5.10 types Feature: types
Projects
None yet
Development

No branches or pull requests

3 participants