apple / swift-system

Low-level system calls and types for Swift
Apache License 2.0
1.18k stars 102 forks source link

Inconsistency with _dup2 on Windows #192

Open jakepetroules opened 2 months ago

jakepetroules commented 2 months ago

On Windows, _dup2 is documented to return 0 to indicate success, in contrast to POSIX's dup2, which returns the new file descriptor.

Should we change the Windows syscall adapter to something like:

@inline(__always)
internal func dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
  _dup2(fd, fd2)
  return fd2 // dup2 is documented on windows to return 0 on success while POSIX expects fd2, 
             // so always return fd2 for consistency
}

I understand the general spirit of swift-system is to match the platform behavior, but this one stands out as potentially quite surprising, so I wonder if we should at least call it out in the documentation comments for this function?

glessard commented 1 day ago

The current state of FileDescriptor on Windows is that it's become a questionable POSIX-emulation layer, and it shouldn't be this way. We should have the thinnest possible wrappers over the platform calls, with the translation limited to adapting language differences. Cross-platform behaviour should be implemented by swift-foundation.