Skip to content

Commit

Permalink
fix codegen for Base10.decode
Browse files Browse the repository at this point in the history
Calling `Base10.decode` may lead to different structures being generated
for use with `uint64`.

The one normally generated is:

```
struct tyObject_Result__559ckyoL0ZZBsNFIYXjaoeg {NIM_BOOL o;
union{
struct {NCSTRING e;
} _o_1;
struct {unsigned long long v;
} _o_2;
};
```

But sometimes, it may be generated as:

```
struct tyObject_Result__xZhi1m1g75ioXsKjx9bN5bg {NIM_BOOL o;
union{
struct {NCSTRING e;
} _o_1;
struct {NU64 v;
} _o_2;
};
```

When the latter is generated, the compiler throws with:
```
error: passing 'tyObject_Result__xZhi1m1g75ioXsKjx9bN5bg' (aka 'struct tyObject_Result__xZhi1m1g75ioXsKjx9bN5bg') to parameter of incompatible type 'tyObject_Result__559ckyoL0ZZBsNFIYXjaoeg' (aka 'struct tyObject_Result__559ckyoL0ZZBsNFIYXjaoeg')
```

By passing the type as a generic param, the `unsigned long long` version
gets consistently generated / used.

Observed this in Chronos httptable.nim during nimbus light client work.
```
proc getInt*(ht: HttpTables, key: string): uint64 =
  let res = Base10.decode(uint64, ht.getString(key))
  if res.isOk():
    res.get()    # This line may lead to the compiler error above
  else:
    0'u64
```

Maybe it depends on include order, because in the nimbus_beacon_chain
binary there is no issue, only in nimbus_light_client.
  • Loading branch information
etan-status committed May 17, 2022
1 parent 419903c commit c016512
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions stew/base10.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Copyright (c) 2021 Status Research & Development GmbH
## Copyright (c) 2021-2022 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand Down Expand Up @@ -39,8 +39,9 @@ type
data*: array[maxLen(Base10, T), byte]
len*: int8 # >= 1 when holding valid unsigned integer

proc decode*[A: byte|char](B: typedesc[Base10], T: typedesc[SomeUnsignedInt],
src: openArray[A]): Result[T, cstring] =
proc decode*[A: byte|char, T: SomeUnsignedInt](
B: typedesc[Base10], t: typedesc[T],
src: openArray[A]): Result[T, cstring] =
## Convert base10 encoded string or array of bytes to unsigned integer.
const
MaxValue = T(high(T) div 10)
Expand Down

0 comments on commit c016512

Please sign in to comment.