From b679b563db6cde5b633bd8bf92f32e3e476d2b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sat, 21 Sep 2024 13:00:58 +0200 Subject: [PATCH] Add `Exception::CallStack.empty` (#15017) This is a follow-up on https://github.com/crystal-lang/crystal/pull/15002 which explicitly assigns a dummy callstack to `RetryLookupWithLiterals` for performance reasons. `CallStack.empty` is intended to make this use case a bit more ergonomical. It doesn't require allocating a dummy instance with fake data. Instead, it's an explicitly empty callstack. This makes this mechanism easier to re-use in other places (ref https://github.com/crystal-lang/crystal/issues/11658#issuecomment-2352649774). --- src/compiler/crystal/semantic/call.cr | 4 +--- src/exception/call_stack.cr | 7 +++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/compiler/crystal/semantic/call.cr b/src/compiler/crystal/semantic/call.cr index e265829a919e..1fa4379d543e 100644 --- a/src/compiler/crystal/semantic/call.cr +++ b/src/compiler/crystal/semantic/call.cr @@ -13,10 +13,8 @@ class Crystal::Call property? uses_with_scope = false class RetryLookupWithLiterals < ::Exception - @@dummy_call_stack = Exception::CallStack.new - def initialize - self.callstack = @@dummy_call_stack + self.callstack = Exception::CallStack.empty end end diff --git a/src/exception/call_stack.cr b/src/exception/call_stack.cr index c80f73a6ce48..09173f2e5500 100644 --- a/src/exception/call_stack.cr +++ b/src/exception/call_stack.cr @@ -31,8 +31,11 @@ struct Exception::CallStack @callstack : Array(Void*) @backtrace : Array(String)? - def initialize - @callstack = CallStack.unwind + def initialize(@callstack : Array(Void*) = CallStack.unwind) + end + + def self.empty + new([] of Void*) end def printable_backtrace : Array(String)