Skip to content

Commit bf65431

Browse files
authored
Merge pull request #4907 from atrick/fix
2 parents f21cf8b + 2c9cde1 commit bf65431

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

stdlib/public/core/UnsafePointer.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,13 @@ public struct ${Self}<Pointee>
373373
/// - Precondition: The memory `self..<self + count * MemoryLayout<T>.stride`
374374
/// is bound to `Pointee`.
375375
public func withMemoryRebound<T, Result>(to: T.Type, capacity count: Int,
376-
_ body: (UnsafeMutablePointer<T>) throws -> Result
376+
_ body: (${Self}<T>) throws -> Result
377377
) rethrows -> Result {
378378
Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
379379
defer {
380380
Builtin.bindMemory(_rawValue, count._builtinWordValue, Pointee.self)
381381
}
382-
return try body(UnsafeMutablePointer<T>(_rawValue))
382+
return try body(${Self}<T>(_rawValue))
383383
}
384384

385385
/// Accesses the pointee at `self + i`.

test/stdlib/UnsafePointer.swift.gyb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,16 @@ ${SelfName}TestSuite.test("Comparable") {
457457
% for SelfName in ['UnsafePointer', 'UnsafeMutablePointer']:
458458

459459
${SelfName}TestSuite.test("withMemoryRebound") {
460-
let mutablePtr = UnsafeMutablePointer<Int>.allocate(capacity: 4)
461-
let ptrI = ${SelfName}<Int>(mutablePtr)
460+
let mutablePtrI = UnsafeMutablePointer<Int>.allocate(capacity: 4)
461+
defer { mutablePtrI.deallocate(capacity: 4) }
462+
let ptrI = ${SelfName}<Int>(mutablePtrI)
462463
ptrI.withMemoryRebound(to: UInt.self, capacity: 4) {
463-
expectType(UInt.self, &$0.pointee)
464+
// Make sure the closure argument isa $SelfName
465+
var ptrU: ${SelfName}<UInt> = $0
466+
// and that the element type is UInt.
467+
var mutablePtrU = UnsafeMutablePointer(mutating: ptrU)
468+
expectType(UInt.self, &mutablePtrU.pointee)
464469
}
465-
mutablePtr.deallocate(capacity: 4)
466470
}
467471

468472
% end

0 commit comments

Comments
 (0)