Skip to content

Commit

Permalink
fix #16758 Nim crashes in fixAbstractType (#20855)
Browse files Browse the repository at this point in the history
* fix #16758 Nim crashes in fixAbstractType

* Update compiler/semexprs.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
  • Loading branch information
bung87 and Araq authored Dec 11, 2022
1 parent 07be179 commit 1585bfe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,9 @@ proc semArrayConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PTyp
proc fixAbstractType(c: PContext, n: PNode) =
for i in 1..<n.len:
let it = n[i]
if it == nil:
localError(c.config, n.info, "'$1' has nil child at index $2" % [renderTree(n, {renderNoComments}), $i])
return
# do not get rid of nkHiddenSubConv for OpenArrays, the codegen needs it:
if it.kind == nkHiddenSubConv and
skipTypes(it.typ, abstractVar).kind notin {tyOpenArray, tyVarargs}:
Expand Down
38 changes: 38 additions & 0 deletions tests/macros/t16758.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
discard """
errormsg: "'blk.p(a)' has nil child at index 1"
action: reject
"""
import macros

type BlockLiteral[T] = object
p: T

proc p[T](a:int) = echo 1
proc p[T](a:string) = echo "a"

iterator arguments(formalParams: NimNode): NimNode =
var iParam = 0
for i in 1 ..< formalParams.len:
let pp = formalParams[i]
for j in 0 .. pp.len - 3:
yield pp[j]
inc iParam

macro implementInvoke(T: typedesc): untyped =
let t = getTypeImpl(T)[1]

let call = newCall(newDotExpr(ident"blk", ident"p"))
let params = copyNimTree(t[0])
result = newProc(ident"invoke", body = call)
# result[2] = newTree(nnkGenericParams,T)
for n in arguments(params):
call.add(n)

params.insert(1, newIdentDefs(ident"blk", newTree(nnkBracketExpr, bindSym"BlockLiteral", T)))
result.params = params

proc getInvoke(T: typedesc) =
implementInvoke(T)


getInvoke(proc(a: int))

0 comments on commit 1585bfe

Please sign in to comment.