-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Description
The SWIFT_NAME
annotation can be applied to C++ free functions to make them be imported as member functions on C++ types (https://github.com/swiftlang/swift-evolution/blob/main/proposals/0044-import-as-member.md). When SWIFT_NAME
with import-as-member syntax is applied to a function template, and the function template is called from Swift as a free function (i.e. the way it would be called if SWIFT_NAME
wasn't present), the compiler crashes.
Reproduction
#include <swift/bridging>
struct IntWrapper {
IntWrapper(int x) : x(x) {}
int x;
};
template <typename T>
void foo(const IntWrapper& wrapper, T t) SWIFT_NAME(IntWrapper.foo(self:_:)) {}
let x = IntWrapper(4)
foo(x, 5 as CInt)
Stack dump
1. Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
2. Compiling with the current language version
3. While evaluating request TypeCheckSourceFileRequest(source_file "/Users/maddyadams/Desktop/Xcode/TemplateImportAsMemberCrash/Sources/TemplateImportAsMemberCrash/main.swift")
4. While type-checking statement at [/Users/maddyadams/Desktop/Xcode/TemplateImportAsMemberCrash/Sources/TemplateImportAsMemberCrash/main.swift:7:1 - line:7:17] RangeText="foo(x, 5 as CInt"
5. While type-checking expression at [/Users/maddyadams/Desktop/Xcode/TemplateImportAsMemberCrash/Sources/TemplateImportAsMemberCrash/main.swift:7:1 - line:7:17] RangeText="foo(x, 5 as CInt"
6. While type-checking-target starting at /Users/maddyadams/Desktop/Xcode/TemplateImportAsMemberCrash/Sources/TemplateImportAsMemberCrash/main.swift:7:1
7. While specializing 'foo(_:_:)' (at /Users/maddyadams/Desktop/Xcode/TemplateImportAsMemberCrash/Sources/CxxTarget/include/CppImpl.hpp:20:6)
with substitution map: (substitution_map generic_signature=<T where T : Copyable, T : Escapable>
(substitution T ->
(type_alias_type decl="Swift.(file).CInt"
(underlying=struct_type decl="Swift.(file).Int32")))
(conformance type="T"
(builtin_conformance type="Int32" protocol="Copyable"))
(conformance type="T"
(builtin_conformance type="Int32" protocol="Escapable")))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 0x0000000107332c28 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1 swift-frontend 0x0000000107330a60 llvm::sys::RunSignalHandlers() + 112
2 swift-frontend 0x0000000107333264 SignalHandler(int) + 360
3 libsystem_platform.dylib 0x000000019e01b624 _sigtramp + 56
4 swift-frontend 0x0000000101408b30 swift::ClangImporter::getCXXFunctionTemplateSpecialization(swift::SubstitutionMap, swift::ValueDecl*) + 728
5 swift-frontend 0x00000001022a2884 swift::constraints::Solution::resolveConcreteDeclRef(swift::ValueDecl*, swift::constraints::ConstraintLocator*) const + 676
6 swift-frontend 0x00000001022c2e60 (anonymous namespace)::ExprRewriter::buildDeclRef(swift::constraints::SelectedOverload, swift::DeclNameLoc, swift::constraints::ConstraintLocatorBuilder, bool) + 1100
7 swift-frontend 0x00000001022b0d40 (anonymous namespace)::ExprRewriter::visitDeclRefExpr(swift::DeclRefExpr*) + 396
8 swift-frontend 0x00000001022abc68 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 376
9 swift-frontend 0x000000010294e854 (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) + 116
10 swift-frontend 0x000000010294bdf0 (anonymous namespace)::Traversal::visit(swift::Expr*) + 84
11 swift-frontend 0x00000001022a58ec (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 1568
12 swift-frontend 0x00000001022a51d0 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 6564
13 swift-frontend 0x000000010258796c swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 792
14 swift-frontend 0x0000000102587530 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 420
15 swift-frontend 0x0000000102686fb0 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 340
16 swift-frontend 0x000000010268ca64 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 288
17 swift-frontend 0x000000010268a3fc bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 312
18 swift-frontend 0x000000010268a5d8 swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 148
19 swift-frontend 0x00000001026da708 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 696
20 swift-frontend 0x00000001026e0b18 swift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()>(swift::TypeCheckSourceFileRequest const&, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()) + 624
21 swift-frontend 0x00000001026da434 swift::performTypeChecking(swift::SourceFile&) + 308
22 swift-frontend 0x0000000101544060 swift::CompilerInstance::performSema() + 320
23 swift-frontend 0x0000000101187274 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 772
24 swift-frontend 0x0000000101185fd8 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3716
25 swift-frontend 0x000000010110a0bc swift::mainEntry(int, char const**) + 5428
26 dyld 0x000000019dc42b4c start + 6000
Expected behavior
I expected the compiler would emit an error but not crash, since foo
should now be called as a member function (i.e. x.foo(5 as CInt)
to call void foo<int>(const IntWrapper& wrapper, int t);
)
Environment
swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0
Additional information
rdar://138926009