Skip to content

Commit acbcf04

Browse files
committed
Re-apply "[stdlib] Avoid storing NULL into a Builtin.RawPointer. (#2236)"
Re-apply 6b8cd5c, reverted in a14a836, now that the prespecialization assertions have been relaxed.
1 parent e1126b3 commit acbcf04

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

stdlib/public/core/StaticString.swift

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

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
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
4845

4946
/// If `_startPtrOrData` is a pointer, contains the length of the UTF-8 data
5047
/// in bytes.
@@ -68,7 +65,7 @@ public struct StaticString
6865
_precondition(
6966
hasPointerRepresentation,
7067
"StaticString should have pointer representation")
71-
return UnsafePointer(_startPtrOrData)
68+
return UnsafePointer(bitPattern: UInt(_startPtrOrData))!
7269
}
7370

7471
/// The stored Unicode scalar value.
@@ -79,9 +76,7 @@ public struct StaticString
7976
_precondition(
8077
!hasPointerRepresentation,
8178
"StaticString should have Unicode scalar representation")
82-
return UnicodeScalar(
83-
UInt32(UInt(bitPattern: UnsafePointer<Builtin.RawPointer>(_startPtrOrData)))
84-
)
79+
return UnicodeScalar(UInt32(UInt(_startPtrOrData)))
8580
}
8681

8782
/// If `self` stores a pointer to ASCII or UTF-8 code units, the
@@ -148,7 +143,10 @@ public struct StaticString
148143
utf8CodeUnitCount: Builtin.Word,
149144
isASCII: Builtin.Int1
150145
) {
151-
self._startPtrOrData = _start
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)
152150
self._utf8CodeUnitCount = utf8CodeUnitCount
153151
self._flags = Bool(isASCII) ? (0x2 as UInt8)._value : (0x0 as UInt8)._value
154152
}
@@ -158,11 +156,7 @@ public struct StaticString
158156
internal init(
159157
unicodeScalar: Builtin.Int32
160158
) {
161-
self._startPtrOrData =
162-
unsafeBitCast(
163-
UInt(UInt32(unicodeScalar)),
164-
to: OpaquePointer.self
165-
)._rawValue
159+
self._startPtrOrData = UInt(UInt32(unicodeScalar))._builtinWordValue
166160
self._utf8CodeUnitCount = 0._builtinWordValue
167161
self._flags = UnicodeScalar(_builtinUnicodeScalarLiteral: unicodeScalar).isASCII
168162
? (0x3 as UInt8)._value

0 commit comments

Comments
 (0)