Skip to content

[stdlibUnittest, 6.2] generalize expectNil(_:) #82703

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
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
3 changes: 3 additions & 0 deletions stdlib/private/StdlibUnittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ endif()

set(swift_stdlib_unittest_compile_flags
"-Xfrontend" "-disable-objc-attr-requires-foundation-module")

list(APPEND swift_stdlib_unittest_compile_flags "-enable-experimental-feature" "Lifetimes")

if (SWIFT_RUNTIME_ENABLE_LEAK_CHECKER)
list(APPEND swift_stdlib_unittest_compile_flags "-DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER")
endif()
Expand Down
28 changes: 24 additions & 4 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -739,20 +739,40 @@ public func expectDoesNotThrow(_ test: () throws -> Void,
}
}

public func expectNil<T>(_ value: T?,
public func expectNil<T>(
_ value: T?,
_ message: @autoclosure () -> String = "",
stackTrace: SourceLocStack = SourceLocStack(),
showFrame: Bool = true,
file: String = #file, line: UInt = #line) {
file: String = #file, line: UInt = #line
) {
if value != nil {
expectationFailure(
"expected optional to be nil\nactual: \"\(value!)\"", trace: message(),
"expected optional to be nil\nactual: \"\(value!)\"",
trace: message(),
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line))
}
}

@_lifetime(copy value)
public func expectNil<T: ~Copyable & ~Escapable>(
_ value: borrowing T?,
_ message: @autoclosure () -> String = "",
stackTrace: SourceLocStack = SourceLocStack(),
showFrame: Bool = true,
file: String = #file, line: UInt = #line
) {
if value != nil {
expectationFailure(
"expected optional to be nil",
trace: message(),
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)
)
}
}

@discardableResult
@lifetime(copy value)
@_lifetime(copy value)
public func expectNotNil<T: ~Copyable & ~Escapable>(
_ value: consuming T?,
_ message: @autoclosure () -> String = "",
Expand Down
29 changes: 24 additions & 5 deletions test/stdlib/OptionalGeneralizations.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// RUN: %target-run-simple-swift(-enable-experimental-feature NonescapableTypes -enable-experimental-feature LifetimeDependence)
// RUN: %target-run-simple-swift(-enable-experimental-feature Lifetimes)
// REQUIRES: executable_test
// REQUIRES: reflection
// REQUIRES: swift_feature_NonescapableTypes
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_Lifetimes

import StdlibUnittest
import Swift
Expand Down Expand Up @@ -99,7 +98,7 @@ suite.test("expectNotNil()") {
_ = expectNotNil(opt1(NoncopyableStruct()))
_ = expectNotNil(opt1(RegularClass()))
#if $NonescapableTypes
@lifetime(copy t)
@_lifetime(copy t)
func opt2<T: ~Copyable & ~Escapable>(_ t: consuming T) -> T? { t }

let ne = NonescapableStruct()
Expand All @@ -110,5 +109,25 @@ suite.test("expectNotNil()") {

let nent = NonescapableNontrivialStruct()
_ = expectNotNil(opt2(nent))
#endif
#endif // $NonescapableTypes
}

suite.test("expectNil()") {
func opt1<T: ~Copyable>(_ t: consuming T) -> T? { nil }
expectNil(opt1(TrivialStruct()))
expectNil(opt1(NoncopyableStruct()))
expectNil(opt1(RegularClass()))
#if $NonescapableTypes
@_lifetime(copy t)
func opt2<T: ~Copyable & ~Escapable>(_ t: consuming T) -> T? { nil }

let ne = NonescapableStruct()
expectNil(opt2(ne))

let ncne = NoncopyableNonescapableStruct()
expectNil(opt2(ncne))

let nent = NonescapableNontrivialStruct()
expectNil(opt2(nent))
#endif // $NonescapableTypes
}