From e94a8dadce994272ae4768640c18373bac4a25ac Mon Sep 17 00:00:00 2001 From: metagn Date: Sun, 23 Jun 2024 12:55:04 +0300 Subject: [PATCH] add test for #13828 --- tests/template/mexport.nim | 19 +++++++++++++++++++ tests/template/texport.nim | 9 +++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/template/mexport.nim create mode 100644 tests/template/texport.nim diff --git a/tests/template/mexport.nim b/tests/template/mexport.nim new file mode 100644 index 000000000000..ab1e781c620f --- /dev/null +++ b/tests/template/mexport.nim @@ -0,0 +1,19 @@ +import macros + +type + Storage[N: static[int]] = array[N, float32] + Quat* = object + data*: Storage[4] + +proc `[]`(q: Quat, index: int): float32 = q.data[index] +proc `[]=`(q: var Quat, index: int, value: float32) = q.data[index] = value + +template genAccessor(t, a, i): untyped = + template a*(q: t): float32 {.inject.} = q[i] + template `a=`*(q: var t, value: float32) {.inject.} = q[i] = value + +genAccessor Quat, w, 0 +genAccessor Quat, x, 1 +genAccessor Quat, y, 2 +expandMacros: + genAccessor Quat, z, 3 diff --git a/tests/template/texport.nim b/tests/template/texport.nim new file mode 100644 index 000000000000..6cbdfdfc2996 --- /dev/null +++ b/tests/template/texport.nim @@ -0,0 +1,9 @@ +# issue #13828 + +import mexport + +var a = Quat() +a.data = [1f,2,3,4] +a.x = 42.0 +doAssert a.x == 42 +doAssert a.z == 4