It looks like using #require and #expect with noncopyable types is not currently supported. See this basic example:
struct A: ~Copyable {
init?() {}
}
@Test func testA() {
let a = #require(A())
}
which gives the error:
Noncopyable type 'A' cannot be substituted for copyable generic parameter 'T' in 'require(_:_:sourceLocation:)'
A similar error occurs with #expect(a != nil).
Is support for noncopyable types planned, and is there a workaround at the moment for using them in tests?
I suppose a current workaround is using normal Swift control flow like guard to unwrap the optional and throwing an error if it fails
grynspan
(Jonathan Grynspan)
3
Support for non-copyable types requires changes to the Swift standard library and runtime that have not landed yet. We do want to support them but it's not planned for Swift 6.
1 Like
Joe_Groff
(Joe Groff)
4
In many cases, if you need to traffic in copyable types but you have a noncopyable value, you might be able to wrap it in a box object class.
grynspan
(Jonathan Grynspan)
5
In the interim, I've filed Add support for non-copyable types as expectation arguments · Issue #543 · apple/swift-testing · GitHub to track eventual support for move-only types. There are a number of limitations we'll need to overcome before we can make things work.