Skip to content

Commit f9435b9

Browse files
committed
[stdlib] Restore MemoryLayout.*(ofValue:)
1 parent bd55d89 commit f9435b9

19 files changed

+64
-55
lines changed

lib/Sema/MiscDiagnostics.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -2145,9 +2145,6 @@ bool AvailabilityWalker::diagnoseMemoryLayoutMigration(const ValueDecl *D,
21452145
.Case("sizeof", {"size", false})
21462146
.Case("alignof", {"alignment", false})
21472147
.Case("strideof", {"stride", false})
2148-
.Case("sizeofValue", {"size", true})
2149-
.Case("alignofValue", {"alignment", true})
2150-
.Case("strideofValue", {"stride", true})
21512148
.Default({});
21522149

21532150
if (KindValue.first.empty())
@@ -2162,7 +2159,7 @@ bool AvailabilityWalker::diagnoseMemoryLayoutMigration(const ValueDecl *D,
21622159

21632160
auto subject = args->getSubExpr();
21642161
if (!isValue) {
2165-
// sizeof(x.dynamicType) is equivalent to sizeofValue(x)
2162+
// sizeof(type(of: x)) is equivalent to sizeofValue(x)
21662163
if (auto DTE = dyn_cast<DynamicTypeExpr>(subject)) {
21672164
subject = DTE->getBase();
21682165
isValue = true;
@@ -2179,14 +2176,14 @@ bool AvailabilityWalker::diagnoseMemoryLayoutMigration(const ValueDecl *D,
21792176
if (isValue) {
21802177
auto valueType = subject->getType()->getRValueType();
21812178
if (!valueType || valueType->is<ErrorType>()) {
2182-
// If we dont have good argument, We cannot emit fix-it.
2183-
return true;
2179+
// If we don't have a suitable argument, we cannot emit a fix-it.
2180+
return true;
21842181
}
21852182

21862183
// NOTE: We are destructively replacing the source text here.
2187-
// For instance, `sizeof(x.doSomethig())` => `MemoryLayout<T>.size` where
2188-
// T is return type of `doSomething()`. If that function have any
2189-
// side effects, it will break the source.
2184+
// `sizeof(type(of: doSomething()))` => `MemoryLayout<T>.size`, where T is
2185+
// the return type of `doSomething()`. If that function has any side
2186+
// effects, this replacement will break the source.
21902187
diag.fixItReplace(call->getSourceRange(),
21912188
(Prefix + valueType->getString() + Suffix + Kind).str());
21922189
} else {

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public func spawnChild(_ args: [String])
116116

117117
// If execve() encountered an error, we write the errno encountered to the
118118
// parent write pipe.
119-
let errnoSize = MemoryLayout._ofInstance(errno).size
119+
let errnoSize = MemoryLayout.size(ofValue: errno)
120120
var execveErrno = errno
121121
let writtenBytes = withUnsafePointer(to: &execveErrno) {
122122
write(childToParentPipe.writeFD, UnsafePointer($0), errnoSize)

stdlib/public/SDK/Foundation/Data.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
669669
public mutating func next() -> UInt8? {
670670
guard _idx < _endIdx else { return nil }
671671
defer { _idx += 1 }
672-
let bufferSize = MemoryLayout._ofInstance(_buffer).size
672+
let bufferSize = MemoryLayout.size(ofValue: _buffer)
673673
return withUnsafeMutablePointer(to: &_buffer) { ptr_ in
674674
let ptr = UnsafeMutableRawPointer(ptr_).assumingMemoryBound(to: UInt8.self)
675675
let bufferIdx = _idx % bufferSize

stdlib/public/core/BridgeObjectiveC.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ internal struct _CocoaFastEnumerationStackBuf {
546546
_item14 = _item0
547547
_item15 = _item0
548548

549-
_sanityCheck(MemoryLayout._ofInstance(self).size >=
549+
_sanityCheck(MemoryLayout.size(ofValue: self) >=
550550
MemoryLayout<Optional<UnsafeRawPointer>>.size * count)
551551
}
552552
}

stdlib/public/core/Builtin.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public func sizeof<T>(_:T.Type) -> Int {
2020
Builtin.unreachable()
2121
}
2222

23-
@available(*, unavailable, message: "use MemoryLayout<T>.size instead.")
23+
@available(*, unavailable, renamed: "MemoryLayout.size(ofValue:)")
2424
public func sizeofValue<T>(_:T) -> Int {
2525
Builtin.unreachable()
2626
}
@@ -30,7 +30,7 @@ public func alignof<T>(_:T.Type) -> Int {
3030
Builtin.unreachable()
3131
}
3232

33-
@available(*, unavailable, message: "use MemoryLayout<T>.alignment instead.")
33+
@available(*, unavailable, renamed: "MemoryLayout.alignment(ofValue:)")
3434
public func alignofValue<T>(_:T) -> Int {
3535
Builtin.unreachable()
3636
}
@@ -40,7 +40,7 @@ public func strideof<T>(_:T.Type) -> Int {
4040
Builtin.unreachable()
4141
}
4242

43-
@available(*, unavailable, message: "use MemoryLayout<T>.stride instead.")
43+
@available(*, unavailable, renamed: "MemoryLayout.stride(ofValue:)")
4444
public func strideofValue<T>(_:T) -> Int {
4545
Builtin.unreachable()
4646
}

stdlib/public/core/Character.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public struct Character :
176176
let (count, initialUTF8) = s._core._encodeSomeUTF8(from: 0)
177177
// Notice that the result of sizeof() is a small non-zero number and can't
178178
// overflow when multiplied by 8.
179-
let bits = MemoryLayout._ofInstance(initialUTF8).size &* 8 &- 1
179+
let bits = MemoryLayout.size(ofValue: initialUTF8) &* 8 &- 1
180180
if _fastPath(
181181
count == s._core.count && (initialUTF8 & (1 << numericCast(bits))) != 0) {
182182
_representation = .small(Builtin.trunc_Int64_Int63(initialUTF8._value))

stdlib/public/core/MemoryLayout.swift

+33-14
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,59 @@
1212

1313
/// The memory layout of a type, describing its size, stride, and alignment.
1414
public enum MemoryLayout<T> {
15-
16-
/// The contiguous memory footprint of the type.
15+
/// The contiguous memory footprint of `T`.
1716
///
18-
/// The `size` property for a type `T` does not include any
19-
/// dynamically-allocated or "remote" storage. In particular,
20-
/// `MemoryLayout<T>.size`, when `T` is a class type, is the same regardless
21-
/// of how many stored properties `T` has.
17+
/// Does not include any dynamically-allocated or "remote" storage. In
18+
/// particular, `MemoryLayout<T>.size`, when `T` is a class type, is the same
19+
/// regardless of how many stored properties `T` has.
2220
@_transparent
2321
public static var size: Int {
2422
return Int(Builtin.sizeof(T.self))
2523
}
2624

27-
/// The number of bytes from the start of one instance to the start of the
28-
/// next, when stored in a contiguous array.
25+
/// The number of bytes from the start of one instance of `T` to the start of
26+
/// the next in an `Array<T>`.
2927
///
3028
/// This is the same as the number of bytes moved when an `UnsafePointer<T>`
31-
/// is incremented. The type may have a lower minimal alignment that trades
32-
/// runtime performance for space efficiency. The result is always positive.
29+
/// is incremented. `T` may have a lower minimal alignment that trades runtime
30+
/// performance for space efficiency. The result is always positive.
3331
@_transparent
3432
public static var stride: Int {
3533
return Int(Builtin.strideof_nonzero(T.self))
3634
}
3735

38-
/// The default memory alignment of the type.
36+
/// The default memory alignment of `T`.
3937
@_transparent
4038
public static var alignment: Int {
4139
return Int(Builtin.alignof(T.self))
4240
}
4341
}
4442

4543
extension MemoryLayout {
44+
/// Returns the contiguous memory footprint of `T`.
45+
///
46+
/// Does not include any dynamically-allocated or "remote" storage. In
47+
/// particular, `MemoryLayout.size(ofValue: x)`, when `x` is a class instance,
48+
/// is the same regardless of how many stored properties `T` has.
49+
@_transparent
50+
public static func size(ofValue _: T) -> Int {
51+
return MemoryLayout.size
52+
}
53+
54+
/// Returns the number of bytes from the start of one instance of `T` to the
55+
/// start of the next in an `Array<T>`.
56+
///
57+
/// This is the same as the number of bytes moved when an `UnsafePointer<T>`
58+
/// is incremented. `T` may have a lower minimal alignment that trades runtime
59+
/// performance for space efficiency. The result is always positive.
60+
@_transparent
61+
public static func stride(ofValue _: T) -> Int {
62+
return MemoryLayout.stride
63+
}
64+
65+
/// Returns the default memory alignment of `T`.
4666
@_transparent
47-
public // @testable
48-
static func _ofInstance(_: @autoclosure () -> T) -> MemoryLayout<T>.Type {
49-
return MemoryLayout<T>.self
67+
public static func alignment(ofValue _: T) -> Int {
68+
return MemoryLayout.alignment
5069
}
5170
}

stdlib/public/core/Unicode.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ internal func _transcodeSomeUTF16AsUTF8<Input>(
839839
nextIndex = input.index(nextIndex, offsetBy: utf16Length)
840840
}
841841
// FIXME: Annoying check, courtesy of <rdar://problem/16740169>
842-
if utf8Count < MemoryLayout._ofInstance(result).size {
842+
if utf8Count < MemoryLayout.size(ofValue: result) {
843843
result |= ~0 << numericCast(utf8Count * 8)
844844
}
845845
return (nextIndex, result)

stdlib/public/core/VarArgs.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ extension Int64 : CVarArg, _CVarArgAligned {
144144
/// the value returned by `_cVarArgEncoding`.
145145
public var _cVarArgAlignment: Int {
146146
// FIXME: alignof differs from the ABI alignment on some architectures
147-
return MemoryLayout._ofInstance(self).alignment
147+
return MemoryLayout.alignment(ofValue: self)
148148
}
149149
}
150150

@@ -192,7 +192,7 @@ extension UInt64 : CVarArg, _CVarArgAligned {
192192
/// the value returned by `_cVarArgEncoding`.
193193
public var _cVarArgAlignment: Int {
194194
// FIXME: alignof differs from the ABI alignment on some architectures
195-
return MemoryLayout._ofInstance(self).alignment
195+
return MemoryLayout.alignment(ofValue: self)
196196
}
197197
}
198198

@@ -265,7 +265,7 @@ extension Float : _CVarArgPassedAsDouble, _CVarArgAligned {
265265
/// the value returned by `_cVarArgEncoding`.
266266
public var _cVarArgAlignment: Int {
267267
// FIXME: alignof differs from the ABI alignment on some architectures
268-
return MemoryLayout._ofInstance(Double(self)).alignment
268+
return MemoryLayout.alignment(ofValue: Double(self))
269269
}
270270
}
271271

@@ -280,7 +280,7 @@ extension Double : _CVarArgPassedAsDouble, _CVarArgAligned {
280280
/// the value returned by `_cVarArgEncoding`.
281281
public var _cVarArgAlignment: Int {
282282
// FIXME: alignof differs from the ABI alignment on some architectures
283-
return MemoryLayout._ofInstance(self).alignment
283+
return MemoryLayout.alignment(ofValue: self)
284284
}
285285
}
286286

test/1_stdlib/Character.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ CharacterTests.test("sizeof") {
142142
expectTrue(size1 == 8 || size1 == 9)
143143

144144
var a: Character = "a"
145-
let size2 = MemoryLayout._ofInstance(a).size
145+
let size2 = MemoryLayout.size(ofValue: a)
146146
expectTrue(size2 == 8 || size2 == 9)
147147

148148
expectEqual(size1, size2)

test/1_stdlib/Renames.swift

-3
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,6 @@ func _MemoryLayout<T>(t: T) {
284284
_ = sizeof(T.self) // expected-error {{'sizeof' is unavailable: use MemoryLayout<T>.size instead.}} {{7-14=MemoryLayout<}} {{15-21=>.size}} {{none}}
285285
_ = alignof(T.self) // expected-error {{'alignof' is unavailable: use MemoryLayout<T>.alignment instead.}} {{7-15=MemoryLayout<}} {{16-22=>.alignment}} {{none}}
286286
_ = strideof(T.self) // expected-error {{'strideof' is unavailable: use MemoryLayout<T>.stride instead.}} {{7-16=MemoryLayout<}} {{17-23=>.stride}} {{none}}
287-
_ = sizeofValue(t) // expected-error {{'sizeofValue' is unavailable: use MemoryLayout<T>.size instead.}} {{7-21=MemoryLayout<T>.size}} {{none}}
288-
_ = alignofValue(t) // expected-error {{'alignofValue' is unavailable: use MemoryLayout<T>.alignment instead.}} {{7-22=MemoryLayout<T>.alignment}} {{none}}
289-
_ = strideofValue(t) // expected-error {{'strideofValue' is unavailable: use MemoryLayout<T>.stride instead.}} {{7-23=MemoryLayout<T>.stride}} {{none}}
290287
}
291288

292289
func _Mirror() {

test/Interpreter/SDK/c_pointers.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ puts(s)
127127
//
128128

129129
var unsorted = [3, 14, 15, 9, 2, 6, 5]
130-
qsort(&unsorted, unsorted.count, MemoryLayout._ofInstance(unsorted[0]).size) { a, b in
130+
qsort(&unsorted, unsorted.count, MemoryLayout.size(ofValue: unsorted[0])) { a, b in
131131
return Int32(a!.load(as: Int.self) - b!.load(as: Int.self))
132132
}
133133
// CHECK-NEXT: [2, 3, 5, 6, 9, 14, 15]

test/Interpreter/enum.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ struct OptionalTuple<T> {
440440
}
441441
}
442442
func test_optional_generic_tuple<T>(_ a: OptionalTuple<T>) -> T {
443-
print("optional pair is same size as pair: \(MemoryLayout._ofInstance(a).size == MemoryLayout<T>.size*2)")
443+
print("optional pair is same size as pair: \(MemoryLayout.size(ofValue: a) == MemoryLayout<T>.size*2)")
444444
return a.value!.0
445445
}
446446
print("Int result: \(test_optional_generic_tuple(OptionalTuple<Int>((5, 6))))")

test/expr/expressions.swift

+1-5
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,5 @@ func se0101<P: Pse0101>(x: Cse0101<P>) {
891891
_ = sizeof(Cse0101<P>.self) // expected-error {{'sizeof' is unavailable: use MemoryLayout<T>.size instead.}} {{7-14=MemoryLayout<}} {{24-30=>.size}} {{none}}
892892
_ = alignof(Cse0101<P>.T.self) // expected-error {{'alignof' is unavailable: use MemoryLayout<T>.alignment instead.}} {{7-15=MemoryLayout<}} {{27-33=>.alignment}} {{none}}
893893
_ = strideof(P.Type.self) // expected-error {{'strideof' is unavailable: use MemoryLayout<T>.stride instead.}} {{7-16=MemoryLayout<}} {{22-28=>.stride}} {{none}}
894-
_ = sizeof(type(of: x)) // expected-error {{'sizeof' is unavailable: use MemoryLayout<T>.size instead.}} {{7-26=MemoryLayout<Cse0101<P>>.size}} {{none}}/
895-
896-
_ = sizeofValue(x) // expected-error {{'sizeofValue' is unavailable: use MemoryLayout<T>.size instead.}} {{7-21=MemoryLayout<Cse0101<P>>.size}} {{none}}
897-
_ = alignofValue(x.val) // expected-error {{'alignofValue' is unavailable: use MemoryLayout<T>.alignment instead.}} {{7-26=MemoryLayout<P>.alignment}} {{none}}
898-
_ = strideofValue(x.val.getIt()) // expected-error {{'strideofValue' is unavailable: use MemoryLayout<T>.stride instead.}} {{7-35=MemoryLayout<P.Value>.stride}} {{none}}
894+
_ = sizeof(type(of: x)) // expected-error {{'sizeof' is unavailable: use MemoryLayout<T>.size instead.}} {{7-26=MemoryLayout<Cse0101<P>>.size}} {{none}}
899895
}

validation-test/stdlib/Arrays.swift.gyb

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ var ArrayTestSuite = TestSuite("Array")
8787
ArrayTestSuite.test("sizeof") {
8888
var a = [ 10, 20, 30 ]
8989
#if arch(i386) || arch(arm)
90-
expectEqual(4, MemoryLayout._ofInstance(a).size)
90+
expectEqual(4, MemoryLayout.size(ofValue: a))
9191
#else
92-
expectEqual(8, MemoryLayout._ofInstance(a).size)
92+
expectEqual(8, MemoryLayout.size(ofValue: a))
9393
#endif
9494
}
9595

validation-test/stdlib/Dictionary.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ DictionaryTestSuite.test("AssociatedTypes") {
7272
DictionaryTestSuite.test("sizeof") {
7373
var dict = [1: "meow", 2: "meow"]
7474
#if arch(i386) || arch(arm)
75-
expectEqual(4, MemoryLayout._ofInstance(dict).size)
75+
expectEqual(4, MemoryLayout.size(ofValue: dict))
7676
#else
77-
expectEqual(8, MemoryLayout._ofInstance(dict).size)
77+
expectEqual(8, MemoryLayout.size(ofValue: dict))
7878
#endif
7979
}
8080

validation-test/stdlib/NSNumberBridging.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ extension ${Self} {
151151
func toNSNumberByteArray() -> [UInt8] {
152152
var v = self.bitPattern
153153
var result: [UInt8] = []
154-
for _ in 0 ..< MemoryLayout._ofInstance(v).size {
154+
for _ in 0 ..< MemoryLayout.size(ofValue: v) {
155155
result.append(UInt8(v & 0xff))
156156
v = v >> 8
157157
}

validation-test/stdlib/OpenCLSDKOverlay.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ tests.test("clSetKernelArgsListAPPLE") {
194194
kernel!, 3,
195195
0, MemoryLayout<cl_mem>.size, inputPtr,
196196
1, MemoryLayout<cl_mem>.size, outputPtr,
197-
2, MemoryLayout._ofInstance(count).size, countPtr)
197+
2, MemoryLayout.size(ofValue: count), countPtr)
198198
}
199199
}
200200
}
@@ -208,7 +208,7 @@ tests.test("clSetKernelArgsListAPPLE") {
208208

209209
// Get the maximum work group size for executing the kernel on the device
210210
//
211-
err = clGetKernelWorkGroupInfo(kernel, device_id, cl_kernel_work_group_info(CL_KERNEL_WORK_GROUP_SIZE), MemoryLayout._ofInstance(local).size, &local, nil)
211+
err = clGetKernelWorkGroupInfo(kernel, device_id, cl_kernel_work_group_info(CL_KERNEL_WORK_GROUP_SIZE), MemoryLayout.size(ofValue: local), &local, nil)
212212
if (err != CL_SUCCESS)
213213
{
214214
print("Error: Failed to retrieve kernel work group info! \(err)")

validation-test/stdlib/Set.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ SetTestSuite.test("AssociatedTypes") {
325325
SetTestSuite.test("sizeof") {
326326
var s = Set(["Hello", "world"])
327327
#if arch(i386) || arch(arm)
328-
expectEqual(4, MemoryLayout._ofInstance(s).size)
328+
expectEqual(4, MemoryLayout.size(ofValue: s))
329329
#else
330-
expectEqual(8, MemoryLayout._ofInstance(s).size)
330+
expectEqual(8, MemoryLayout.size(ofValue: s))
331331
#endif
332332
}
333333

0 commit comments

Comments
 (0)