Skip to content

[Reflection] Some bug fixes and other improvements #63535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 0 additions & 77 deletions stdlib/public/Reflection/Package.swift

This file was deleted.

3 changes: 0 additions & 3 deletions stdlib/public/Reflection/README.md

This file was deleted.

8 changes: 5 additions & 3 deletions stdlib/public/Reflection/Sources/Reflection/Case.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ public struct Case {

@inlinable
public init?(from instance: Any) {
guard Type(instance).isEnum else {
let instanceTy = type(of: instance)

guard Type(instanceTy).isEnum else {
return nil
}

var container = unsafeBitCast(instance, to: AnyExistentialContainer.self)

let tag = container.projectValue {
Metadata(type(of: instance)).enum.enumVWT.getEnumTag($0)
Metadata(instanceTy).enum.enumVWT.getEnumTag($0)
}

self.parent = Metadata(type(of: instance)).enum
self.parent = Metadata(instanceTy).enum
self.tag = Int(truncatingIfNeeded: tag)
}
}
Expand Down
3 changes: 1 addition & 2 deletions stdlib/public/Reflection/Sources/Reflection/Field.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ extension Field {
@inlinable
public var name: String {
guard parent.kind != .tuple else {
//return TupleMetadata(parent.ptr).elements[index].
return "hello"
return parent.tuple.elements[index].label
}

return parent.type.descriptor.fields[index].name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension GenericArguments: RandomAccessCollection {

let start = argumentPointer.unsafelyUnwrapped
let address = start + position * MemoryLayout<Type>.size
return address.loadUnaligned(as: Type.self)
return address.unprotectedLoad(as: Type.self)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension Case {
var value = pair.buffer

if isIndirect {
let owner = value.loadUnaligned(as: HeapObject.self)
let owner = value.unprotectedLoad(as: HeapObject.self)
value = swift_projectBox(owner)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ extension ClassDescriptor {
// 'TargetStoredClassMetadataBounds', by the time we access this it will
// have already been initialized way before for us. Thus, it is safe to
// access this value non-atomically.
return storedBounds.load(as: Int.self)
return storedBounds.unprotectedLoad(as: Int.self)
}

@inlinable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ extension GenericSignature.RequirementDescriptor {
@available(SwiftStdlib 5.9, *)
@inlinable
public var layoutKind: GenericSignature.LayoutKind {
address(for: \.requirement).loadUnaligned(
address(for: \.requirement).unprotectedLoad(
as: GenericSignature.LayoutKind.self
)
}
Expand All @@ -140,7 +140,7 @@ extension GenericSignature.RequirementDescriptor {
) -> Any.Type? {
_getTypeByMangledNameInContext(
UnsafePointer(parameter.ptr._rawValue),
UInt(parameter.length),
UInt(truncatingIfNeeded: parameter.length),
genericContext: context.ptr,
genericArguments: argPtr
)
Expand Down Expand Up @@ -269,7 +269,7 @@ extension GenericSignature.RequirementDescriptor {
func getGenericSignature(at address: UnsafeRawPointer) -> GenericSignature {
var address = address

let header = address.loadUnaligned(as: GenericSignature.Header.self)
let header = address.unprotectedLoad(as: GenericSignature.Header.self)
address += MemoryLayout<GenericSignature.Header>.size

let parameters = BufferView<GenericSignature.ParameterDescriptor>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ extension AnyExistentialContainer {
}
}

let alignMask = UInt(metadata.vwt.flags.alignmentMask)
let alignMask = UInt(truncatingIfNeeded:metadata.vwt.flags.alignmentMask)
let heapObjSize = UInt(MemoryLayout<Int>.size * 2)
let byteOffset = (heapObjSize + alignMask) & ~alignMask

return try withUnsafeMutablePointer(to: &self) {
let raw = UnsafeMutableRawPointer($0)
let heap = raw.loadUnaligned(as: UnsafeMutableRawPointer.self)
let heap = $0.raw.unprotectedLoad(as: UnsafeMutableRawPointer.self)

return try body(heap + Int(byteOffset))
return try body(heap + Int(truncatingIfNeeded: byteOffset))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extension TupleMetadata.Elements.Element {
// string
address -= MemoryLayout<Int>.size

guard let cString = address.loadUnaligned(
guard let cString = address.unprotectedLoad(
as: UnsafePointer<CChar>?.self
) else {
return ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension TypeMetadata {
@inlinable
public var descriptor: TypeDescriptor {
var address: UnsafeRawPointer

switch metadata.kind {
case .struct:
address = ptr + MemoryLayout<StructMetadata.Layout>.offset(of: \.descriptor)!
Expand All @@ -39,12 +39,12 @@ extension TypeMetadata {
default:
address = ptr + MemoryLayout<ClassMetadata.Layout>.offset(of: \.descriptor)!
}
address = address.loadUnaligned(as: UnsafeRawPointer.self)

address = address.unprotectedLoad(as: UnsafeRawPointer.self)

return TypeDescriptor(address)
}

@inlinable
public var genericArguments: UnsafeRawPointer {
switch metadata.kind {
Expand All @@ -68,17 +68,17 @@ extension TypeMetadata {
public var metadata: Metadata {
Metadata(ptr)
}

@inlinable
public var `class`: ClassMetadata {
ClassMetadata(ptr)
}

@inlinable
public var `enum`: EnumMetadata {
EnumMetadata(ptr)
}

@inlinable
public var `struct`: StructMetadata {
StructMetadata(ptr)
Expand All @@ -93,10 +93,10 @@ extension TypeMetadata {
if let ss = typeRef.standardSubstitution {
return ss
}

return _resolve(typeRef)
}

@usableFromInline
func _resolve(_ typeRef: MangledTypeReference) -> Any.Type? {
typeCache.getOrInsert(typeRef, from: self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ extension ValueWitnessTable {
_ dest: UnsafeMutableRawPointer,
_ src: UnsafeRawPointer
) -> UnsafeMutableRawPointer {
let address = layout.raw
let address = address(for: \.initializeBufferWithCopyOfBuffer)

return address.signedVWTInitializeBufferWithCopyOfBuffer(
dest,
Expand All @@ -139,17 +139,7 @@ extension ValueWitnessTable {

@inlinable
public func destroy(_ src: UnsafeMutableRawPointer) {
// rdar://103834325
// FIXME: There's currently a compiler bug preventing me from doing:
// 'address(of: \.destroy)'
// or even
// 'MemoryLayout<ValueWitnessTable.Layout.Pointee>.offset(of: \.destroy)'
//
// The same goes for everything else in this file
let address = layout.raw
+ MemoryLayout<InitializeBufferWithCopyOfBuffer>.size

address.signedVWTDestroy(src, trailing)
address(for: \.destroy).signedVWTDestroy(src, trailing)
}

@inlinable
Expand All @@ -158,9 +148,7 @@ extension ValueWitnessTable {
_ dest: UnsafeMutableRawPointer,
_ src: UnsafeRawPointer
) -> UnsafeMutableRawPointer {
let address = layout.raw
+ MemoryLayout<InitializeBufferWithCopyOfBuffer>.size
+ MemoryLayout<Destroy>.size
let address = address(for: \.initializeWithCopy)

return address.signedVWTInitializeWithCopy(dest, src, trailing)
}
Expand All @@ -171,10 +159,7 @@ extension ValueWitnessTable {
_ dest: UnsafeMutableRawPointer,
_ src: UnsafeRawPointer
) -> UnsafeMutableRawPointer {
let address = layout.raw
+ MemoryLayout<InitializeBufferWithCopyOfBuffer>.size
+ MemoryLayout<Destroy>.size
+ MemoryLayout<InitializeWithCopy>.size
let address = address(for: \.assignWithCopy)

return address.signedVWTAssignWithCopy(dest, src, trailing)
}
Expand All @@ -185,11 +170,7 @@ extension ValueWitnessTable {
_ dest: UnsafeMutableRawPointer,
_ src: UnsafeMutableRawPointer
) -> UnsafeMutableRawPointer {
let address = layout.raw
+ MemoryLayout<InitializeBufferWithCopyOfBuffer>.size
+ MemoryLayout<Destroy>.size
+ MemoryLayout<InitializeWithCopy>.size
+ MemoryLayout<AssignWithCopy>.size
let address = address(for: \.initializeWithTake)

return address.signedVWTInitializeWithTake(dest, src, trailing)
}
Expand All @@ -200,12 +181,7 @@ extension ValueWitnessTable {
_ dest: UnsafeMutableRawPointer,
_ src: UnsafeMutableRawPointer
) -> UnsafeMutableRawPointer {
let address = layout.raw
+ MemoryLayout<InitializeBufferWithCopyOfBuffer>.size
+ MemoryLayout<Destroy>.size
+ MemoryLayout<InitializeWithCopy>.size
+ MemoryLayout<AssignWithCopy>.size
+ MemoryLayout<InitializeWithTake>.size
let address = address(for: \.assignWithTake)

return address.signedVWTAssignWithTake(dest, src, trailing)
}
Expand All @@ -216,13 +192,7 @@ extension ValueWitnessTable {
_ src: UnsafeRawPointer,
_ numberOfEmptyCases: UInt32
) -> UInt32 {
let address = layout.raw
+ MemoryLayout<InitializeBufferWithCopyOfBuffer>.size
+ MemoryLayout<Destroy>.size
+ MemoryLayout<InitializeWithCopy>.size
+ MemoryLayout<AssignWithCopy>.size
+ MemoryLayout<InitializeWithTake>.size
+ MemoryLayout<AssignWithTake>.size
let address = address(for: \.getEnumTagSinglePayload)

return address.signedVWTGetEnumTagSinglePayload(
src,
Expand All @@ -237,14 +207,7 @@ extension ValueWitnessTable {
_ tag: UInt32,
_ numberOfEmptyCases: UInt32
) -> () {
let address = layout.raw
+ MemoryLayout<InitializeBufferWithCopyOfBuffer>.size
+ MemoryLayout<Destroy>.size
+ MemoryLayout<InitializeWithCopy>.size
+ MemoryLayout<AssignWithCopy>.size
+ MemoryLayout<InitializeWithTake>.size
+ MemoryLayout<AssignWithTake>.size
+ MemoryLayout<GetEnumTagSinglePayload>.size
let address = address(for: \.storeEnumTagSinglePayload)

return address.signedVWTStoreEnumTagSinglePayload(
src,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension BufferView: RandomAccessCollection {

@inlinable
public subscript(position: Int) -> Element {
start.loadUnaligned(
start.unprotectedLoad(
fromByteOffset: position * MemoryLayout<Element>.size,
as: Element.self
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension PublicLayout {
@inline(__always)
@inlinable
public var layout: Layout {
ptr.loadUnaligned(as: Layout.self)
ptr.unprotectedLoad(as: Layout.self)
}

@inline(__always)
Expand Down Expand Up @@ -74,7 +74,7 @@ protocol PrivateLayout {
@available(SwiftStdlib 5.9, *)
extension PrivateLayout {
var layout: Layout {
ptr.loadUnaligned(as: Layout.self)
ptr.unprotectedLoad(as: Layout.self)
}

var trailing: UnsafeRawPointer {
Expand Down
Loading