about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--nim.cfg1
-rw-r--r--src/utils/myaddr.nim18
2 files changed, 19 insertions, 0 deletions
diff --git a/nim.cfg b/nim.cfg
index 8d688ac0..1dc64525 100644
--- a/nim.cfg
+++ b/nim.cfg
@@ -6,6 +6,7 @@
 --experimental:"overloadableEnums"
 --warning:Effect:off
 --mm:refc
+--include:"utils/myaddr"
 --import:"utils/eprint"
 --styleCheck:usages
 --styleCheck:hint
diff --git a/src/utils/myaddr.nim b/src/utils/myaddr.nim
new file mode 100644
index 00000000..4405e0c4
--- /dev/null
+++ b/src/utils/myaddr.nim
@@ -0,0 +1,18 @@
+# Before 2.0, `addr' only worked on mutable types, and `unsafeAddr'
+# was needed to take the address of immutable ones.
+#
+# This was changed in 2.0 for some incomprehensible reason, so now to
+# write code that compiles on both versions you have to take care that
+# you use the right overload without compiler help.
+#
+# This module fixes the above problem; it is automatically included
+# in every file by nim.cfg.
+
+when NimMajor >= 2:
+  const msg = "expression has no address; maybe use `unsafeAddr'"
+
+  template addr(x: untyped): untyped {.used, error: msg.} =
+    discard
+
+  template addr(x: var untyped): untyped {.used.} =
+    system.addr x