I'm adding Windows capability to a project which, as a ubiquitous need (for a few hundred functions), uses unsafeBitCast to cast the result of a dlsym(..) call, which on non-Windows platforms is an UnsafeMutableRawPointer, to the true function signature. In Swift I use the following (error checking removed and lines folded for brevity):
typealias FunctionPtr = UnsafeMutableRawPointer
typealias functionSignature = @convention(c)
( UnsafeMutablePointer<CChar> ) -> Int32
let pointer: FunctionPtr = dlsym(libHandle, functionName)
let typedFunction = unsafeBitCast(pointer, to: functionSignature.self)
This works as desired on macOS and Linux.
On Windows, the code is the same, except for the call that returns the function pointer:
typealias FunctionPtr = FARPROC
typealias functionSignature = @convention(c)
( UnsafeMutablePointer<CChar> ) -> Int32
let pointer: FunctionPtr = GetProcAddress(libHandle, functionName)
let typedFunction = unsafeBitCast(pointer, to: functionSignature.self)
However, GetProcAddress(..) does not return an UnsafeMutableRawPointer, it returns a 'generic' function pointer (because value pointers and function pointers are different types). The returned pointer is a type called FARPROC which, though used as a stand-in for a function of any signature, is actually a:
@convention(c) ( ) -> Int
This works as desired on Windows.
So, what's my problem?! When the functionSignature described by the typealias happen to be the same signature as returned by GetProcAddress, the compiler complains, with a warning, and correctly, that unsafeBitCast is not required because its two parameters, pointer and functionSignature.self are the same type.
The code on both platforms does what I want (and I can't think of another way to do it), but on Windows the compiler coughs up these warnings. This will be production level source code to be compiled by others and I want to suppress these warning to avoid worries and confusion. Is there a way I could recode this to suppress them?