readFieldIMPL workaround when orc enabled

This commit is contained in:
jangko 2023-04-17 09:23:17 +07:00
parent 845bed4478
commit bc46b4c1c1
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9

View File

@ -1,4 +1,5 @@
import
std/typetraits,
stew/shims/macros, stew/objects,
./errors
@ -183,10 +184,20 @@ template GetFieldType(FT: type FieldTag): type =
typeof field(declval(FT.RecordType), FT.fieldName)
template readFieldIMPL[Reader](field: type FieldTag,
reader: var Reader): untyped =
reader: var Reader): auto =
mixin readValue
type FieldType = GetFieldType(field)
{.gcsafe.}: # needed by Nim-1.6
reader.readValue GetFieldType(field)
# TODO: The `FieldType` coercion below is required to deal
# with a nim bug caused by the distinct `ssz.List` type.
# It seems to break the generics cache mechanism, which
# leads to an incorrect return type being reported from
# the `readFieldIMPL` function.
# additional notes: putting the FieldType coercion in
# `makeFieldReadersTable` will cause problems when orc enabled
# hence, move it here
FieldType reader.readValue(FieldType)
template writeFieldIMPL*[Writer](writer: var Writer,
fieldTag: type FieldTag,
@ -204,9 +215,9 @@ proc makeFieldReadersTable(RecordType, ReaderType: distinct type,
enumAllSerializedFields(RecordType):
proc readField(obj: var RecordType, reader: var ReaderType)
{.gcsafe, nimcall, raises: [SerializationError, Defect].} =
mixin readValue
when RecordType is tuple:
const i = fieldName.parseInt
@ -214,20 +225,8 @@ proc makeFieldReadersTable(RecordType, ReaderType: distinct type,
when RecordType is tuple:
reader.readValue obj[i]
else:
# TODO: The `FieldType` coercion below is required to deal
# with a nim bug caused by the distinct `ssz.List` type.
# It seems to break the generics cache mechanism, which
# leads to an incorrect return type being reported from
# the `readFieldIMPL` function.
field(obj, realFieldName) = FieldType reader.readValue(FieldType)
when false:
# somehow this code result in crash with nim orc enabled.
# that's why we use reader.readValue
# removing FieldType coercion also prevent crash, but see above comment
type F = FieldTag[RecordType, realFieldName]
field(obj, realFieldName) = FieldType readFieldIMPL(F, reader)
type F = FieldTag[RecordType, realFieldName]
field(obj, realFieldName) = readFieldIMPL(F, reader)
except SerializationError as err:
raise err
except CatchableError as err: