Revert "fixes #20699; generate an empty struct for void type" (#20806)

* Revert "fixes #20699; generate an empty struct for void type (#20790)"

This reverts commit 8fcb9380f0.

* better fix [backport]
This commit is contained in:
Andreas Rumpf 2022-11-12 22:56:40 +01:00 • committed by GitHub
commit 165d523edf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 27 deletions

View file

@ -3230,6 +3230,11 @@ proc caseObjDefaultBranch(obj: PNode; branch: Int128): int =
return i return i
assert(false, "unreachable") assert(false, "unreachable")
proc isEmptyCaseObjectBranch(n: PNode): bool =
for it in n:
if it.kind == nkSym and not isEmptyType(it.sym.typ): return false
return true
proc getNullValueAux(p: BProc; t: PType; obj, constOrNil: PNode, proc getNullValueAux(p: BProc; t: PType; obj, constOrNil: PNode,
result: var Rope; count: var int; result: var Rope; count: var int;
isConst: bool, info: TLineInfo) = isConst: bool, info: TLineInfo) =
@ -3258,7 +3263,7 @@ proc getNullValueAux(p: BProc; t: PType; obj, constOrNil: PNode,
let b = lastSon(obj[selectedBranch]) let b = lastSon(obj[selectedBranch])
# designated initilization is the only way to init non first element of unions # designated initilization is the only way to init non first element of unions
# branches are allowed to have no members (b.len == 0), in this case they don't need initializer # branches are allowed to have no members (b.len == 0), in this case they don't need initializer
if b.kind == nkRecList and b.len > 0: if b.kind == nkRecList and not isEmptyCaseObjectBranch(b):
result.add "._" & mangleRecFieldName(p.module, obj[0].sym) & "_" & $selectedBranch & " = {" result.add "._" & mangleRecFieldName(p.module, obj[0].sym) & "_" & $selectedBranch & " = {"
getNullValueAux(p, t, b, constOrNil, result, countB, isConst, info) getNullValueAux(p, t, b, constOrNil, result, countB, isConst, info)
result.add "}" result.add "}"

View file

@ -514,10 +514,7 @@ proc genRecordFieldsAux(m: BModule, n: PNode,
let structName = "_" & mangleRecFieldName(m, n[0].sym) & "_" & $i let structName = "_" & mangleRecFieldName(m, n[0].sym) & "_" & $i
var a = newRopeAppender() var a = newRopeAppender()
genRecordFieldsAux(m, k, rectype, check, a, unionPrefix & $structName & ".") genRecordFieldsAux(m, k, rectype, check, a, unionPrefix & $structName & ".")
# When 'k' is 'void', 'a' is the empty string and we just generate if a != "":
# empty struct. This prevents field access errors when generating
# static initializers for the type.
# See issue #20699
if tfPacked notin rectype.flags: if tfPacked notin rectype.flags:
unionBody.add("struct {") unionBody.add("struct {")
else: else:

View file

@ -476,6 +476,7 @@ template main {.dirty.} =
foo2() foo2()
block: # issue #20699 block: # issue #20699
type type
Either[A,B] = object Either[A,B] = object