summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2024-10-03 21:39:55 +0300
committerGitHub <noreply@github.com>2024-10-03 20:39:55 +0200
commit7dfadb8b4e95d09981fbeb01d85b12f23946c3e7 (patch)
treebe82d847e1a7dd4bebf1ca3605c97fa19d48c969 /tests
parent4eed341ba53e69674d7e8e8a609efd1ea0a54b4b (diff)
downloadNim-7dfadb8b4e95d09981fbeb01d85b12f23946c3e7.tar.gz
stricter set type match, implicit conversion for literals (#24176) devel
fixes #18396, fixes #20142

Set types with base types matching less than a generic match (so
subrange matches, conversion matches, int conversion matches) are now
considered mismatching, as their representation is different on the
backends (except VM and JS), causing codegen issues. An exception is
granted for set literal types, which now implicitly convert each element
to the matched base type, so things like `s == {'a', 'b'}` are still
possible where `s` is `set[range['a'..'z']]`. Also every conversion
match in this case is unified under the normal "conversion" match, so a
literal doesn't match one set type better than the other, unless it's
equal.

However `{'a', 'b'} == s` or `{'a', 'b'} - s` etc is now not possible.
when it used to work in the VM. So this is somewhat breaking, and needs
a changelog entry.
Diffstat (limited to 'tests')
-rw-r--r--tests/sets/trangeincompatible.nim32
-rw-r--r--tests/sets/twrongenumrange.nim5
2 files changed, 34 insertions, 3 deletions
diff --git a/tests/sets/trangeincompatible.nim b/tests/sets/trangeincompatible.nim
new file mode 100644
index 000000000..554a50235
--- /dev/null
+++ b/tests/sets/trangeincompatible.nim
@@ -0,0 +1,32 @@
+block: # issue #20142
+  let
+    s1: set['a' .. 'g'] = {'a', 'e'}
+    s2: set['a' .. 'g'] = {'b', 'c', 'd', 'f'} # this works fine
+    s3 = {'b', 'c', 'd', 'f'}
+
+  doAssert s1 != s2
+  doAssert s1 == {range['a'..'g'] 'a', 'e'}
+  doAssert s2 == {range['a'..'g'] 'b', 'c', 'd', 'f'}
+  # literal conversion:
+  doAssert s1 == {'a', 'e'}
+  doAssert s2 == {'b', 'c', 'd', 'f'}
+  doAssert s3 == {'b', 'c', 'd', 'f'}
+  doAssert not compiles(s1 == s3)
+  doAssert not compiles(s2 == s3)
+  # can't convert literal 'z', overload match fails
+  doAssert not compiles(s1 == {'a', 'z'})
+
+block: # issue #18396
+  var s1: set[char] = {'a', 'b'}
+  var s2: set['a'..'z'] = {'a', 'b'}
+  doAssert s1 == {'a', 'b'}
+  doAssert s2 == {range['a'..'z'] 'a', 'b'}
+  doAssert s2 == {'a', 'b'}
+  doAssert not compiles(s1 == s2)
+
+block: # issue #16270
+  var s1: set[char] = {'a', 'b'}
+  var s2: set['a'..'z'] = {'a', 'c'}
+  doAssert not (compiles do: s2 = s2 + s1)
+  s2 = s2 + {'a', 'b'}
+  doAssert s2 == {'a', 'b', 'c'}
diff --git a/tests/sets/twrongenumrange.nim b/tests/sets/twrongenumrange.nim
index fc90c142f..a8d64ac44 100644
--- a/tests/sets/twrongenumrange.nim
+++ b/tests/sets/twrongenumrange.nim
@@ -45,7 +45,6 @@ block:
     TNoteKind = range[k1..k2]
   var notes: set[TNoteKind]
   notes = {k0} #[tt.Error
-           ^ cannot convert 'k0' to 'TNoteKind = range 1..2(TMsgKind)]#
+          ^ type mismatch: got <set[TMsgKind]> but expected 'set[TNoteKind]']#
   notes = {k0..k3} #[tt.Error
-           ^ cannot convert 'k0' to 'TNoteKind = range 1..2(TMsgKind)'; tt.Error
-               ^ cannot convert 'k3' to 'TNoteKind = range 1..2(TMsgKind)']#
+          ^ type mismatch: got <set[TMsgKind]> but expected 'set[TNoteKind]']#