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 334848f3ae)
This commit is contained in:
metagn
2025-06-22 00:25:04 +03:00
committed by narimiran
parent 3d634911b8
commit f003664a14
3 changed files with 22 additions and 3 deletions

View File

@@ -1568,8 +1568,11 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
# set['a'..'z'] and set[char] have different representations
result = isNone
else:
# but we can convert individual elements of the constructor
result = isConvertible
if result >= isConvertible:
# but we can convert individual elements of the constructor
result = isConvertible
else:
result = isNone
of tyPtr, tyRef:
a = reduceToBase(a)
if a.kind == f.kind:

View File

@@ -104,7 +104,7 @@ block: # issue #24484
foo[E]()
proc bar[T](t: set[T] = {T(0), 5}) =
doAssert t == {0, 5}
doAssert t == {T(0), 5}
bar[uint8]()
doAssert not compiles(bar[string]())

View File

@@ -0,0 +1,16 @@
# issue #25009
type EnumOne {.pure.} = enum
aaa
bbb
type EnumTwo {.pure.} = enum
ccc
ddd
eee
proc doStuff(e: set[EnumOne]) =
echo e
doStuff({EnumTwo.ddd}) #[tt.Error
^ type mismatch]#