Skip to content

[stdlib] Workaround C++ lifetime rules #35339

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 4 additions & 2 deletions stdlib/public/runtime/MetadataLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ swift::_contextDescriptorMatchesMangling(const ContextDescriptor *context,
static const ContextDescriptor *
_searchTypeMetadataRecords(TypeMetadataPrivateState &T,
Demangle::NodePointer node) {
for (auto &section : T.SectionsToScan.snapshot()) {
auto snapshot = T.SectionsToScan.snapshot(); // Workaround C++ lifetime rules
for (auto &section : snapshot) {
for (const auto &record : section) {
if (auto context = record.getContextDescriptor()) {
if (_contextDescriptorMatchesMangling(context, node)) {
Expand Down Expand Up @@ -846,7 +847,8 @@ void swift::swift_registerProtocols(const ProtocolRecord *begin,
static const ProtocolDescriptor *
_searchProtocolRecords(ProtocolMetadataPrivateState &C,
NodePointer node) {
for (auto &section : C.SectionsToScan.snapshot()) {
auto snapshot = C.SectionsToScan.snapshot(); // Workaround C++ lifetime rules
for (auto &section : snapshot) {
for (const auto &record : section) {
if (auto protocol = record.Protocol.getPointer()) {
if (_contextDescriptorMatchesMangling(protocol, node))
Expand Down
6 changes: 4 additions & 2 deletions stdlib/public/runtime/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ void ConformanceState::verify() const {
// Iterate over all of the sections and verify all of the protocol
// descriptors.
auto &Self = const_cast<ConformanceState &>(*this);
for (const auto &Section : Self.SectionsToScan.snapshot()) {
auto snapshot = Self.SectionsToScan.snapshot(); // Workaround C++ lifetime rules
for (const auto &Section : snapshot) {
for (const auto &Record : Section) {
Record.get()->verify();
}
Expand Down Expand Up @@ -513,7 +514,8 @@ const ContextDescriptor *
swift::_searchConformancesByMangledTypeName(Demangle::NodePointer node) {
auto &C = Conformances.get();

for (auto &section : C.SectionsToScan.snapshot()) {
auto snapshot = C.SectionsToScan.snapshot(); // Workaround C++ lifetime rules
for (auto &section : snapshot) {
for (const auto &record : section) {
if (auto ntd = record->getTypeDescriptor()) {
if (_contextDescriptorMatchesMangling(ntd, node))
Expand Down
6 changes: 4 additions & 2 deletions unittests/runtime/Concurrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ TEST(ConcurrentReadableArrayTest, SingleThreaded) {
};
auto check = [&]{
size_t i = 0;
for (auto element : array.snapshot()) {
auto snapshot = array.snapshot(); // Workaround C++ lifetime rules
for (auto element : snapshot) {
ASSERT_EQ(element, i);
i++;
}
Expand Down Expand Up @@ -70,7 +71,8 @@ TEST(ConcurrentReadableArrayTest, MultiThreaded) {
while (!done) {
for (int i = 0; i < writerCount; i++)
maxByThread[i] = -1;
for (auto element : array.snapshot()) {
auto snapshot = array.snapshot(); // Workaround C++ lifetime rules
for (auto element : snapshot) {
ASSERT_LT(element.threadNumber, writerCount);
// Each element we see must be larger than the maximum element we've
// previously seen for that writer thread, otherwise that means that
Expand Down