From bac59ecd5a33bae43300a3499f5723e30e57cfb7 Mon Sep 17 00:00:00 2001 From: Julien Portalier Date: Fri, 24 Jan 2025 10:42:08 +0100 Subject: [PATCH] Fix invalid returns in class getter's lazy evaluation blocks (#15364) Using `return` in a class getter will immediately return from the generated method, never setting the class variable to the returned value, which is kept nil and so the initializer code is called repeatedly instead of being called _once_ then cached. --- spec/std/socket/spec_helper.cr | 4 +++- spec/support/time.cr | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/std/socket/spec_helper.cr b/spec/std/socket/spec_helper.cr index 276e2f4195f2..38aadaf802d9 100644 --- a/spec/std/socket/spec_helper.cr +++ b/spec/std/socket/spec_helper.cr @@ -2,7 +2,9 @@ require "spec" require "socket" module SocketSpecHelper - class_getter?(supports_ipv6 : Bool) do + class_getter?(supports_ipv6 : Bool) { detect_supports_ipv6? } + + private def self.detect_supports_ipv6? : Bool TCPServer.open("::1", 0) { return true } false rescue Socket::Error diff --git a/spec/support/time.cr b/spec/support/time.cr index d550a83af2c3..2b4738b5d71e 100644 --- a/spec/support/time.cr +++ b/spec/support/time.cr @@ -72,7 +72,9 @@ end # Enable the `SeTimeZonePrivilege` privilege before changing the system time # zone. This is necessary because the privilege is by default granted but # disabled for any new process. This only needs to be done once per run. - class_getter? time_zone_privilege_enabled : Bool do + class_getter?(time_zone_privilege_enabled : Bool) { detect_time_zone_privilege_enabled? } + + private def self.detect_time_zone_privilege_enabled? : Bool if LibC.LookupPrivilegeValueW(nil, SeTimeZonePrivilege, out time_zone_luid) == 0 raise RuntimeError.from_winerror("LookupPrivilegeValueW") end