From 6eef0cc2d5108f51ae3e0e46cee3d59d489df645 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 23 Jun 2026 18:22:29 +0800 Subject: [PATCH] fixes #25908; resolves lent enum disambiguation (#25929) fixes #25908 When an enum identifier is resolved as an `nkSymChoice`, one of the candidates may come from a loop-local view and carry `tyVar` or `tyLent`. Enum disambiguation should compare the underlying enum type only. Otherwise a pure-enum field can win incorrectly even though the intended symbol is already present in the choice set. Keep the `includePureEnum` lookup path for enum-typed expectations so #23976 still works, but normalize `var`/`lent` only at the symchoice selection point. --- compiler/sem.nim | 5 ++++- testament/important_packages.nim | 1 + tests/enum/tenum.nim | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/sem.nim b/compiler/sem.nim index ba9190f572..54042d3fb2 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -105,9 +105,12 @@ proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode = result.typ = formal elif arg.kind in nkSymChoices and formal.skipTypes(abstractInst).kind == tyEnum: # Pick the right 'sym' from the sym choice by looking at 'formal' type: + # The choice candidates may be wrapped in `var`/`lent` when they come from + # a loop-local view, but for enum disambiguation only the underlying enum + # type matters. result = nil for ch in arg: - if sameType(ch.typ, formal): + if sameType(ch.typ.skipTypes({tyVar, tyLent}), formal): return ch typeMismatch(c.config, info, formal, arg.typ, arg) else: diff --git a/testament/important_packages.nim b/testament/important_packages.nim index d05b9385e9..9ba4a24035 100644 --- a/testament/important_packages.nim +++ b/testament/important_packages.nim @@ -158,6 +158,7 @@ pkg "sim" pkg "smtp", "nimble compileExample" pkg "snip", "nimble test", "https://github.com/genotrance/snip" pkg "ssostrings", "nim c -r tests/tssostrings.nim" +pkg "ssz_serialization", "nim c -r tests/test_all.nim" pkg "stew" pkg "stint", "nimble test_internal" pkg "strslice" diff --git a/tests/enum/tenum.nim b/tests/enum/tenum.nim index 7fd89ca0fa..92ee8a523b 100644 --- a/tests/enum/tenum.nim +++ b/tests/enum/tenum.nim @@ -283,3 +283,13 @@ block: # bug #23952 doAssert s1 != s2 static: foo() foo() + +# bug #25908 +type S {.pure.} = enum a, b + +iterator foo(x: array[1, S]): lent S = + yield x[0] + +iterator f(_: int | int): S = + for a in foo([S.b]): yield a +for v in f(0): doAssert v == S.b \ No newline at end of file