Skip to content
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

add {.raises.} annotation to writeValue #62

Merged
merged 2 commits into from
Aug 19, 2023
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
7 changes: 4 additions & 3 deletions serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ template saveFile*(Format: type, filename: string, value: auto, params: varargs[
template borrowSerialization*(Alias: type) {.dirty.} =
bind distinctBase

proc writeValue*[Writer](writer: var Writer, value: Alias) =
proc writeValue*[Writer](
writer: var Writer, value: Alias) {.raises: [IOError].} =
mixin writeValue
writeValue(writer, distinctBase value)

Expand All @@ -115,7 +116,8 @@ template borrowSerialization*(Alias: type) {.dirty.} =
template borrowSerialization*(Alias: distinct type,
OriginalType: distinct type) {.dirty.} =

proc writeValue*[Writer](writer: var Writer, value: Alias) =
proc writeValue*[Writer](
writer: var Writer, value: Alias) {.raises: [IOError].} =
mixin writeValue
writeValue(writer, OriginalType value)

Expand Down Expand Up @@ -161,4 +163,3 @@ template writeValue*(stream: OutputStream,
type WriterType = Writer(Format)
var writer = unpackArgs(init, [WriterType, stream, params])
writeValue writer, value

2 changes: 1 addition & 1 deletion serialization/object_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ proc genCustomSerializationForType(Format, typ: NimNode,
result.add quote do:
type WriterType = Writer(`Format`)
proc writeValue*(`writerSym`: var WriterType, `valueSym`: `typ`)
{.raises: [IOError, Defect], gcsafe.} =
{.raises: [IOError], gcsafe.} =
`writeBody`

macro useCustomSerialization*(Format: typed, field: untyped, body: untyped): untyped =
Expand Down
9 changes: 4 additions & 5 deletions serialization/testing/generic_suite.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ Simple.setSerializedFields distance, x, y

proc default(T: typedesc): T = discard

func caseObjectEquals(a, b: CaseObject): bool
func caseObjectEquals(a, b: CaseObject): bool {.raises: [].}

func `==`*(a, b: CaseObjectRef): bool =
func `==`*(a, b: CaseObjectRef): bool {.raises: [].} =
let nils = ord(a.isNil) + ord(b.isNil)
if nils == 0:
caseObjectEquals(a[], b[])
else:
nils == 2

func caseObjectEquals(a, b: CaseObject): bool =
func caseObjectEquals(a, b: CaseObject): bool {.raises: [].} =
# TODO This is needed to work-around a Nim overload selection issue
if a.kind != b.kind: return false

Expand Down Expand Up @@ -161,7 +161,7 @@ template roundtripChecks*(Format: type, value: auto, expectedResult: auto) =
try:
const typeName = typetraits.name(type(origValue))
{.warning: "WWW: " & typeName.}

let decoded = Format.decode(serialized, type(origValue))
checkpoint "(decoded value): " & repr(decoded)
let success = maybeDefer(decoded) == maybeDefer(origValue)
Expand Down Expand Up @@ -369,4 +369,3 @@ proc executeReaderWriterTests*(Format: type) =
findFieldReader(bazFields[], "some_other_name", pos) == nil

executeRoundtripTests(Format)