Skip to content

[stdlib] Preserve random-access Mirror.children inside AnyCollection #33319

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

Closed
Closed
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
7 changes: 6 additions & 1 deletion stdlib/public/core/Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ public struct Mirror {

/// A collection of `Child` elements describing the structure of the
/// reflected subject.
public var children: Children { AnyCollection(_children) }
public var children: Children {
switch _children {
case let .left(l): return AnyCollection(l)
case let .right(r): return AnyCollection(r)
}
}

/// A suggested display style for the reflected subject.
public let displayStyle: DisplayStyle?
Expand Down
19 changes: 19 additions & 0 deletions test/stdlib/Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,25 @@ mirrors.test("LabeledStructure") {
expectEqual("[bark: 1, bite: Zee]", h.testDescription)
}

mirrors.test("ErasedChildren") {
let m1 = Mirror(reflecting: 1..<4)
let c1 = AnyRandomAccessCollection(m1.children)
expectNotNil(c1)
expectNil(c1?.first?.label)
expectTrue(c1?.first?.label as? Int == 1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be expectTrue(c1?.first?.value as? Int == 1)?

expectNil(c1?.last?.label)
expectTrue(c1?.last?.value as? Int == 3)

struct Foo { let i = 42; let s = "bar" }
let m2 = Mirror(reflecting: Foo())
let c2 = AnyRandomAccessCollection(m2.children)
expectNotNil(c2)
expectTrue(c2?.first?.label == "i")
expectTrue(c2?.first?.label as? Int == 42)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be expectTrue(c2?.first?.value as? Int == 42)?

expectTrue(c2?.last?.label == "s")
expectTrue(c2?.last?.value as? String == "bar")
}

mirrors.test("Legacy") {
let m = Mirror(reflecting: [1, 2, 3])
expectTrue(m.subjectType == [Int].self)
Expand Down