summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2024-09-02 19:11:59 +0300
committerGitHub <noreply@github.com>2024-09-02 18:11:59 +0200
commit5e55e16ad83483f15f5a5df7dfdccf70ed9e3e8e (patch)
treedbf1d4bae4468dc74fb08bfa2d5fdf5c12771023 /tests
parent4789af71fe107e137524bc7746b902e644aca2dc (diff)
downloadNim-5e55e16ad83483f15f5a5df7dfdccf70ed9e3e8e.tar.gz
check constant conditions in generic `when` in objects (#24042)
fixes #24041

`when` statements in generic object types normally just leave their
conditions as expressions and still typecheck their branch bodies.
Instead of this, when the condition can be evaluated as a constant as
well as the ones before it and it resolves to `true`, it now uses the
body of that branch without typechecking the remaining ones.
Diffstat (limited to 'tests')
-rw-r--r--tests/generics/tgenericwhen.nim46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/generics/tgenericwhen.nim b/tests/generics/tgenericwhen.nim
new file mode 100644
index 000000000..e1b23873b
--- /dev/null
+++ b/tests/generics/tgenericwhen.nim
@@ -0,0 +1,46 @@
+discard """
+  targets: "c js"
+"""
+
+block: # issue #24041
+  type ArrayBuf[N: static int, T = byte] = object
+    when sizeof(int) > sizeof(uint8):
+      when N <= int(uint8.high):
+        n: uint8
+      else:
+        when sizeof(int) > sizeof(uint16):
+          when N <= int(uint16.high):
+            n: uint16
+          else:
+            when sizeof(int) > sizeof(uint32):
+              when N <= int(uint32.high):
+                n: uint32
+              else:
+                n: int
+            else:
+              n: int
+        else:
+          n: int
+    else:
+      n: int
+
+  var x: ArrayBuf[8]
+  doAssert x.n is uint8
+  when sizeof(int) > sizeof(uint32):
+    var y: ArrayBuf[int(uint32.high) * 8]
+    doAssert y.n is int
+
+block: # constant condition after dynamic one
+  type Foo[T] = object
+    when T is int:
+      a: int
+    elif true:
+      a: string
+    else:
+      a: bool
+  var x: Foo[string]
+  doAssert x.a is string
+  var y: Foo[int]
+  doAssert y.a is int
+  var z: Foo[float]
+  doAssert z.a is string