added test cases

This commit is contained in:
Araq
2026-06-16 18:31:52 +02:00
parent 009f8776e8
commit 590b35dc89
12 changed files with 76 additions and 0 deletions

3
tests/ic/mtoffcodec.nim Normal file
View File

@@ -0,0 +1,3 @@
# Helper for ttypeoffer.nim: the extra overload that flips `compiles(toSszType(Gwei))`.
import mtoffgwei
template toSszType*(v: Gwei): uint64 = uint64(v)

2
tests/ic/mtoffgwei.nim Normal file
View File

@@ -0,0 +1,2 @@
# Helper for ttypeoffer.nim: a distinct basic type.
type Gwei* = distinct uint64

10
tests/ic/mtoffssz.nim Normal file
View File

@@ -0,0 +1,10 @@
# Helper for ttypeoffer.nim: a generic container whose hash-array bound depends
# on a `mixin toSszType` resolved at the instantiation site (cf. ssz dataPerChunk).
template perChunk*(T: type): int =
mixin toSszType
when compiles(toSszType(default(T))): 4 else: 1
type
HA*[N: static int; T] = object
data*: array[N, T]
hashes*: array[N div perChunk(T), uint64]

5
tests/ic/mtoffstate.nim Normal file
View File

@@ -0,0 +1,5 @@
# Helper for ttypeoffer.nim ("datatypes" analog): instantiates HA[64,Gwei] WITHOUT
# the codec in scope -> perChunk=1. This is the instance that must be shared.
import mtoffssz, mtoffgwei
type StateA* = object
field*: HA[64, Gwei]

10
tests/ic/mtoffuser.nim Normal file
View File

@@ -0,0 +1,10 @@
# Helper for ttypeoffer.nim ("db_immutable" analog): imports the codec (so
# `toSszType(Gwei)` is visible -> perChunk=4) and re-instantiates HA[64,Gwei].
# With the `(toffer …)` fix it reuses mtoffstate's instance instead.
import mtoffssz, mtoffgwei, mtoffcodec, mtoffstate
type StateB* = object
field*: HA[64, Gwei]
proc check*() =
static: doAssert sizeof(StateA) == sizeof(StateB)
echo "ok"

2
tests/ic/mtscopea.nim Normal file
View File

@@ -0,0 +1,2 @@
# Helper for ttransitiveoffer.nim: const that the generic body binds at definition.
const TScopeSize* = 65

2
tests/ic/mtscopeb.nim Normal file
View File

@@ -0,0 +1,2 @@
# Helper: a CONFLICTING const of the same name, visible only in the consumer.
const TScopeSize* = 33

7
tests/ic/mtscopegen.nim Normal file
View File

@@ -0,0 +1,7 @@
# Helper: defines a generic whose body uses TScopeSize via the strformat `&`
# macro (late-bound), resolved in THIS module's scope (mtscopea -> 65).
import mtscopea, std/strformat
export mtscopea
func fromRaw*[T](x: T): string =
const msg = &"size {TScopeSize - 1}"
result = msg & " " & $int(x)

3
tests/ic/mtscopemid.nim Normal file
View File

@@ -0,0 +1,3 @@
# Helper: makes mtscopewarm a TRANSITIVE import of the consumer.
import mtscopewarm
export mtscopewarm

4
tests/ic/mtscopewarm.nim Normal file
View File

@@ -0,0 +1,4 @@
# Helper: instantiates fromRaw[int] in a CLEAN scope (no mtscopeb) -> the
# correct instance that the consumer must reuse.
import mtscopegen
proc warm*(): string = fromRaw(5)

View File

@@ -0,0 +1,14 @@
discard """
output: '''size 64 64'''
"""
# Regression test for TRANSITIVE generic-instance offers. `fromRaw[int]` is first
# instantiated in mtscopewarm (a clean scope where `TScopeSize` is unambiguously
# 65). The consumer here also imports mtscopeb (`TScopeSize` = 33), so a fresh
# re-instantiation of fromRaw's body would resolve `TScopeSize` ambiguously. The
# clean instance reaches here only TRANSITIVELY (via mtscopemid), so the offer
# rebuild must walk the whole import closure, not just direct imports. Mirrors
# nimbus-eth2 `keys.fromRaw` -> `SkRawPublicKeySize` (secp vs secp256k1).
import mtscopegen, mtscopeb, mtscopemid
echo fromRaw(64)

14
tests/ic/ttypeoffer.nim Normal file
View File

@@ -0,0 +1,14 @@
discard """
output: '''ok'''
"""
# Regression test for the `(toffer …)` generic-TYPE-instance sharing across the
# NIF boundary. A `tyGenericInst` whose structure (here an `array` bound) depends
# on a `mixin`/`compiles()` resolved at the instantiation site must be REUSED
# from the module that created it, not re-instantiated in a consumer whose import
# scope flips the `compiles()` and so bakes a different bound. Mirrors the SSZ
# `HashArray[8192, Gwei]` `sizeof` divergence in nimbus-eth2. Before the fix this
# failed under `nim ic` with `doAssert sizeof(StateA) == sizeof(StateB)`.
import mtoffuser
check()