diff --git a/compiler/semfields.nim b/compiler/semfields.nim index 874055cdcde96..c13f70e8c467a 100644 --- a/compiler/semfields.nim +++ b/compiler/semfields.nim @@ -17,8 +17,9 @@ type field: PSym replaceByFieldName: bool c: PContext + leftPartOfDefinition: 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.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 @@ -37,6 +41,9 @@ proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode = # other fields: for i in ord(c.replaceByFieldName)..