From 2ab3ee5ec15739893a4960069d429a067cd5fbed Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sat, 30 May 2026 21:11:42 -0700 Subject: [PATCH] Remove unnecessary type casting The compiler now imports these as their canonical type, allowing us to directly use without the explicit type cast. --- .../_Testing_Foundation/Attachments/Attachment+URL.swift | 3 +-- .../HBITMAP+AttachableAsIWICBitmapSource.swift | 2 +- Sources/Testing/ExitTests/ExitTest.swift | 4 ++-- Sources/Testing/ExitTests/SpawnProcess.swift | 6 +++--- Sources/Testing/ExitTests/WaitFor.swift | 2 +- .../SourceAttribution/Backtrace+Symbolication.swift | 2 +- .../Testing/Support/Additions/CommandLineAdditions.swift | 8 ++++---- Sources/Testing/Support/CError.swift | 2 +- Sources/Testing/Support/Environment.swift | 6 +++--- Sources/_TestDiscovery/Additions/WinSDKAdditions.swift | 2 +- Sources/_TestingInternals/include/Stubs.h | 2 +- Tests/TestingTests/ABIEntryPointTests.swift | 2 +- Tests/TestingTests/Support/CErrorTests.swift | 6 +++--- Tests/TestingTests/Support/FileHandleTests.swift | 6 +++--- 14 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Sources/Overlays/_Testing_Foundation/Attachments/Attachment+URL.swift b/Sources/Overlays/_Testing_Foundation/Attachments/Attachment+URL.swift index fb6475cdd..c0de66e11 100644 --- a/Sources/Overlays/_Testing_Foundation/Attachments/Attachment+URL.swift +++ b/Sources/Overlays/_Testing_Foundation/Attachments/Attachment+URL.swift @@ -131,8 +131,7 @@ private let _archiverPath: String? = { return _archiverName.withCString(encodedAs: UTF16.self) { archiverName -> String? in var result: UnsafeMutablePointer? - let flags = ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue) - guard S_OK == PathAllocCombine(buffer.baseAddress!, archiverName, flags, &result) else { + guard S_OK == PathAllocCombine(buffer.baseAddress!, archiverName, DWORD(PATHCCH_ALLOW_LONG_PATHS.rawValue), &result) else { return nil } defer { diff --git a/Sources/Overlays/_Testing_WinSDK/Attachments/HBITMAP+AttachableAsIWICBitmapSource.swift b/Sources/Overlays/_Testing_WinSDK/Attachments/HBITMAP+AttachableAsIWICBitmapSource.swift index 0982234cc..80e997438 100644 --- a/Sources/Overlays/_Testing_WinSDK/Attachments/HBITMAP+AttachableAsIWICBitmapSource.swift +++ b/Sources/Overlays/_Testing_WinSDK/Attachments/HBITMAP+AttachableAsIWICBitmapSource.swift @@ -28,7 +28,7 @@ extension HBITMAP__: _AttachableByAddressAsIWICBitmapSource { // The only reasonable failure mode for `CopyImage()` is allocation failure, // and Swift treats allocation failures as fatal. Hence, we do not check for // `nil` on return. - CopyImage(imageAddress, UINT(IMAGE_BITMAP), 0, 0, 0).assumingMemoryBound(to: Self.self) + CopyImage(imageAddress, IMAGE_BITMAP, 0, 0, 0).assumingMemoryBound(to: Self.self) } public static func _deinitializeAttachableValue(at imageAddress: UnsafeMutablePointer) { diff --git a/Sources/Testing/ExitTests/ExitTest.swift b/Sources/Testing/ExitTests/ExitTest.swift index 9ad50417e..3f025eef8 100644 --- a/Sources/Testing/ExitTests/ExitTest.swift +++ b/Sources/Testing/ExitTests/ExitTest.swift @@ -242,8 +242,8 @@ extension ExitTest { // On Windows, similarly disable Windows Error Reporting and the Windows // Error Reporting UI. Note we expect to be the first component to call // these functions, so we don't attempt to preserve any previously-set bits. - _ = SetErrorMode(UINT(SEM_NOGPFAULTERRORBOX)) - _ = WerSetFlags(DWORD(WER_FAULT_REPORTING_NO_UI)) + _ = SetErrorMode(SEM_NOGPFAULTERRORBOX) + _ = WerSetFlags(WER_FAULT_REPORTING_NO_UI) #else #warning("Platform-specific implementation missing: unable to disable crash reporting") #endif diff --git a/Sources/Testing/ExitTests/SpawnProcess.swift b/Sources/Testing/ExitTests/SpawnProcess.swift index 82feac745..7529e0808 100644 --- a/Sources/Testing/ExitTests/SpawnProcess.swift +++ b/Sources/Testing/ExitTests/SpawnProcess.swift @@ -258,7 +258,7 @@ func spawnExecutable( } // Ensure the file handle can be inherited by the child process. - guard SetHandleInformation(windowsHANDLE, DWORD(HANDLE_FLAG_INHERIT), DWORD(HANDLE_FLAG_INHERIT)) else { + guard SetHandleInformation(windowsHANDLE, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT) else { throw Win32Error(rawValue: GetLastError()) } @@ -326,13 +326,13 @@ func spawnExecutable( // SEE: https://devblogs.microsoft.com/oldnewthing/20101109-00/?p=12323 let workingDirectoryPath = rootDirectoryPath - var flags = DWORD(CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT) + var flags = CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT // Start the process suspended so we can attach a debugger if needed. We // always start the child process in a suspended state even if the // "SWT_START_CHILD_PROCESSES_SUSPENDED" environment variable isn't set so that // the debugger has a chance to attach to the child. - flags |= DWORD(CREATE_SUSPENDED) + flags |= CREATE_SUSPENDED return try environ.withCString(encodedAs: UTF16.self) { environ in try workingDirectoryPath.withCString(encodedAs: UTF16.self) { workingDirectoryPath in diff --git a/Sources/Testing/ExitTests/WaitFor.swift b/Sources/Testing/ExitTests/WaitFor.swift index 9a9c85dc2..ca7f1a10c 100644 --- a/Sources/Testing/ExitTests/WaitFor.swift +++ b/Sources/Testing/ExitTests/WaitFor.swift @@ -296,7 +296,7 @@ func wait(for processHandle: consuming HANDLE) async throws -> ExitStatus { // We only want the callback to fire once (and not be rescheduled.) Waiting // may take an arbitrarily long time, so let the thread pool know that too. - let flags = ULONG(WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION) + let flags = WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION guard RegisterWaitForSingleObject(&waitHandle, processHandle, callback, context, INFINITE, flags) else { continuation.resume(throwing: Win32Error(rawValue: GetLastError())) return diff --git a/Sources/Testing/SourceAttribution/Backtrace+Symbolication.swift b/Sources/Testing/SourceAttribution/Backtrace+Symbolication.swift index fb9c1a22f..f8689122c 100644 --- a/Sources/Testing/SourceAttribution/Backtrace+Symbolication.swift +++ b/Sources/Testing/SourceAttribution/Backtrace+Symbolication.swift @@ -86,7 +86,7 @@ extension Backtrace { withUnsafeTemporaryAllocation(of: SYMBOL_INFO_PACKAGEW.self, capacity: 1) { symbolInfo in let symbolInfo = symbolInfo.baseAddress! symbolInfo.pointee.si.SizeOfStruct = ULONG(MemoryLayout.stride) - symbolInfo.pointee.si.MaxNameLen = ULONG(MAX_SYM_NAME) + symbolInfo.pointee.si.MaxNameLen = MAX_SYM_NAME var displacement = DWORD64(0) if SymFromAddrW(hProcess, DWORD64(clamping: address), &displacement, symbolInfo.pointer(to: \.si)!) { let symbolName = String.decodeCString(symbolInfo.pointer(to: \.si.Name)!, as: UTF16.self)?.result diff --git a/Sources/Testing/Support/Additions/CommandLineAdditions.swift b/Sources/Testing/Support/Additions/CommandLineAdditions.swift index fa51c9550..0b6648e9f 100644 --- a/Sources/Testing/Support/Additions/CommandLineAdditions.swift +++ b/Sources/Testing/Support/Additions/CommandLineAdditions.swift @@ -108,15 +108,15 @@ extension CommandLine { #endif while result == nil { try withUnsafeTemporaryAllocation(of: CWideChar.self, capacity: bufferCount) { buffer in - SetLastError(DWORD(ERROR_SUCCESS)) + SetLastError(ERROR_SUCCESS) _ = GetModuleFileNameW(nil, buffer.baseAddress!, DWORD(buffer.count)) switch GetLastError() { - case DWORD(ERROR_SUCCESS): + case ERROR_SUCCESS: result = String.decodeCString(buffer.baseAddress!, as: UTF16.self)?.result if result == nil { - throw Win32Error(rawValue: DWORD(ERROR_ILLEGAL_CHARACTER)) + throw Win32Error(rawValue: ERROR_ILLEGAL_CHARACTER) } - case DWORD(ERROR_INSUFFICIENT_BUFFER): + case ERROR_INSUFFICIENT_BUFFER: bufferCount += Int(MAX_PATH) case let errorCode: throw Win32Error(rawValue: errorCode) diff --git a/Sources/Testing/Support/CError.swift b/Sources/Testing/Support/CError.swift index d35e0d0e6..bc6b0cf71 100644 --- a/Sources/Testing/Support/CError.swift +++ b/Sources/Testing/Support/CError.swift @@ -97,7 +97,7 @@ extension Win32Error: CustomStringConvertible { // we need to temporarily mis-cast the pointer before we can pass it in. let count = buffer.withMemoryRebound(to: CWideChar.self) { buffer in FormatMessageW( - DWORD(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK), + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, nil, rawValue, DWORD(swt_MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)), diff --git a/Sources/Testing/Support/Environment.swift b/Sources/Testing/Support/Environment.swift index bef8b77e9..2e522adae 100644 --- a/Sources/Testing/Support/Environment.swift +++ b/Sources/Testing/Support/Environment.swift @@ -199,14 +199,14 @@ package enum Environment { name.withCString(encodedAs: UTF16.self) { name in func getVariable(maxCount: Int) -> String? { withUnsafeTemporaryAllocation(of: CWideChar.self, capacity: maxCount) { buffer in - SetLastError(DWORD(ERROR_SUCCESS)) + SetLastError(ERROR_SUCCESS) let count = GetEnvironmentVariableW(name, buffer.baseAddress!, DWORD(buffer.count)) if count == 0 { switch GetLastError() { - case DWORD(ERROR_SUCCESS): + case ERROR_SUCCESS: // Empty String return "" - case DWORD(ERROR_ENVVAR_NOT_FOUND): + case ERROR_ENVVAR_NOT_FOUND: // The environment variable wasn't set. return nil case let errorCode: diff --git a/Sources/_TestDiscovery/Additions/WinSDKAdditions.swift b/Sources/_TestDiscovery/Additions/WinSDKAdditions.swift index 20019ebda..fc9654399 100644 --- a/Sources/_TestDiscovery/Additions/WinSDKAdditions.swift +++ b/Sources/_TestDiscovery/Additions/WinSDKAdditions.swift @@ -42,7 +42,7 @@ extension HMODULE { } } else { // Create a toolhelp snapshot that lists modules. - guard let snapshot = CreateToolhelp32Snapshot(DWORD(TH32CS_SNAPMODULE), 0) else { + guard let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0) else { return nil } state.snapshot = snapshot diff --git a/Sources/_TestingInternals/include/Stubs.h b/Sources/_TestingInternals/include/Stubs.h index ec7e09706..831cb1b95 100644 --- a/Sources/_TestingInternals/include/Stubs.h +++ b/Sources/_TestingInternals/include/Stubs.h @@ -118,7 +118,7 @@ SWT_DEFINE_ATOMIC_OPERATIONS(void const *_Nullable) /// /// This function is provided because `MAKELANGID()` is a complex macro and /// cannot be imported directly into Swift. -static LANGID swt_MAKELANGID(int p, int s) { +static LANGID swt_MAKELANGID(WORD p, WORD s) { return MAKELANGID(p, s); } diff --git a/Tests/TestingTests/ABIEntryPointTests.swift b/Tests/TestingTests/ABIEntryPointTests.swift index d50be60f3..fa00f4ce5 100644 --- a/Tests/TestingTests/ABIEntryPointTests.swift +++ b/Tests/TestingTests/ABIEntryPointTests.swift @@ -262,7 +262,7 @@ private func withTestingLibraryImageAddress(_ body: (ImageAddress?) throws -> // We can't dynamically look up a function linked into the test executable on // ELF-based platforms. #elseif os(Windows) - let flags = DWORD(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS) + let flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS try addressInTestingLibrary.withMemoryRebound(to: CWideChar.self, capacity: MemoryLayout.stride / MemoryLayout.stride) { addressInTestingLibrary in try #require(GetModuleHandleExW(flags, addressInTestingLibrary, &testingLibraryAddress)) } diff --git a/Tests/TestingTests/Support/CErrorTests.swift b/Tests/TestingTests/Support/CErrorTests.swift index 07eebaf94..37ffd5fdb 100644 --- a/Tests/TestingTests/Support/CErrorTests.swift +++ b/Tests/TestingTests/Support/CErrorTests.swift @@ -26,9 +26,9 @@ struct CErrorTests { struct Win32ErrorTests { @Test("Win32Error.description property", arguments: [ - (DWORD(ERROR_OUTOFMEMORY), "Not enough memory resources are available to complete this operation."), - (DWORD(ERROR_INVALID_ACCESS), "The access code is invalid."), - (DWORD(ERROR_ARITHMETIC_OVERFLOW), "Arithmetic result exceeded 32 bits."), + (ERROR_OUTOFMEMORY, "Not enough memory resources are available to complete this operation."), + (ERROR_INVALID_ACCESS, "The access code is invalid."), + (ERROR_ARITHMETIC_OVERFLOW, "Arithmetic result exceeded 32 bits."), (999_999_999, "An unknown error occurred (999999999)."), ] ) diff --git a/Tests/TestingTests/Support/FileHandleTests.swift b/Tests/TestingTests/Support/FileHandleTests.swift index bc5b6c4ed..e16e95622 100644 --- a/Tests/TestingTests/Support/FileHandleTests.swift +++ b/Tests/TestingTests/Support/FileHandleTests.swift @@ -62,10 +62,10 @@ struct FileHandleTests { CreateFileW( path, GENERIC_READ, - DWORD(FILE_SHARE_READ | FILE_SHARE_WRITE), + FILE_SHARE_READ | FILE_SHARE_WRITE, nil, - DWORD(OPEN_ALWAYS), - DWORD(FILE_ATTRIBUTE_NORMAL), + OPEN_ALWAYS, + FILE_ATTRIBUTE_NORMAL, nil ) )