fixes #24720; std lib iterators unnecessarily require value copies#25783
Draft
ringabout wants to merge 4 commits into
Draft
fixes #24720; std lib iterators unnecessarily require value copies#25783ringabout wants to merge 4 commits into
ringabout wants to merge 4 commits into
Conversation
This was referenced Apr 30, 2026
Contributor
There was a problem hiding this comment.
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
pairsiterators forTable/TableRef/OrderedTable/OrderedTableRef/CountTable/CountTableRefto returnlentkeys/values where applicable. - Extend compiler signature matching / implicit conversion logic to handle tuple view types (tuples containing
var/lentelements) and materialize them when needed. - Add a regression test ensuring iteration over a
Tablewith 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 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 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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #24720