fix #25508; the alternative way

This commit is contained in:
ringabout
2026-02-25 19:40:11 +08:00
parent c292981fd3
commit 23543db308
4 changed files with 44 additions and 16 deletions

View File

@@ -216,6 +216,8 @@ proc genOptAsgnTuple(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
flags
let t = skipTypes(dest.t, abstractInst).getUniqueType()
for i, t in t.ikids:
# Do not produce code for void types
if isEmptyType(t): continue
let field = "Field$1" % [i.rope]
genAssignment(p, optAsgnLoc(dest, t, field),
optAsgnLoc(src, t, field), newflags)
@@ -3155,6 +3157,8 @@ proc genTupleConstr(p: BProc, n: PNode, d: var TLoc) =
for i in 0..<n.len:
var it = n[i]
if it.kind == nkExprColonExpr: it = it[1]
# Do not produce code for void types
if it.typ != nil and isEmptyType(it.typ): continue
rec = initLoc(locExpr, it, dest[].storage)
rec.snippet = dotField(rdLoc(dest[]), "Field" & rope(i))
rec.flags.incl(lfEnforceDeref)
@@ -3766,6 +3770,7 @@ proc containsOpaqueImportcField(typ: PType): bool =
return true
of tyTuple:
for i, a in t.ikids:
if isEmptyType(a): continue
if containsOpaqueImportcField(a):
return true
of tyArray:
@@ -3819,6 +3824,8 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder)
result.addField(tupleInit, name = "dummy"):
result.addIntValue(0)
for i, a in t.ikids:
# Do not produce code for void types
if isEmptyType(a): continue
let elemTyp = skipTypes(a, abstractRange+{tyOwned}-{tyTypeDesc})
if not isOpaqueImportcType(elemTyp):
result.addField(tupleInit, name = "Field" & $i):
@@ -3993,6 +4000,8 @@ proc genConstTuple(p: BProc, n: PNode; isConst: bool; tup: PType; result: var Bu
var it = n[i]
if it.kind == nkExprColonExpr:
it = it[1]
# Do not produce code for void types
if isEmptyType(tup[i]): continue
result.addField(tupleInit, name = "Field" & $i):
genBracedInit(p, it, isConst, tup[i], result)

View File

@@ -800,6 +800,8 @@ proc getTupleDesc(m: BModule; typ: PType, name: Rope,
var res = newBuilder("")
res.addStruct(m, typ, name, ""):
for i, a in typ.ikids:
# Do not produce code for void types
if isEmptyType(a): continue
res.addField(
name = "Field" & $i,
typ = getTypeDescAux(m, a, check, dkField))
@@ -1476,21 +1478,29 @@ proc genTupleInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo)
genTypeInfoAuxBase(m, typ, typ, name, cIntValue(0), info)
var expr = getNimNode(m)
if not typ.isEmptyTupleType:
var tmp = getTempName(m) & "_" & $typ.kidsLen
genTNimNodeArray(m, tmp, typ.kidsLen)
for i, a in typ.ikids:
var tmp2 = getNimNode(m)
let fieldTypInfo = genTypeInfoV1(m, a, info)
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(i), cAddr(tmp2))
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "kind", 1)
m.s[cfsTypeInit3].addFieldAssignmentWithValue(tmp2, "offset"):
m.s[cfsTypeInit3].addOffsetof(getTypeDesc(m, origType, dkVar), "Field" & $i)
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "typ", fieldTypInfo)
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "name", "\"Field" & $i & "\"")
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", typ.kidsLen)
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind", 2)
m.s[cfsTypeInit3].addFieldAssignment(expr, "sons",
cAddr(subscript(tmp, cIntValue(0))))
var nonVoidKids = 0
for a in typ.kids:
if not isEmptyType(a): inc nonVoidKids
if nonVoidKids > 0:
var tmp = getTempName(m) & "_" & $nonVoidKids
genTNimNodeArray(m, tmp, nonVoidKids)
var j = 0
for i, a in typ.ikids:
# Do not produce code for void types
if isEmptyType(a): continue
var tmp2 = getNimNode(m)
let fieldTypInfo = genTypeInfoV1(m, a, info)
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(j), cAddr(tmp2))
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "kind", 1)
m.s[cfsTypeInit3].addFieldAssignmentWithValue(tmp2, "offset"):
m.s[cfsTypeInit3].addOffsetof(getTypeDesc(m, origType, dkVar), "Field" & $i)
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "typ", fieldTypInfo)
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "name", "\"Field" & $i & "\"")
inc j
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", nonVoidKids)
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind", 2)
m.s[cfsTypeInit3].addFieldAssignment(expr, "sons",
cAddr(subscript(tmp, cIntValue(0))))
else:
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", typ.kidsLen)
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind", 2)

View File

@@ -2018,8 +2018,12 @@ proc createVar(p: PProc, typ: PType, indirect: bool): Rope =
if indirect: result = "[$1]" % [result]
of tyTuple:
result = rope("{")
var first = true
for i in 0..<t.len:
if i > 0: result.add(", ")
# Do not produce code for void types
if isEmptyType(t[i]): continue
if not first: result.add(", ")
first = false
result.addf("Field$1: $2", [i.rope,
createVar(p, t[i], false)])
result.add("}")

View File

@@ -4,6 +4,7 @@ empty
he, no return type;
abc a string
ha'''
target: "c js"
"""
proc ReturnT[T](x: T): T =
@@ -96,3 +97,7 @@ block: # typeof(stmt)
block:
template bad2 = echo (nonexistent; discard)
doAssert not compiles(bad2())
block:
discard default(tuple[b: void])
discard default((void,))