Skip to content

Concurrency: Use std::bit_cast over ignoring -Wcast-function-type-mismatch #79036

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 2 commits 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
2 changes: 1 addition & 1 deletion include/swift/Runtime/STLCompatibility.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===---- STLCompatibility.h - Runtime C++ Compatibiltiy Stubs --*- C++ -*-===//
//===---- STLCompatibility.h - Runtime C++ Compatibility Stubs --*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
Expand Down
1 change: 0 additions & 1 deletion lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
auto *ClassTy = BaseTy->castTo<BoundGenericClassType>();
auto *Decl = ClassTy->getDecl();
auto L = getFileAndLocation(Decl);
unsigned FwdDeclLine = 0;

return createSpecializedStructOrClassType(ClassTy, Decl, Scope, L.File,
L.Line, SizeInBits, AlignInBits,
Expand Down
37 changes: 10 additions & 27 deletions stdlib/public/Concurrency/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
#include "swift/Basic/Lazy.h"
#include "swift/Runtime/Concurrency.h"
#include "swift/Runtime/EnvironmentVariables.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Heap.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/STLCompatibility.h"
#include "swift/Threading/Mutex.h"
#include <atomic>
#include <new>
Expand Down Expand Up @@ -583,11 +584,8 @@ SWIFT_CC(swiftasync)
static void task_wait_throwing_resume_adapter(SWIFT_ASYNC_CONTEXT AsyncContext *_context) {

auto context = static_cast<TaskFutureWaitAsyncContext *>(_context);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
auto resumeWithError =
reinterpret_cast<AsyncVoidClosureEntryPoint *>(context->ResumeParent);
#pragma clang diagnostic pop
std::bit_cast<AsyncVoidClosureEntryPoint *>(context->ResumeParent);
return resumeWithError(context->Parent, context->errorResult);
}

Expand Down Expand Up @@ -962,23 +960,17 @@ swift_task_create_commonImpl(size_t rawTaskCreateFlags,
auto asyncContextPrefix = reinterpret_cast<AsyncContextPrefix *>(
reinterpret_cast<char *>(allocation) + headerSize -
sizeof(AsyncContextPrefix));
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
asyncContextPrefix->asyncEntryPoint =
reinterpret_cast<AsyncVoidClosureEntryPoint *>(function);
#pragma clang diagnostic pop
std::bit_cast<AsyncVoidClosureEntryPoint *>(function);
asyncContextPrefix->closureContext = closureContext;
function = non_future_adapter;
assert(sizeof(AsyncContextPrefix) == 3 * sizeof(void *));
} else {
auto asyncContextPrefix = reinterpret_cast<FutureAsyncContextPrefix *>(
reinterpret_cast<char *>(allocation) + headerSize -
sizeof(FutureAsyncContextPrefix));
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
asyncContextPrefix->asyncEntryPoint =
reinterpret_cast<AsyncGenericClosureEntryPoint *>(function);
#pragma clang diagnostic pop
std::bit_cast<AsyncGenericClosureEntryPoint *>(function);
function = future_adapter;
asyncContextPrefix->closureContext = closureContext;
assert(sizeof(FutureAsyncContextPrefix) == 4 * sizeof(void *));
Expand Down Expand Up @@ -1049,8 +1041,6 @@ swift_task_create_commonImpl(size_t rawTaskCreateFlags,
// Initialize the parent context pointer to null.
initialContext->Parent = nullptr;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
// Initialize the resumption funclet pointer (async return address) to
// the final funclet for completing the task.

Expand All @@ -1064,21 +1054,20 @@ swift_task_create_commonImpl(size_t rawTaskCreateFlags,
// The final funclet shouldn't release the task or the task function.
} else if (asyncLet) {
initialContext->ResumeParent =
reinterpret_cast<TaskContinuationFunction*>(&completeTask);
std::bit_cast<TaskContinuationFunction *>(&completeTask);

// If we have a non-null closure context and the task function is not
// consumed by calling it, use a final funclet that releases both the
// task and the closure context.
} else if (closureContext && !taskCreateFlags.isTaskFunctionConsumed()) {
initialContext->ResumeParent =
reinterpret_cast<TaskContinuationFunction*>(&completeTaskWithClosure);
std::bit_cast<TaskContinuationFunction *>(&completeTaskWithClosure);

// Otherwise, just release the task.
} else {
initialContext->ResumeParent =
reinterpret_cast<TaskContinuationFunction*>(&completeTaskAndRelease);
std::bit_cast<TaskContinuationFunction *>(&completeTaskAndRelease);
}
#pragma clang diagnostic pop

// Initialize the task-local allocator and our other private runtime
// state for the task.
Expand Down Expand Up @@ -1292,13 +1281,10 @@ AsyncTaskAndContext swift::swift_task_create(
FutureAsyncSignature,
SpecialPointerAuthDiscriminators::AsyncFutureFunction>(closureEntry);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
return swift_task_create_common(
rawTaskCreateFlags, options, futureResultType,
reinterpret_cast<TaskContinuationFunction *>(taskEntry), closureContext,
std::bit_cast<TaskContinuationFunction *>(taskEntry), closureContext,
initialContextSize);
#pragma clang diagnostic pop
}
}

Expand Down Expand Up @@ -1383,10 +1369,7 @@ void swift_task_future_wait_throwingImpl(
waitingTask->ResumeTask = task_wait_throwing_resume_adapter;
waitingTask->ResumeContext = callContext;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
auto resumeFn = reinterpret_cast<TaskContinuationFunction *>(resumeFunction);
#pragma clang diagnostic pop
auto resumeFn = std::bit_cast<TaskContinuationFunction *>(resumeFunction);

// Wait on the future.
assert(task->isFuture());
Expand Down