-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
292: Consistently initialize `Exception.name` across back-ends r=saem a=zerbina Creating an `Exception` without raising it left it's `name` field as nil on the JS and VM back-ends. For the C targets, the name initialization was injected during code-gen. Initializing the `name` field now either happens in `newException` or when raising the exception and thus works the same across all back-ends. Creating an exception without `newException` doesn't set the `name` field on the C back-ends anymore. In addition, when raising an exception on the JS target, the exception's name is not overridden if it's non-nil. This is a breaking change. Co-authored-by: zerbina <[email protected]>
- Loading branch information
Showing
6 changed files
with
81 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
discard """ | ||
description: "Tests to make sure that the Exception.name field is set" | ||
targets: "c cpp js" | ||
matrix: "--gc:refc; --gc:arc" | ||
""" | ||
|
||
template test(desc, body) {.dirty.} = | ||
block: | ||
# Don't execute at top level | ||
proc wrapper() = | ||
body | ||
|
||
# TODO: once it's available in testament, use the VM target instead | ||
static: wrapper() | ||
wrapper() | ||
|
||
|
||
test "Valid name without raising first": | ||
let e1 = IOError.newException("") | ||
doAssert e1.name == "IOError" | ||
|
||
let e2 = ValueError.newException("") | ||
doAssert e2.name == "ValueError" | ||
|
||
|
||
test "Valid name after raising": | ||
|
||
proc raiseTest[T](typ: typedesc[T], name: string) = | ||
try: | ||
raise T.newException("") | ||
except T as e: | ||
doAssert e.name == name | ||
|
||
raiseTest(IOError, "IOError") | ||
raiseTest(ValueError, "ValueError") | ||
|
||
|
||
test "Empty name": | ||
|
||
let e = (ref CatchableError)() | ||
doAssert e.name == nil | ||
|
||
|
||
test "Name is set on raise if it was unset": | ||
try: | ||
raise (ref IOError)() # leave `name` as `nil` | ||
except IOError as e: | ||
doAssert e.name == "IOError" | ||
|
||
|
||
# fails for the VM. See ``texception_name_vm_issue.nim`` | ||
block no_override_empty: | ||
try: | ||
raise (ref IOError)(name: "") # explicitly set name to an empty string | ||
except IOError as e: | ||
doAssert e.name == "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
discard """ | ||
action: compile | ||
description: '''An empty exception name is overridden on raise when run in | ||
the VM''' | ||
knownIssue | ||
""" | ||
|
||
# The VM treats `cstring(nil)` and `cstring("")` as the same thing | ||
|
||
static: | ||
try: | ||
raise (ref CatchableError)(name: "") | ||
except CatchableError as e: | ||
doAssert e.name == "" |