Skip to content

Commit 21f0045

Browse files
authored
Merge pull request #4263 from apple/se-0136
Implementation of SE-0136 => Swift-3.0 branch
2 parents ff1875f + 4f4fab4 commit 21f0045

File tree

19 files changed

+67
-55
lines changed

19 files changed

+67
-55
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 6 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 33 additions & 14 deletions
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

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

stdlib/public/core/VarArgs.swift

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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)

0 commit comments

Comments
 (0)