Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension UInt32 {
extension AnyKeyPath {
private static var WORD_SIZE: Int { MemoryLayout<Int>.size }

func _validateForPredicateUsage(restrictArguments: Bool = false) {
func _validateForPredicateUsage(restrictArguments: Bool = true) {
var ptr = unsafeBitCast(self, to: UnsafeRawPointer.self)
ptr = ptr.advanced(by: Self.WORD_SIZE * 3) // skip isa, type metadata, and KVC string pointers
let header = ptr.load(as: UInt32.self)
Expand Down
101 changes: 101 additions & 0 deletions Tests/FoundationEssentialsTests/PredicateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,107 @@ private struct PredicateTests {
}
_ = #Predicate<Foo> { $0.id == 2 }
}

#if FOUNDATION_EXIT_TESTS
@Test func unsupportedKeyPaths() async {
struct Sample {
let stored: Sample2
var immutableComputed: Sample2 { fatalError() }
var mutableComputed: Sample2 {
get { fatalError() }
set { fatalError() }
}
var optional: Sample2? { fatalError() }

subscript(_ arg: Int) -> Sample2 { fatalError() }
subscript() -> Sample2 { fatalError() }
}

struct Sample2 {
var prop: Int
}

// multiple components
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample.stored.prop
)
}
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample.immutableComputed.prop
)
}
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample.mutableComputed.prop
)
}
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample.optional?.prop
)
}
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample.[1].prop
)
}

// subscripts with arguments
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample.[0]
)
}

// Optional chaining
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample.optional?
)
}
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample?.?
)
}
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample?.?.stored
)
}

// Force unwrapping
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample.optional!
)
}
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample?.!
)
}
await #expect(processExitsWith: .failure) {
_ = PredicateExpressions.KeyPath(
root: PredicateExpressions.Variable(),
keyPath: \Sample?.!.stored
)
}
}
#endif

@Test
func regex() throws {
Expand Down