Skip to content

Commit a14a836

Browse files
author
Greg Parker
committed
Revert "[stdlib] Avoid storing NULL into a Builtin.RawPointer. (#2236)"
This broke test sil-opt/emit-sib.swift with compiler asserts enabled. https://ci.swift.org/job/oss-swift_tools-RA_stdlib-DA_test-simulator/1022/ This reverts commit 6b8cd5c.
1 parent abacf7e commit a14a836

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

stdlib/public/core/StaticString.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ public struct StaticString
3939
CustomDebugStringConvertible,
4040
CustomReflectable {
4141

42-
/// Either a pointer to the start of UTF-8 data, represented as an integer,
43-
/// or an integer representation of a single Unicode scalar.
44-
internal var _startPtrOrData: Builtin.Word
42+
// FIXME(ABI): RawPointer is non-nullable, but we can store a null Unicode
43+
// scalar in it. Change it to an integer.
44+
//
45+
/// Either a pointer to the start of UTF-8 data, or an integer representation
46+
/// of a single Unicode scalar.
47+
internal var _startPtrOrData: Builtin.RawPointer
4548

4649
/// If `_startPtrOrData` is a pointer, contains the length of the UTF-8 data
4750
/// in bytes.
@@ -65,7 +68,7 @@ public struct StaticString
6568
_precondition(
6669
hasPointerRepresentation,
6770
"StaticString should have pointer representation")
68-
return UnsafePointer(bitPattern: UInt(_startPtrOrData))!
71+
return UnsafePointer(_startPtrOrData)
6972
}
7073

7174
/// The stored Unicode scalar value.
@@ -76,7 +79,9 @@ public struct StaticString
7679
_precondition(
7780
!hasPointerRepresentation,
7881
"StaticString should have Unicode scalar representation")
79-
return UnicodeScalar(UInt32(UInt(_startPtrOrData)))
82+
return UnicodeScalar(
83+
UInt32(UInt(bitPattern: UnsafePointer<Builtin.RawPointer>(_startPtrOrData)))
84+
)
8085
}
8186

8287
/// If `self` stores a pointer to ASCII or UTF-8 code units, the
@@ -143,10 +148,7 @@ public struct StaticString
143148
utf8CodeUnitCount: Builtin.Word,
144149
isASCII: Builtin.Int1
145150
) {
146-
// We don't go through UnsafePointer here to make things simpler for alias
147-
// analysis. A higher-level algorithm may be trying to make sure an
148-
// unrelated buffer is not accessed or freed.
149-
self._startPtrOrData = Builtin.ptrtoint_Word(_start)
151+
self._startPtrOrData = _start
150152
self._utf8CodeUnitCount = utf8CodeUnitCount
151153
self._flags = Bool(isASCII) ? (0x2 as UInt8)._value : (0x0 as UInt8)._value
152154
}
@@ -156,7 +158,11 @@ public struct StaticString
156158
internal init(
157159
unicodeScalar: Builtin.Int32
158160
) {
159-
self._startPtrOrData = UInt(UInt32(unicodeScalar))._builtinWordValue
161+
self._startPtrOrData =
162+
unsafeBitCast(
163+
UInt(UInt32(unicodeScalar)),
164+
to: OpaquePointer.self
165+
)._rawValue
160166
self._utf8CodeUnitCount = 0._builtinWordValue
161167
self._flags = UnicodeScalar(_builtinUnicodeScalarLiteral: unicodeScalar).isASCII
162168
? (0x3 as UInt8)._value

0 commit comments

Comments
 (0)