void object fields are now ignored by codegen and fields/fieldPairs iterator (#10144)

* Codegen now ignores object fields of type void
* Fix `$` bug for objects/tuples where it does not add a comma
* fields/fieldPairs iterators now ignore void types
* Use `isEmptyType` instead of checking for `tyVoid` directly
This commit is contained in:
Neelesh Chandola
2019-01-10 17:19:35 +05:30
committed by Andreas Rumpf
parent 6389271d1c
commit d998cb58dd
5 changed files with 25 additions and 0 deletions

View File

@@ -1041,6 +1041,8 @@ proc genObjectFields(m: BModule, typ, origType: PType, n: PNode, expr: Rope;
else: internalError(m.config, n.info, "genObjectFields(nkRecCase)")
of nkSym:
var field = n.sym
# Do not produce code for void types
if isEmptyType(field.typ): return
if field.bitsize == 0:
if field.loc.r == nil: fillObjectFields(m, typ)
if field.loc.t == nil:

View File

@@ -1502,6 +1502,8 @@ proc createRecordVarAux(p: PProc, rec: PNode, excludedFieldIDs: IntSet, output:
for i in countup(1, sonsLen(rec) - 1):
createRecordVarAux(p, lastSon(rec.sons[i]), excludedFieldIDs, output)
of nkSym:
# Do not produce code for void types
if isEmptyType(rec.sym.typ): return
if rec.sym.id notin excludedFieldIDs:
if output.len > 0: output.add(", ")
output.addf("$#: ", [mangleName(p.module, rec.sym)])

View File

@@ -19,6 +19,9 @@ type
c: PContext
proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode =
if c.field != nil and isEmptyType(c.field.typ):
result = newNode(nkEmpty)
return
case n.kind
of nkEmpty..pred(nkIdent), succ(nkSym)..nkNilLit: result = n
of nkIdent, nkSym:

View File

@@ -2700,6 +2700,7 @@ proc `$`*[T: tuple|object](x: T): string =
firstElement = false
else:
result.add("...")
firstElement = false
when not isNamed:
if count == 1:
result.add(",") # $(1,) should print as the semantically legal (1,)

17
tests/objects/t3734.nim Normal file
View File

@@ -0,0 +1,17 @@
discard """
output: "i0"
"""
type
Application = object
config: void
i: int
f: void
proc printFields(rec: Application) =
for k, v in fieldPairs(rec):
echo k, v
var app: Application
printFields(app)