From 645cbea2f0ac8c16f2cf349473ac2f759601d796 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Sat, 19 Feb 2022 11:06:38 +0000 Subject: [PATCH 1/3] Add `OpaquePointer` implementation --- Sources/Core/OpaquePointer.swift | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Sources/Core/OpaquePointer.swift diff --git a/Sources/Core/OpaquePointer.swift b/Sources/Core/OpaquePointer.swift new file mode 100644 index 0000000..9f22fec --- /dev/null +++ b/Sources/Core/OpaquePointer.swift @@ -0,0 +1,48 @@ +// Copyright © 2022 Max Desiatov . +// All Rights Reserved. +// SPDX-License-Identifier: BSD-3 + +@frozen +public struct OpaquePointer { + @usableFromInline + internal var _rawValue: Builtin.RawPointer + + @usableFromInline @_transparent + internal init(_ v: Builtin.RawPointer) { + _rawValue = v + } + + @_transparent + public init?(bitPattern: Int) { + guard bitPattern != 0 else { return nil } + _rawValue = Builtin.inttoptr_Word(bitPattern._value) + } + + @_transparent + public init?(bitPattern: UInt) { + guard bitPattern != 0 else { return nil } + _rawValue = Builtin.inttoptr_Word(bitPattern._value) + } + + @_transparent + public init(@_nonEphemeral _ from: UnsafePointer) { + _rawValue = from._rawValue + } + + @_transparent + public init?(@_nonEphemeral _ from: UnsafePointer?) { + guard let unwrapped = from else { return nil } + self.init(unwrapped) + } + + @_transparent + public init(@_nonEphemeral _ from: UnsafeMutablePointer) { + _rawValue = from._rawValue + } + + @_transparent + public init?(@_nonEphemeral _ from: UnsafeMutablePointer?) { + guard let unwrapped = from else { return nil } + self.init(unwrapped) + } +} From 5f4eff0b28b22f4850d208487a866a97b18c49bf Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Sat, 19 Feb 2022 11:20:13 +0000 Subject: [PATCH 2/3] Add `OpaquePointer.swift` to `CMakeLists.txt` --- Sources/Core/CMakeLists.txt | 1 + Sources/Core/Int.swift | 3 +-- Sources/Core/UInt.swift | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Sources/Core/CMakeLists.txt b/Sources/Core/CMakeLists.txt index a09687e..ae1806d 100644 --- a/Sources/Core/CMakeLists.txt +++ b/Sources/Core/CMakeLists.txt @@ -15,6 +15,7 @@ add_library(swiftCore Int32.swift Integers.swift Never.swift + OpaquePointer.swift Optional.swift OptionSet.swift Policy.swift diff --git a/Sources/Core/Int.swift b/Sources/Core/Int.swift index e357970..bafa752 100644 --- a/Sources/Core/Int.swift +++ b/Sources/Core/Int.swift @@ -15,8 +15,7 @@ extension Int: _ExpressibleByBuiltinIntegerLiteral { } } -extension Int: ExpressibleByIntegerLiteral { -} +extension Int: ExpressibleByIntegerLiteral {} extension Int: Equatable { @_transparent diff --git a/Sources/Core/UInt.swift b/Sources/Core/UInt.swift index 45fff3b..57cfd9e 100644 --- a/Sources/Core/UInt.swift +++ b/Sources/Core/UInt.swift @@ -15,8 +15,7 @@ extension UInt: _ExpressibleByBuiltinIntegerLiteral { } } -extension UInt: ExpressibleByIntegerLiteral { -} +extension UInt: ExpressibleByIntegerLiteral {} extension UInt: Equatable { @_transparent From f324ab293785ee49e1fb02026d6dff9b622543ae Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Sat, 19 Feb 2022 11:34:01 +0000 Subject: [PATCH 3/3] Exclude failing 5.4 and 5.5 in `OpaquePointer` --- Sources/Core/Int.swift | 3 ++- Sources/Core/OpaquePointer.swift | 9 +++++++-- Sources/Core/UInt.swift | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/Core/Int.swift b/Sources/Core/Int.swift index bafa752..e357970 100644 --- a/Sources/Core/Int.swift +++ b/Sources/Core/Int.swift @@ -15,7 +15,8 @@ extension Int: _ExpressibleByBuiltinIntegerLiteral { } } -extension Int: ExpressibleByIntegerLiteral {} +extension Int: ExpressibleByIntegerLiteral { +} extension Int: Equatable { @_transparent diff --git a/Sources/Core/OpaquePointer.swift b/Sources/Core/OpaquePointer.swift index 9f22fec..8744a2b 100644 --- a/Sources/Core/OpaquePointer.swift +++ b/Sources/Core/OpaquePointer.swift @@ -2,6 +2,9 @@ // All Rights Reserved. // SPDX-License-Identifier: BSD-3 +// This `OpaquePointer` implementation is known to crash 5.4 and 5.5 compiler releases on Windows. +#if swift(>=5.6) + @frozen public struct OpaquePointer { @usableFromInline @@ -14,13 +17,13 @@ public struct OpaquePointer { @_transparent public init?(bitPattern: Int) { - guard bitPattern != 0 else { return nil } + if bitPattern == 0 { return nil } _rawValue = Builtin.inttoptr_Word(bitPattern._value) } @_transparent public init?(bitPattern: UInt) { - guard bitPattern != 0 else { return nil } + if bitPattern == 0 { return nil } _rawValue = Builtin.inttoptr_Word(bitPattern._value) } @@ -46,3 +49,5 @@ public struct OpaquePointer { self.init(unwrapped) } } + +#endif diff --git a/Sources/Core/UInt.swift b/Sources/Core/UInt.swift index 57cfd9e..45fff3b 100644 --- a/Sources/Core/UInt.swift +++ b/Sources/Core/UInt.swift @@ -15,7 +15,8 @@ extension UInt: _ExpressibleByBuiltinIntegerLiteral { } } -extension UInt: ExpressibleByIntegerLiteral {} +extension UInt: ExpressibleByIntegerLiteral { +} extension UInt: Equatable { @_transparent