Skip to content

Commit 5238c3f

Browse files
author
Gabor Horvath
committed
[cxx-interop] Work around crash in codegen when initializing C++ span
When initializing span with an UnsafePointer<Element>? we call into the generic initializer that we imported from the C++ templated constructor instead of the concrete initializer we have in the overlay that takes an UnsafePointer<Element> (non-optional). We cannot properly codegen for this generic initializer at the moment, so let's stop importing them since the user probably wanted to call the initializer from the overlay. We should come back later and fix the root cause. rdar://148961349
1 parent bc07600 commit 5238c3f

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
@@ -4110,6 +4110,15 @@ namespace {
41104110
if (ctordecl->isCopyConstructor() || ctordecl->isMoveConstructor())
41114111
return nullptr;
41124112

4113+
// Don't import the generic ctors of std::span, rely on the ctors that
4114+
// we instantiate when conforming to the overlay. These generic ctors
4115+
// can cause crashes in codegen.
4116+
// FIXME: figure out why.
4117+
const auto *parent = ctordecl->getParent();
4118+
if (funcTemplate && parent->isInStdNamespace() &&
4119+
parent->getIdentifier() && parent->getName() == "span")
4120+
return nullptr;
4121+
41134122
DeclName ctorName(Impl.SwiftContext, DeclBaseName::createConstructor(),
41144123
bodyParams);
41154124
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)