Skip to content

Commit 1c92940

Browse files
authored
Merge pull request #81047 from swiftlang/gaborh/span-ctor-crash-6.2
[6.2][cxx-interop] Work around crash in codegen when initializing C++ span
2 parents ad86786 + 0b1df04 commit 1c92940

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4102,6 +4102,15 @@ namespace {
41024102
if (ctordecl->isCopyConstructor() || ctordecl->isMoveConstructor())
41034103
return nullptr;
41044104

4105+
// Don't import the generic ctors of std::span, rely on the ctors that
4106+
// we instantiate when conforming to the overlay. These generic ctors
4107+
// can cause crashes in codegen.
4108+
// FIXME: figure out why.
4109+
const auto *parent = ctordecl->getParent();
4110+
if (funcTemplate && parent->isInStdNamespace() &&
4111+
parent->getIdentifier() && parent->getName() == "span")
4112+
return nullptr;
4113+
41054114
DeclName ctorName(Impl.SwiftContext, DeclBaseName::createConstructor(),
41064115
bodyParams);
41074116
result = Impl.createDeclWithClangNode<ConstructorDecl>(

test/Interop/Cxx/stdlib/use-std-span-typechecker.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ arr.withUnsafeBufferPointer { ubpointer in
1111
let _ = ConstSpanOfInt(ubpointer.baseAddress!, ubpointer.count)
1212
// expected-warning@-1 {{'init(_:_:)' is deprecated: use 'init(_:)' instead.}}
1313
}
14+
15+
arr.withUnsafeBufferPointer { ubpointer in
16+
// FIXME: this crashes the compiler once we import span's templated ctors as Swift generics.
17+
let _ = ConstSpanOfInt(ubpointer.baseAddress, ubpointer.count)
18+
// expected-error@-1 {{value of optional type 'UnsafePointer<Int32>?' must be unwrapped to a value of type 'UnsafePointer<Int32>'}}
19+
// expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
20+
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
21+
}

0 commit comments

Comments
 (0)