From 70d771d1a97d12c508fe0801ce643b5bd8310fd7 Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 2 Sep 2023 11:32:46 +0300 Subject: [PATCH] fix isNil folding for compile time closures (#22574) fixes #20543 (cherry picked from commit bd6adbcc9d5a22f686eed7bc988a1c0b1b0a17e4) --- compiler/semfold.nim | 8 +++++++- tests/vm/tvmmisc.nim | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/semfold.nim b/compiler/semfold.nim index 07c3b3ae4e..67b708d270 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -230,7 +230,13 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; idgen: IdGenerator; g: ModuleGraph): P of mMulF64: result = newFloatNodeT(getFloat(a) * getFloat(b), n, g) of mDivF64: result = newFloatNodeT(getFloat(a) / getFloat(b), n, g) - of mIsNil: result = newIntNodeT(toInt128(ord(a.kind == nkNilLit)), n, idgen, g) + of mIsNil: + let val = a.kind == nkNilLit or + # nil closures have the value (nil, nil) + (a.typ != nil and skipTypes(a.typ, abstractRange).kind == tyProc and + a.kind == nkTupleConstr and a.len == 2 and + a[0].kind == nkNilLit and a[1].kind == nkNilLit) + result = newIntNodeT(toInt128(ord(val)), n, idgen, g) of mLtI, mLtB, mLtEnum, mLtCh: result = newIntNodeT(toInt128(ord(getOrdValue(a) < getOrdValue(b))), n, idgen, g) of mLeI, mLeB, mLeEnum, mLeCh: diff --git a/tests/vm/tvmmisc.nim b/tests/vm/tvmmisc.nim index ab8fae81cd..9bc6e38017 100644 --- a/tests/vm/tvmmisc.nim +++ b/tests/vm/tvmmisc.nim @@ -229,6 +229,14 @@ block: # bug #15595 static: main() main() +block: # issue #20543 + type F = proc() + const myArray = block: + var r: array[1, F] + r[0] = nil + r + doAssert isNil(myArray[0]) + # bug #15363 import sequtils