From ae8ae8fa9593417f757bdeb7b909d514319e874f Mon Sep 17 00:00:00 2001 From: metagn Date: Tue, 11 Mar 2025 11:59:21 +0300 Subject: [PATCH] fix canRaise for non-proc calls (#24752) fixes #24751 `typeof` leaves the object constructor as a call node for some reason, in this case it tries to access the first child of the type node but the object has no fields so the type field is empty. Alternatively the optimizer can stop looking into `typeof` (cherry picked from commit e2e77907795237961aa866717bc6fc3a226d6106) --- compiler/ast.nim | 8 +++++--- tests/types/ttypeofobjconstr.nim | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 tests/types/ttypeofobjconstr.nim diff --git a/compiler/ast.nim b/compiler/ast.nim index a187687f4e..3ed9a7c675 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -2089,14 +2089,16 @@ proc canRaise*(fn: PNode): bool = result = false elif fn.kind == nkSym and fn.sym.magic == mEcho: result = true - else: + elif fn.typ != nil and fn.typ.kind == tyProc and fn.typ.n != nil: # TODO check for n having sons? or just return false for now if not - if fn.typ != nil and fn.typ.n != nil and fn.typ.n[0].kind == nkSym: + if fn.typ.n[0].kind == nkSym: result = false else: - result = fn.typ != nil and fn.typ.n != nil and ((fn.typ.n[0].len < effectListLen) or + result = ((fn.typ.n[0].len < effectListLen) or (fn.typ.n[0][exceptionEffects] != nil and fn.typ.n[0][exceptionEffects].safeLen > 0)) + else: + result = false proc toHumanStrImpl[T](kind: T, num: static int): string = result = $kind diff --git a/tests/types/ttypeofobjconstr.nim b/tests/types/ttypeofobjconstr.nim new file mode 100644 index 0000000000..2825f40d85 --- /dev/null +++ b/tests/types/ttypeofobjconstr.nim @@ -0,0 +1,5 @@ +# issue #24751 + +type A = object + +var a: typeof(A())