Skip to content

Commit e4b1d8e

Browse files
authored
fix #25508; ignores void types in the backends (#25550)
fix #25508
1 parent 5094369 commit e4b1d8e

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

compiler/ccgexprs.nim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ proc genOptAsgnTuple(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
216216
flags
217217
let t = skipTypes(dest.t, abstractInst).getUniqueType()
218218
for i, t in t.ikids:
219+
# Do not produce code for void types
220+
if isEmptyType(t): continue
219221
let field = "Field$1" % [i.rope]
220222
genAssignment(p, optAsgnLoc(dest, t, field),
221223
optAsgnLoc(src, t, field), newflags)
@@ -3207,6 +3209,8 @@ proc genTupleConstr(p: BProc, n: PNode, d: var TLoc) =
32073209
for i in 0..<n.len:
32083210
var it = n[i]
32093211
if it.kind == nkExprColonExpr: it = it[1]
3212+
# Do not produce code for void types
3213+
if it.typ != nil and isEmptyType(it.typ): continue
32103214
rec = initLoc(locExpr, it, dest[].storage)
32113215
rec.snippet = dotField(rdLoc(dest[]), "Field" & rope(i))
32123216
rec.flags.incl(lfEnforceDeref)
@@ -3818,6 +3822,7 @@ proc containsOpaqueImportcField(typ: PType): bool =
38183822
return true
38193823
of tyTuple:
38203824
for i, a in t.ikids:
3825+
if isEmptyType(a): continue
38213826
if containsOpaqueImportcField(a):
38223827
return true
38233828
of tyArray:
@@ -3867,10 +3872,12 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder)
38673872
var tupleInit: StructInitializer
38683873
let initKind = if containsOpaqueImportcField(t): siNamedStruct else: siOrderedStruct
38693874
result.addStructInitializer(tupleInit, kind = initKind):
3870-
if p.vccAndC and t.isEmptyTupleType:
3875+
if p.vccAndC and validTupleTypeFields(t) == 0:
38713876
result.addField(tupleInit, name = "dummy"):
38723877
result.addIntValue(0)
38733878
for i, a in t.ikids:
3879+
# Do not produce code for void types
3880+
if isEmptyType(a): continue
38743881
let elemTyp = skipTypes(a, abstractRange+{tyOwned}-{tyTypeDesc})
38753882
if not isOpaqueImportcType(elemTyp):
38763883
result.addField(tupleInit, name = "Field" & $i):
@@ -4045,6 +4052,8 @@ proc genConstTuple(p: BProc, n: PNode; isConst: bool; tup: PType; result: var Bu
40454052
var it = n[i]
40464053
if it.kind == nkExprColonExpr:
40474054
it = it[1]
4055+
# Do not produce code for void types
4056+
if isEmptyType(tup[i]): continue
40484057
result.addField(tupleInit, name = "Field" & $i):
40494058
genBracedInit(p, it, isConst, tup[i], result)
40504059

compiler/ccgtypes.nim

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,8 @@ proc getTupleDesc(m: BModule; typ: PType, name: Rope,
808808
var res = newBuilder("")
809809
res.addStruct(m, typ, name, ""):
810810
for i, a in typ.ikids:
811+
# Do not produce code for void types
812+
if isEmptyType(a): continue
811813
res.addField(
812814
name = "Field" & $i,
813815
typ = getTypeDescAux(m, a, check, dkField))
@@ -1480,27 +1482,38 @@ proc genObjectInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo
14801482
t.incl tfObjHasKids
14811483
t = t.baseClass
14821484

1485+
proc validTupleTypeFields(t: PType): int =
1486+
# we want to treat tuples with only void fields as empty, so we need to exclude void types here:
1487+
result = 0
1488+
for a in t.kids:
1489+
if not isEmptyType(a): inc result
1490+
14831491
proc genTupleInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo) =
14841492
genTypeInfoAuxBase(m, typ, typ, name, cIntValue(0), info)
14851493
var expr = getNimNode(m)
1486-
if not typ.isEmptyTupleType:
1487-
var tmp = getTempName(m) & "_" & $typ.kidsLen
1488-
genTNimNodeArray(m, tmp, typ.kidsLen)
1494+
let nonVoidKids = validTupleTypeFields(typ)
1495+
if nonVoidKids > 0:
1496+
var tmp = getTempName(m) & "_" & $nonVoidKids
1497+
genTNimNodeArray(m, tmp, nonVoidKids)
1498+
var j = 0
14891499
for i, a in typ.ikids:
1500+
# Do not produce code for void types
1501+
if isEmptyType(a): continue
14901502
var tmp2 = getNimNode(m)
14911503
let fieldTypInfo = genTypeInfoV1(m, a, info)
1492-
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(i), cAddr(tmp2))
1504+
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(j), cAddr(tmp2))
14931505
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "kind", 1)
14941506
m.s[cfsTypeInit3].addFieldAssignmentWithValue(tmp2, "offset"):
14951507
m.s[cfsTypeInit3].addOffsetof(getTypeDesc(m, origType, dkVar), "Field" & $i)
14961508
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "typ", fieldTypInfo)
14971509
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "name", "\"Field" & $i & "\"")
1498-
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", typ.kidsLen)
1510+
inc j
1511+
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", nonVoidKids)
14991512
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind", 2)
15001513
m.s[cfsTypeInit3].addFieldAssignment(expr, "sons",
15011514
cAddr(subscript(tmp, cIntValue(0))))
15021515
else:
1503-
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", typ.kidsLen)
1516+
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", cIntValue(0))
15041517
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind", 2)
15051518
m.s[cfsTypeInit3].addFieldAssignment(tiNameForHcr(m, name), "node", cAddr(expr))
15061519

compiler/jsgen.nim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2018,8 +2018,12 @@ proc createVar(p: PProc, typ: PType, indirect: bool): Rope =
20182018
if indirect: result = "[$1]" % [result]
20192019
of tyTuple:
20202020
result = rope("{")
2021+
var first = true
20212022
for i in 0..<t.len:
2022-
if i > 0: result.add(", ")
2023+
# Do not produce code for void types
2024+
if isEmptyType(t[i]): continue
2025+
if not first: result.add(", ")
2026+
first = false
20232027
result.addf("Field$1: $2", [i.rope,
20242028
createVar(p, t[i], false)])
20252029
result.add("}")

tests/typerel/tvoid.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ empty
44
he, no return type;
55
abc a string
66
ha'''
7+
target: "c js"
78
"""
89

910
proc ReturnT[T](x: T): T =
@@ -96,3 +97,7 @@ block: # typeof(stmt)
9697
block:
9798
template bad2 = echo (nonexistent; discard)
9899
doAssert not compiles(bad2())
100+
101+
block:
102+
discard default(tuple[b: void])
103+
discard default((void,))

0 commit comments

Comments
 (0)