Skip to content

[cxx-interop] Work around crash in codegen when initializing C++ span #81030

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

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4110,6 +4110,15 @@ namespace {
if (ctordecl->isCopyConstructor() || ctordecl->isMoveConstructor())
return nullptr;

// Don't import the generic ctors of std::span, rely on the ctors that
// we instantiate when conforming to the overlay. These generic ctors
// can cause crashes in codegen.
// FIXME: figure out why.
const auto *parent = ctordecl->getParent();
if (funcTemplate && parent->isInStdNamespace() &&
parent->getIdentifier() && parent->getName() == "span")
return nullptr;

DeclName ctorName(Impl.SwiftContext, DeclBaseName::createConstructor(),
bodyParams);
result = Impl.createDeclWithClangNode<ConstructorDecl>(
Expand Down
8 changes: 8 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-span-typechecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ arr.withUnsafeBufferPointer { ubpointer in
let _ = ConstSpanOfInt(ubpointer.baseAddress!, ubpointer.count)
// expected-warning@-1 {{'init(_:_:)' is deprecated: use 'init(_:)' instead.}}
}

arr.withUnsafeBufferPointer { ubpointer in
// FIXME: this crashes the compiler once we import span's templated ctors as Swift generics.
let _ = ConstSpanOfInt(ubpointer.baseAddress, ubpointer.count)
// expected-error@-1 {{value of optional type 'UnsafePointer<Int32>?' must be unwrapped to a value of type 'UnsafePointer<Int32>'}}
// expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
}