Skip to content

Commit

Permalink
Fix invalid returns in class getter's lazy evaluation blocks (#15364)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ysbaddaden authored Jan 24, 2025
1 parent df06679 commit bac59ec
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion spec/std/socket/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion spec/support/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bac59ec

Please sign in to comment.