Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #23454; IndexDefect thrown when destructuring a lent tuple #23993

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/transf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,10 @@ proc transformAddrDeref(c: PTransf, n: PNode, kinds: TNodeKinds): PNode =
elif n.typ.skipTypes(abstractInst).kind in {tyVar}:
result.typ = toVar(result.typ, n.typ.skipTypes(abstractInst).kind, c.idgen)
else:
if n[0].kind in kinds:
if n[0].kind in kinds and
not (n[0][0].kind == nkSym and n[0][0].sym.kind == skForVar and
n[0][0].typ.skipTypes(abstractVar).kind == tyTuple
): # elimination is harmful to `for tuple unpack` because of newTupleAccess
# addr ( deref ( x )) --> x
result = n[0][0]
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
Expand Down
36 changes: 36 additions & 0 deletions tests/lent/tlent_from_var.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,39 @@ template get*[T: not void](self: Opt[T]): T = self.value()
method connect*(
self: Opt[(int, int)]) =
discard self.get()[0]

block: # bug #23454
type
Letter = enum
A

LetterPairs = object
values: seq[(Letter, string)]

iterator items(list: var LetterPairs): lent (Letter, string) =
for item in list.values:
yield item

var instance = LetterPairs(values: @[(A, "foo")])

for (a, _) in instance:
case a
of A: discard

block: # bug #23454
type
Letter = enum
A

LetterPairs = object
values: seq[(Letter, string)]

iterator items(list: var LetterPairs): var (Letter, string) =
for item in list.values.mItems:
yield item

var instance = LetterPairs(values: @[(A, "foo")])

for (a, _) in instance:
case a
of A: discard
Loading