summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-11-19 23:07:51 -0800
committerGitHub <noreply@github.com>2020-11-20 08:07:51 +0100
commit109cc45398bcdafc22f0a5e49cdfda8234bc46de (patch)
tree5e1573dbb2b2946cd67b8e47def266ddae59796c
parent59332ec2352aa1d1c0fa4401eff7fefe8f4187da (diff)
downloadNim-109cc45398bcdafc22f0a5e49cdfda8234bc46de.tar.gz
packedsets fix regression introduced in #15564 (#16060)
* packedsets fix regression introduced in #15564

* add tests
-rw-r--r--lib/std/packedsets.nim17
-rw-r--r--tests/stdlib/mintsets.nim10
-rw-r--r--tests/stdlib/tintsets.nim6
3 files changed, 25 insertions, 8 deletions
diff --git a/lib/std/packedsets.nim b/lib/std/packedsets.nim
index 20002d07f..be0555d3a 100644
--- a/lib/std/packedsets.nim
+++ b/lib/std/packedsets.nim
@@ -265,7 +265,7 @@ proc incl*[A](s: var PackedSet[A], other: PackedSet[A]) =
     assert len(a) == 2
     assert 5 in a
 
-  for item in other: incl(s, item)
+  for item in other.items: incl(s, item)
 
 proc toPackedSet*[A](x: openArray[A]): PackedSet[A] {.since: (1, 3).} =
   ## Creates a new PackedSet[A] that contains the elements of `x`.
@@ -358,7 +358,7 @@ proc excl*[A](s: var PackedSet[A], other: PackedSet[A]) =
     assert len(a) == 1
     assert 5 notin a
 
-  for item in other:
+  for item in other.items:
     excl(s, item)
 
 proc len*[A](s: PackedSet[A]): int {.inline.} =
@@ -367,7 +367,8 @@ proc len*[A](s: PackedSet[A]): int {.inline.} =
     result = s.elems
   else:
     result = 0
-    for _ in s:
+    for _ in s.items:
+      # pending bug #11167; when fixed, check each explicit `items` to see if it can be removed
       inc(result)
 
 proc missingOrExcl*[A](s: var PackedSet[A], key: A): bool =
@@ -488,7 +489,7 @@ proc intersection*[A](s1, s2: PackedSet[A]): PackedSet[A] =
     ## {3}
 
   result = initPackedSet[A]()
-  for item in s1:
+  for item in s1.items:
     if contains(s2, item):
       incl(result, item)
 
@@ -506,7 +507,7 @@ proc difference*[A](s1, s2: PackedSet[A]): PackedSet[A] =
     ## {1, 2}
 
   result = initPackedSet[A]()
-  for item in s1:
+  for item in s1.items:
     if not contains(s2, item):
       incl(result, item)
 
@@ -522,7 +523,7 @@ proc symmetricDifference*[A](s1, s2: PackedSet[A]): PackedSet[A] =
     ## {1, 2, 4, 5}
 
   result.assign(s1)
-  for item in s2:
+  for item in s2.items:
     if containsOrIncl(result, item): excl(result, item)
 
 proc `+`*[A](s1, s2: PackedSet[A]): PackedSet[A] {.inline.} =
@@ -549,7 +550,7 @@ proc disjoint*[A](s1, s2: PackedSet[A]): bool =
     b.excl(2)
     assert disjoint(a, b) == true
 
-  for item in s1:
+  for item in s1.items:
     if contains(s2, item):
       return false
   return true
@@ -575,7 +576,7 @@ proc `<=`*[A](s1, s2: PackedSet[A]): bool =
     a.incl(3)
     assert(not (a <= b))
 
-  for item in s1:
+  for item in s1.items:
     if not s2.contains(item):
       return false
   return true
diff --git a/tests/stdlib/mintsets.nim b/tests/stdlib/mintsets.nim
new file mode 100644
index 000000000..b4d9ed516
--- /dev/null
+++ b/tests/stdlib/mintsets.nim
@@ -0,0 +1,10 @@
+import std/intsets
+
+proc test1*[]() =
+  let a = initIntSet()
+  doAssert len(a) == 0
+
+proc test2*[]() =
+  var a = initIntSet()
+  var b = initIntSet()
+  a.incl b
diff --git a/tests/stdlib/tintsets.nim b/tests/stdlib/tintsets.nim
new file mode 100644
index 000000000..191ef117e
--- /dev/null
+++ b/tests/stdlib/tintsets.nim
@@ -0,0 +1,6 @@
+import ./mintsets
+
+block: # bug https://github.com/nim-lang/Nim/pull/15564#issuecomment-729878104
+  # related to bug #11167
+  test1()
+  test2()