Skip to content

Commit f003664

Browse files
metagnnarimiran
authored andcommitted
fix regression with enum types wrongly matching [backport:2.2] (#25010)
fixes #25009 Introduced by #24176, when matching a set type to another, if the given set is a constructor and the element types match worse than a generic match (which includes the case with no match), the match is always set to a convertible match, without checking that it is at least a convertible match. This is fixed by checking this. (cherry picked from commit 334848f)
1 parent 3d63491 commit f003664

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

compiler/sigmatch.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,8 +1568,11 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
15681568
# set['a'..'z'] and set[char] have different representations
15691569
result = isNone
15701570
else:
1571-
# but we can convert individual elements of the constructor
1572-
result = isConvertible
1571+
if result >= isConvertible:
1572+
# but we can convert individual elements of the constructor
1573+
result = isConvertible
1574+
else:
1575+
result = isNone
15731576
of tyPtr, tyRef:
15741577
a = reduceToBase(a)
15751578
if a.kind == f.kind:

tests/proc/tgenericdefaultparam.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ block: # issue #24484
104104
foo[E]()
105105
106106
proc bar[T](t: set[T] = {T(0), 5}) =
107-
doAssert t == {0, 5}
107+
doAssert t == {T(0), 5}
108108
bar[uint8]()
109109
doAssert not compiles(bar[string]())
110110

tests/sets/twrongsetmatch.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# issue #25009
2+
3+
type EnumOne {.pure.} = enum
4+
aaa
5+
bbb
6+
7+
type EnumTwo {.pure.} = enum
8+
ccc
9+
ddd
10+
eee
11+
12+
proc doStuff(e: set[EnumOne]) =
13+
echo e
14+
15+
doStuff({EnumTwo.ddd}) #[tt.Error
16+
^ type mismatch]#

0 commit comments

Comments
 (0)