From f2e4ae0016ff0a344e814238ffc26146ab765f21 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 6 May 2026 14:42:36 +0800 Subject: [PATCH] fixes lent tuple codegen error (#25782) ref https://github.com/nim-lang/Nim/pull/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. --- compiler/semexprs.nim | 13 ++++++++++--- tests/lent/tlent_tuple_address.nim | 12 ++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/lent/tlent_tuple_address.nim diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 64a8d2a4f9..aa0489cd22 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -652,6 +652,9 @@ proc overloadedCallOpr(c: PContext, n: PNode): PNode = result = semExpr(c, result, flags = {efNoUndeclared}) proc changeType(c: PContext; n: PNode, newType: PType, check: bool) = + template isViewTarget(t: PType): bool = + t.skipTypes({tyGenericInst, tyAlias, tySink}).kind in {tyVar, tyLent} + case n.kind of nkCurly: for i in 0..