mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
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:
committed by
Andreas Rumpf
parent
6389271d1c
commit
d998cb58dd
@@ -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:
|
||||
|
||||
@@ -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)])
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
17
tests/objects/t3734.nim
Normal 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)
|
||||
Reference in New Issue
Block a user