Skip to content

Commit 3bc9ce1

Browse files
authored
Merge pull request #22964 from moiseev/nil-unwrap-location
Print nil unwrap location in no-assert builds of stdlib
2 parents 48d8ebd + 2ce0630 commit 3bc9ce1

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

stdlib/public/core/Optional.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ func _diagnoseUnexpectedNilOptional(_filenameStart: Builtin.RawPointer,
300300
_filenameIsASCII: Builtin.Int1,
301301
_line: Builtin.Word,
302302
_isImplicitUnwrap: Builtin.Int1) {
303-
_preconditionFailure(
303+
// Cannot use _preconditionFailure as the file and line info would not be
304+
// printed.
305+
preconditionFailure(
304306
Bool(_isImplicitUnwrap)
305307
? "Unexpectedly found nil while implicitly unwrapping an Optional value"
306308
: "Unexpectedly found nil while unwrapping an Optional value",

test/stdlib/OptionalTraps.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,22 @@ OptionalTraps.test("UnwrapNone")
2525
{ _isFastAssertConfiguration() },
2626
reason: "this trap is not guaranteed to happen in -Ounchecked"))
2727
.code {
28-
var a: AnyObject? = returnNil()
28+
let a: AnyObject? = returnNil()
29+
expectCrashLater()
30+
let unwrapped: AnyObject = a!
31+
_blackHole(unwrapped)
32+
}
33+
34+
OptionalTraps.test("UnwrapNone/location")
35+
.skip(.custom(
36+
{ _isFastAssertConfiguration() },
37+
reason: "this trap is not guaranteed to happen in -Ounchecked"))
38+
.crashOutputMatches(_isDebugAssertConfiguration()
39+
? "test/stdlib/OptionalTraps.swift, line 45"
40+
: "")
41+
.code {
42+
expectCrashLater()
43+
let a: AnyObject? = returnNil()
2944
expectCrashLater()
3045
let unwrapped: AnyObject = a!
3146
_blackHole(unwrapped)

0 commit comments

Comments
 (0)