Skip to content

Commit c2f560e

Browse files
authored
Merge pull request #81030 from swiftlang/gaborh/span-ctor-crash
2 parents a3993f9 + 5238c3f commit c2f560e

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)