Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Void as a "real" type #20609

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions compiler/ccgcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc,
var pl = callee & "(" & params
# getUniqueType() is too expensive here:
var typ = skipTypes(ri[0].typ, abstractInst)
if typ[0] != nil:
if typ[0] != nil and typ[0].kind != tyVoid:
if isInvalidReturnType(p.config, typ):
if params.len != 0: pl.add(", ")
# beware of 'result = p(result)'. We may need to allocate a temporary:
Expand Down Expand Up @@ -308,8 +308,11 @@ proc genArg(p: BProc, n: PNode, param: PSym; call: PNode; result: var Rope; need
else:
addRdLoc(a, result)
else:
initLocExprSingleUse(p, n, a)
addRdLoc(withTmpIfNeeded(p, a, needsTmp), result)
if param.typ.kind == tyVoid:
expr(p, n, a)
else:
initLocExprSingleUse(p, n, a)
addRdLoc(withTmpIfNeeded(p, a, needsTmp), result)
#assert result != nil

proc genArgNoParam(p: BProc, n: PNode; result: var Rope; needsTmp = false) =
Expand Down Expand Up @@ -404,6 +407,8 @@ proc genParams(p: BProc, ri: PNode, typ: PType; result: var Rope) =
result.add(", ")
oldLen = result.len
genArg(p, ri[i], paramType.sym, ri, result, needTmp[i-1])
elif paramType.typ.kind == tyVoid:
genArg(p, ri[i], paramType.sym, ri, result, needTmp[i-1])
else:
if oldLen != result.len:
result.add(", ")
Expand Down Expand Up @@ -459,7 +464,7 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =

let rawProc = getClosureType(p.module, typ, clHalf)
let canRaise = p.config.exc == excGoto and canRaiseDisp(p, ri[0])
if typ[0] != nil:
if typ[0] != nil and typ[0].kind != tyVoid:
if isInvalidReturnType(p.config, typ):
if ri.len > 1: pl.add(", ")
# beware of 'result = p(result)'. We may need to allocate a temporary:
Expand Down
2 changes: 1 addition & 1 deletion compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2987,7 +2987,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
internalError(p.config, n.info, "expr: temp not init " & sym.name.s & "_" & $sym.id)
putLocIntoDest(p, d, sym.loc)
of skParam:
if sym.loc.r == "" or sym.loc.t == nil:
if n.typ.kind != tyVoid and (sym.loc.r == "" or sym.loc.t == nil):
# echo "FAILED FOR PRCO ", p.prc.name.s
# debug p.prc.typ.n
# echo renderTree(p.prc.ast, {renderIds})
Expand Down
7 changes: 5 additions & 2 deletions compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const

proc registerTraverseProc(p: BProc, v: PSym) =
var traverseProc = ""
if p.config.selectedGC in {gcMarkAndSweep, gcHooks, gcRefc} and
if v.typ.kind != tyVoid and p.config.selectedGC in {gcMarkAndSweep, gcHooks, gcRefc} and
optOwnedRefs notin p.config.globalOptions and
containsGarbageCollectedRef(v.loc.t):
# we register a specialized marked proc here; this has the advantage
Expand Down Expand Up @@ -1615,8 +1615,11 @@ proc genAsgn(p: BProc, e: PNode, fastAsgn: bool) =
else:
let le = e[0]
let ri = e[1]
let letype = le.typ.skipTypes(skipPtrs)
if letype.kind == tyVoid:
return
var a: TLoc
discard getTypeDesc(p.module, le.typ.skipTypes(skipPtrs), skVar)
discard getTypeDesc(p.module, letype, skVar)
initLoc(a, locNone, le, OnUnknown)
a.flags.incl(lfEnforceDeref)
a.flags.incl(lfPrepareForMutation)
Expand Down
5 changes: 3 additions & 2 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ proc genProcParams(m: BModule, t: PType, rettype, params: var Rope,
check: var IntSet, declareEnvironment=true;
weakDep=false) =
params = "("
if t[0] == nil or isInvalidReturnType(m.config, t):
if t[0] == nil or t[0].kind == tyVoid or isInvalidReturnType(m.config, t):

rettype = "void"
else:
rettype = getTypeDescAux(m, t[0], check, skResult)
Expand Down Expand Up @@ -463,7 +464,7 @@ proc genProcParams(m: BModule, t: PType, rettype, params: var Rope,
params.addf(", NI $1Len_$2", [param.loc.r, j.rope])
inc(j)
arr = arr[0].skipTypes({tySink})
if t[0] != nil and isInvalidReturnType(m.config, t):
if t[0] != nil and t[0].kind != tyVoid and isInvalidReturnType(m.config, t):
var arr = t[0]
if params != "(": params.add(", ")
if mapReturnType(m.config, t[0]) != ctArray:
Expand Down
4 changes: 3 additions & 1 deletion compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ proc treatGlobalDifferentlyForHCR(m: BModule, s: PSym): bool =

proc assignGlobalVar(p: BProc, n: PNode; value: Rope) =
let s = n.sym
if s.typ.kind == tyVoid: return

if s.loc.k == locNone:
fillBackendName(p.module, s)
fillLoc(s.loc, locGlobalVar, n, OnHeap)
Expand Down Expand Up @@ -1077,7 +1079,7 @@ proc genProcAux(m: BModule, prc: PSym) =
if sfInjectDestructors in prc.flags:
procBody = injectDestructorCalls(m.g.graph, m.idgen, prc, procBody)

if sfPure notin prc.flags and prc.typ[0] != nil:
if sfPure notin prc.flags and prc.typ[0] != nil and prc.typ[0].kind != tyVoid:
if resultPos >= prc.ast.len:
internalError(m.config, prc.info, "proc has no result symbol")
let resNode = prc.ast[resultPos]
Expand Down
2 changes: 1 addition & 1 deletion compiler/seminst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ proc instantiateProcType(c: PContext, pt: TIdTable,
if result[0] != nil:
propagateToOwner(result, result[0])

eraseVoidParams(result)
#eraseVoidParams(result)
skipIntLiteralParams(result, c.idgen)

prc.typ = result
Expand Down
4 changes: 2 additions & 2 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,8 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
if isType: localError(c.config, a.info, "':' expected")
if kind in {skTemplate, skMacro}:
typ = newTypeS(tyUntyped, c)
elif skipTypes(typ, {tyGenericInst, tyAlias, tySink}).kind == tyVoid:
continue
#elif skipTypes(typ, {tyGenericInst, tyAlias, tySink}).kind == tyVoid:
# continue

for j in 0..<a.len-2:
var arg = newSymG(skParam, if a[j].kind == nkPragmaExpr: a[j][0] else: a[j], c)
Expand Down
6 changes: 3 additions & 3 deletions compiler/semtypinst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ proc replaceTypeVarsN(cl: var TReplTypeVars, n: PNode; start=0): PNode =
of nkOpenSymChoice, nkClosedSymChoice: result = n
of nkSym:
result.sym = replaceTypeVarsS(cl, n.sym)
if result.sym.typ.kind == tyVoid:
#if result.sym.typ.kind == tyVoid:
# don't add the 'void' field
result = newNodeI(nkRecList, n.info)
# result = newNodeI(nkRecList, n.info)
of nkRecWhen:
var branch: PNode = nil # the branch to take
for i in 0..<n.len:
Expand Down Expand Up @@ -629,7 +629,7 @@ proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType =
result.flags.incl tfRequiresInit

of tyProc:
eraseVoidParams(result)
#eraseVoidParams(result)
skipIntLiteralParams(result, cl.c.idgen)

of tyRange:
Expand Down
43 changes: 39 additions & 4 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -607,19 +607,43 @@ proc procParamTypeRel(c: var TCandidate, f, a: PType): TTypeRelation =

proc procTypeRel(c: var TCandidate, f, a: PType): TTypeRelation =
case a.kind
of tyProc:
if f.len != a.len: return
of tyProc:
# void skipping can't work with this
#if f.len != a.len: return
result = isEqual # start with maximum; also correct for no
# params at all

template checkParam(f, a) =
result = minRel(result, procParamTypeRel(c, f, a))
if result == isNone: return

# compare params with voids skipped over
var
fi = 1
ai = 1
while true:
while fi < f.len and f[fi].kind == tyVoid:
inc fi
continue
while ai < a.len and a[ai].kind == tyVoid:
inc ai
continue

if (fi == f.len and ai != a.len) or (fi != f.len and ai == a.len):
return isNone

if fi == f.len and ai == a.len:
result = isEqual
break

checkParam(f[fi], a[ai])
inc fi
inc ai

# Note: We have to do unification for the parameters before the
# return type!
for i in 1..<f.len:
checkParam(f[i], a[i])
#for i in 1..<f.len:
# checkParam(f[i], a[i])

if f[0] != nil:
if a[0] != nil:
Expand Down Expand Up @@ -2515,6 +2539,13 @@ proc matchesAux(c: PContext, n, nOrig: PNode, m: var TCandidate, marker: var Int
m.baseTypeMatch = false
m.typedescMatched = false
n[a] = prepareOperand(c, formal.typ, n[a])

# void params get skipped implicitly (maybe a bad idea)
if formal.typ.kind == tyVoid and n[a].typ.kind != tyVoid:
setSon(m.call, f, m.callee.n[f])
inc f
continue

arg = paramTypesMatch(m, formal.typ, n[a].typ,
n[a], nOrig[a])
if arg == nil:
Expand Down Expand Up @@ -2595,7 +2626,11 @@ proc matches*(c: PContext, n, nOrig: PNode, m: var TCandidate) =
var container = newNodeIT(cnKind, n.info, arrayConstr(c, n.info))
setSon(m.call, formal.position + 1,
implicitConv(nkHiddenStdConv, formal.typ, container, m, c))
elif formal.typ.kind == tyVoid:
# Void params get skipped so we can just pass this along
setSon(m.call, formal.position+1, m.callee.n[f])
else:
#echo m.callee
# no default value
m.state = csNoMatch
m.firstMismatch.kind = kMissingParam
Expand Down
3 changes: 2 additions & 1 deletion compiler/typeallowed.nim
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
of tyStatic:
if kind notin {skParam}: result = t
of tyVoid:
if taField notin flags: result = t
discard
#if taField notin flags: result = t
of tyTypeClasses:
if tfGenericTypeParam in t.flags or taConcept in flags: #or taField notin flags:
discard
Expand Down
2 changes: 1 addition & 1 deletion compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ proc compatibleEffects*(formal, actual: PType): EffectsCompat =


proc isCompileTimeOnly*(t: PType): bool {.inline.} =
result = t.kind in {tyTypeDesc, tyStatic}
result = t.kind in {tyTypeDesc, tyStatic, tyVoid}

proc containsCompileTimeOnly*(t: PType): bool =
if isCompileTimeOnly(t): return true
Expand Down