From 1c79ef1090a746e516df03d17c2a99117e82bdce Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:32:39 +0800 Subject: [PATCH 1/5] prohibits field variables from being used as lvalues in a 'fields' loop --- compiler/semfields.nim | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/compiler/semfields.nim b/compiler/semfields.nim index 874055cdcde96..7544f012f4c1e 100644 --- a/compiler/semfields.nim +++ b/compiler/semfields.nim @@ -17,8 +17,9 @@ type field: PSym replaceByFieldName: bool c: PContext + leftPartOfAssign: bool -proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode = +proc instFieldLoopBody(c: var TFieldInstCtx, n: PNode, forLoop: PNode): PNode = if c.field != nil and isEmptyType(c.field.typ): result = newNode(nkEmpty) return @@ -29,6 +30,9 @@ proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode = let ident = considerQuotedIdent(c.c, n) if c.replaceByFieldName: if ident.id == considerQuotedIdent(c.c, forLoop[0]).id: + if c.leftPartOfAssign: + localError(c.c.config, n.info, + "field variable '$1' is not allowed to use as a lvalue in a 'fields' loop" % [ident.s]) let fieldName = if c.tupleType.isNil: c.field.name.s elif c.tupleType.n.isNil: "Field" & $c.tupleIndex else: c.tupleType.n[c.tupleIndex].sym.name.s @@ -37,6 +41,9 @@ proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode = # other fields: for i in ord(c.replaceByFieldName).. Date: Tue, 22 Oct 2024 21:34:45 +0800 Subject: [PATCH 2/5] wordy --- compiler/semfields.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/semfields.nim b/compiler/semfields.nim index 7544f012f4c1e..87b9b4145daa8 100644 --- a/compiler/semfields.nim +++ b/compiler/semfields.nim @@ -32,7 +32,7 @@ proc instFieldLoopBody(c: var TFieldInstCtx, n: PNode, forLoop: PNode): PNode = if ident.id == considerQuotedIdent(c.c, forLoop[0]).id: if c.leftPartOfAssign: localError(c.c.config, n.info, - "field variable '$1' is not allowed to use as a lvalue in a 'fields' loop" % [ident.s]) + "field variable '$1' is not allowed to be used as a lvalue in a 'fields' loop" % [ident.s]) let fieldName = if c.tupleType.isNil: c.field.name.s elif c.tupleType.n.isNil: "Field" & $c.tupleIndex else: c.tupleType.n[c.tupleIndex].sym.name.s @@ -43,7 +43,7 @@ proc instFieldLoopBody(c: var TFieldInstCtx, n: PNode, forLoop: PNode): PNode = if ident.id == considerQuotedIdent(c.c, forLoop[i]).id: if c.leftPartOfAssign: localError(c.c.config, n.info, - "field variable '$1' is not allowed to use as a lvalue in a 'fields' loop" % [ident.s]) + "field variable '$1' is not allowed to be used as a lvalue in a 'fields' loop" % [ident.s]) var call = forLoop[^2] var tupl = call[i+1-ord(c.replaceByFieldName)] if c.field.isNil: From aca59572c701c3aca769117c90f5473b1c67b6af Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:40:57 +0800 Subject: [PATCH 3/5] oops --- compiler/semfields.nim | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/semfields.nim b/compiler/semfields.nim index 87b9b4145daa8..6e1498a140555 100644 --- a/compiler/semfields.nim +++ b/compiler/semfields.nim @@ -30,9 +30,6 @@ proc instFieldLoopBody(c: var TFieldInstCtx, n: PNode, forLoop: PNode): PNode = let ident = considerQuotedIdent(c.c, n) if c.replaceByFieldName: if ident.id == considerQuotedIdent(c.c, forLoop[0]).id: - if c.leftPartOfAssign: - localError(c.c.config, n.info, - "field variable '$1' is not allowed to be used as a lvalue in a 'fields' loop" % [ident.s]) let fieldName = if c.tupleType.isNil: c.field.name.s elif c.tupleType.n.isNil: "Field" & $c.tupleIndex else: c.tupleType.n[c.tupleIndex].sym.name.s From 8af81e3d246f5272f56a22018ef9cb67d652810a Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:51:19 +0800 Subject: [PATCH 4/5] redefining field variables is disabled --- compiler/semfields.nim | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/compiler/semfields.nim b/compiler/semfields.nim index 6e1498a140555..43f159343d1a9 100644 --- a/compiler/semfields.nim +++ b/compiler/semfields.nim @@ -17,7 +17,7 @@ type field: PSym replaceByFieldName: bool c: PContext - leftPartOfAssign: bool + leftPartOfDefinition: bool proc instFieldLoopBody(c: var TFieldInstCtx, n: PNode, forLoop: PNode): PNode = if c.field != nil and isEmptyType(c.field.typ): @@ -29,6 +29,9 @@ proc instFieldLoopBody(c: var TFieldInstCtx, n: PNode, forLoop: PNode): PNode = result = n let ident = considerQuotedIdent(c.c, n) if c.replaceByFieldName: + if c.leftPartOfDefinition: + localError(c.c.config, n.info, + "redefine field variable '$1' in a 'fields' loop" % [ident.s]) if ident.id == considerQuotedIdent(c.c, forLoop[0]).id: let fieldName = if c.tupleType.isNil: c.field.name.s elif c.tupleType.n.isNil: "Field" & $c.tupleIndex @@ -38,9 +41,9 @@ proc instFieldLoopBody(c: var TFieldInstCtx, n: PNode, forLoop: PNode): PNode = # other fields: for i in ord(c.replaceByFieldName).. Date: Tue, 22 Oct 2024 21:53:56 +0800 Subject: [PATCH 5/5] fixes --- compiler/semfields.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/semfields.nim b/compiler/semfields.nim index 43f159343d1a9..c13f70e8c467a 100644 --- a/compiler/semfields.nim +++ b/compiler/semfields.nim @@ -29,10 +29,10 @@ proc instFieldLoopBody(c: var TFieldInstCtx, n: PNode, forLoop: PNode): PNode = result = n let ident = considerQuotedIdent(c.c, n) if c.replaceByFieldName: - if c.leftPartOfDefinition: - localError(c.c.config, n.info, - "redefine field variable '$1' in a 'fields' loop" % [ident.s]) if ident.id == considerQuotedIdent(c.c, forLoop[0]).id: + if c.leftPartOfDefinition: + localError(c.c.config, n.info, + "redefine field variable '$1' in a 'fields' loop" % [ident.s]) let fieldName = if c.tupleType.isNil: c.field.name.s elif c.tupleType.n.isNil: "Field" & $c.tupleIndex else: c.tupleType.n[c.tupleIndex].sym.name.s