Skip to content

fixes #24720; std lib iterators unnecessarily require value copies#25783

Draft
ringabout wants to merge 4 commits into
develfrom
pr_lent_iter
Draft

fixes #24720; std lib iterators unnecessarily require value copies#25783
ringabout wants to merge 4 commits into
develfrom
pr_lent_iter

Conversation

@ringabout
Copy link
Copy Markdown
Member

fixes #24720

ringabout and others added 2 commits May 1, 2026 21:21
Co-authored-by: Copilot <copilot@github.com>
@ringabout ringabout marked this pull request as ready for review May 5, 2026 11:26
Copilot AI review requested due to automatic review settings May 5, 2026 11:26
@ringabout ringabout marked this pull request as draft May 5, 2026 11:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses #24720 by changing stdlib tables iterators to yield lent (borrowed) keys/values instead of forcing value copies, enabling iteration over move-only / non-copyable table entries under ARC/ORC. To support the new (lent A, lent B) tuple typing, the compiler is updated to better normalize and materialize “tuple view” types during signature matching and typeof handling.

Changes:

  • Update pairs iterators for Table/TableRef/OrderedTable/OrderedTableRef/CountTable/CountTableRef to return lent keys/values where applicable.
  • Extend compiler signature matching / implicit conversion logic to handle tuple view types (tuples containing var/lent elements) and materialize them when needed.
  • Add a regression test ensuring iteration over a Table with a non-copyable value type works under --mm:orc.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/arc/t24720.nim Regression test for iterating Table[int, NoCopies] without triggering =copy.
lib/pure/collections/tables.nim Change pairs iterator return types to borrowed (lent ...) tuples to avoid copies.
compiler/sigmatch.nim Add tuple-view normalization/materialization in generic binding + implicit conversions.
compiler/semtypes.nim Decay view-typed results in typeof computation (esp. for tuple views).
compiler/semstmts.nim Materialize inferred direct view types in var/let init; adjust type-allowed check for tuple unpacking path.
compiler/semmagic.nim Apply view-decay when constructing the typedesc for typeof magic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread compiler/sigmatch.nim
Comment on lines +136 to +144
result = copyType(t, idgen, t.owner)
for i in 0..<t.len:
result[i] = materializeTupleViewType(t[i], idgen)
if result.n != nil:
let fieldCount = min(result.n.len, result.len)
for i in 0..<fieldCount:
if result.n[i].kind == nkSym:
result.n[i].sym.typ = result[i]
else:
Comment thread compiler/sigmatch.nim Outdated
Comment on lines +147 to +158
proc materializeTupleViewArg(c: PContext; targetType: PType; arg: PNode): PNode =
result = newNodeIT(nkTupleConstr, arg.info, targetType)
let targetTuple = targetType.skipTypes({tyGenericInst, tyAlias, tySink, tyDistinct, tyInferred})
for i in 0..<targetTuple.len:
let targetField = targetTuple[i]
var field = newTupleAccess(c.graph, arg, i)
let sourceField = field.typ.skipTypes({tyGenericInst, tyAlias, tySink, tyDistinct, tyInferred})
if sourceField.kind in {tyVar, tyLent}:
field = newDeref(field)
elif targetField.kind == tyTuple and classifyViewType(sourceField) != noView:
field = materializeTupleViewArg(c, targetField, field)
result.add field
Co-authored-by: Copilot <copilot@github.com>
Araq pushed a commit that referenced this pull request May 6, 2026
ref #25783

This pull request addresses an issue with addressability of tuple
elements of type `lent` or `var` in Nim, ensuring that expressions
involving these types are handled correctly during type changes. The
main changes introduce a check to prevent attempting to change the type
of tuple elements that are views (`var` or `lent`), and a new test is
added to verify the correct error is raised when trying to take the
address of such elements.

Type system and semantic analysis improvements:

* Added the `isViewTarget` template in `semexprs.nim` to check if a type
is a view (`var` or `lent`), and updated `changeType` to skip type
changes for tuple elements that are views. This prevents invalid
addressability operations on these types.
[[1]](diffhunk://#diff-539da3a63df08fa987f1b0c67d26cdc690753843d110b6bf0805a685eeaffd40R655-R657)
[[2]](diffhunk://#diff-539da3a63df08fa987f1b0c67d26cdc690753843d110b6bf0805a685eeaffd40R686-R693)

Testing:

* Added a new test `tlent_tuple_address.nim` to verify that attempting
to take the address of tuple elements of type `lent` correctly produces
an "expression has no address" error.
narimiran pushed a commit that referenced this pull request May 8, 2026
ref #25783

This pull request addresses an issue with addressability of tuple
elements of type `lent` or `var` in Nim, ensuring that expressions
involving these types are handled correctly during type changes. The
main changes introduce a check to prevent attempting to change the type
of tuple elements that are views (`var` or `lent`), and a new test is
added to verify the correct error is raised when trying to take the
address of such elements.

Type system and semantic analysis improvements:

* Added the `isViewTarget` template in `semexprs.nim` to check if a type
is a view (`var` or `lent`), and updated `changeType` to skip type
changes for tuple elements that are views. This prevents invalid
addressability operations on these types.
[[1]](diffhunk://#diff-539da3a63df08fa987f1b0c67d26cdc690753843d110b6bf0805a685eeaffd40R655-R657)
[[2]](diffhunk://#diff-539da3a63df08fa987f1b0c67d26cdc690753843d110b6bf0805a685eeaffd40R686-R693)

Testing:

* Added a new test `tlent_tuple_address.nim` to verify that attempting
to take the address of tuple elements of type `lent` correctly produces
an "expression has no address" error.

(cherry picked from commit f2e4ae0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

std lib iterators unnecessarily require value copies

2 participants