sorbet-runtime 0.6.13188 → 0.6.13189

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee766380df193f2cc47403e32ea913cd5da012247fec0a6c19609b20ddf0667f
4
- data.tar.gz: 6841b53a14b1e754321975d39782270accf9998c9a772bcd7a9fb3e5f01a11c2
3
+ metadata.gz: df6bf8fcb2561e2428429b30dc9059636064a8a735e253fe40f42345c634dc9a
4
+ data.tar.gz: 156e30c64f4ef4dad43b989ee8a462d458b8c282956de77a9f1f9e21ee2b08d8
5
5
  SHA512:
6
- metadata.gz: 4cf62dfc5309a5d6c3568dd735aad71318e88b07ef06866ffb0f5193b9c398bd6137090734dda461d95282976b0ed13610ea0f9d2c05a72f0a05170e9bb11815
7
- data.tar.gz: 8c14515625524567c0aba3f3004a35880004b3f2d9f4da072020f89373d605f0c4b464070e77276d3d56e1039fea1345cce78857de8a3788bf4bfb9b9b3d571a
6
+ metadata.gz: 85b4031fe378bad5cb9982bf02b9b4f52f888a02f3d5ef3442ef27c0343da8c9415bf443b798933807b59fb74519f8706965ac0a06a72f883ae15d6b29a5cdab
7
+ data.tar.gz: de199554534b1c28bbcfd69c7718bbd1b128190e7d91102eb93d24c257b4df186be56c7c617ad1fb08491ff18efa35dfc0494197a903367a14eff2bdb34e0a30
@@ -3,61 +3,14 @@
3
3
 
4
4
  # Cut down version of Chalk::Tools::ClassUtils with only :replace_method functionality.
5
5
  # Extracted to a separate namespace so the type system can be used standalone.
6
+ #
7
+ # Note: the functionality to "restore" a method was removed, because it is no
8
+ # longer being used by sorbet-runtime. Restoring a method requires care--if you
9
+ # need to reintroduce this functionality, consult the git history for how to do
10
+ # it safely.
6
11
  module T::Private::ClassUtils
7
- class ReplacedMethod
8
- def initialize(mod, old_method, new_method, overwritten, visibility)
9
- if old_method.name != new_method.name
10
- raise "Method names must match. old=#{old_method.name} new=#{new_method.name}"
11
- end
12
- @mod = mod
13
- @old_method = old_method
14
- @new_method = new_method
15
- @overwritten = overwritten
16
- @name = old_method.name
17
- @visibility = visibility
18
- @restored = false
19
- end
20
-
21
- def restore
22
- # The check below would also catch this, but this makes the failure mode much clearer
23
- if @restored
24
- raise "Method '#{@name}' on '#{@mod}' was already restored"
25
- end
26
-
27
- if @mod.instance_method(@name) != @new_method
28
- raise "Trying to restore #{@mod}##{@name} but the method has changed since the call to replace_method"
29
- end
30
-
31
- @restored = true
32
-
33
- if @overwritten
34
- # The original method was overwritten. Overwrite again to restore it.
35
- T::Configuration.without_ruby_warnings do
36
- @mod.send(:define_method, @old_method.name, @old_method)
37
- end
38
- else
39
- # The original method was in an ancestor. Restore it by removing the overriding method.
40
- @mod.send(:remove_method, @old_method.name)
41
- end
42
-
43
- # Restore the visibility. Note that we need to do this even when we call remove_method
44
- # above, because the module may have set custom visibility for a method it inherited.
45
- @mod.send(@visibility, @old_method.name)
46
-
47
- nil
48
- end
49
-
50
- def bind(obj)
51
- @old_method.bind(obj)
52
- end
53
-
54
- def to_s
55
- @old_method.to_s
56
- end
57
- end
58
-
59
12
  # `name` must be an instance method (for class methods, pass in mod.singleton_class)
60
- private_class_method def self.visibility_method_name(mod, name)
13
+ def self.visibility_method_name(mod, name)
61
14
  if mod.public_method_defined?(name)
62
15
  :public
63
16
  elsif mod.protected_method_defined?(name)
@@ -65,7 +18,10 @@ module T::Private::ClassUtils
65
18
  elsif mod.private_method_defined?(name)
66
19
  :private
67
20
  else
68
- mod.method(name) # Raises
21
+ # Raises a NameError formatted like the Ruby VM would (the exact text formatting
22
+ # of these errors changed across Ruby VM versions, in ways that would sometimes
23
+ # cause tests to fail if they were dependent on hard coding errors).
24
+ mod.method(name)
69
25
  end
70
26
  end
71
27
 
@@ -90,15 +46,18 @@ module T::Private::ClassUtils
90
46
  end
91
47
  end
92
48
 
93
- # Replaces a method, either by overwriting it (if it is defined directly on `mod`) or by
94
- # overriding it (if it is defined by one of mod's ancestors). If `original_only` is
95
- # false, returns a ReplacedMethod instance on which you can call `bind(...).call(...)`
96
- # to call the original method, or `restore` to restore the original method (by
97
- # overwriting or removing the override).
49
+ # Replaces a method, either by overwriting it (if it is defined directly on
50
+ # `mod`) or by overriding it (if it is defined by one of mod's ancestors).
51
+ #
52
+ # Takes the `original_method` as a parameter, so it does not return anything.
53
+ #
54
+ # Can also avoid `T.let` pinning errors by letting the caller pre-compute the
55
+ # `original_method`, so it knows that it will always be defined (because it
56
+ # doesn't know that the block will always run once)
98
57
  #
99
- # If `original_only` is true, return the `UnboundMethod` representing the original method.
100
- def self.replace_method(mod, name, original_only=false, &blk)
101
- original_method = mod.instance_method(name)
58
+ # Does not share code with `replace_method_with_handle`, for performance (do
59
+ # not want to increase the call stack, as this is a very sensitive code path).
60
+ def self.replace_method(original_method, mod, name, &blk)
102
61
  original_visibility = visibility_method_name(mod, name)
103
62
  original_owner = original_method.owner
104
63
 
@@ -117,18 +76,12 @@ module T::Private::ClassUtils
117
76
  end
118
77
  end
119
78
 
120
- overwritten = original_owner == mod
121
79
  T::Configuration.without_ruby_warnings do
122
80
  T::Private::DeclState.current.without_on_method_added do
123
81
  def_with_visibility(mod, name, original_visibility, &blk)
124
82
  end
125
83
  end
126
84
 
127
- if original_only
128
- original_method
129
- else
130
- new_method = mod.instance_method(name)
131
- ReplacedMethod.new(mod, original_method, new_method, overwritten, original_visibility)
132
- end
85
+ nil
133
86
  end
134
87
  end
@@ -242,7 +242,7 @@ module T::Private::Methods
242
242
  # (or unwrap back to the original method).
243
243
  key = method_owner_and_name_to_key(mod, method_name)
244
244
  unless current_declaration.raw
245
- T::Private::ClassUtils.replace_method(mod, method_name, true) do |*args, &blk|
245
+ T::Private::ClassUtils.replace_method(original_method, mod, method_name) do |*args, &blk|
246
246
  method_sig = T::Private::Methods.maybe_run_sig_block_for_key(key)
247
247
  method_sig ||= T::Private::Methods._handle_missing_method_signature(
248
248
  self,
@@ -541,15 +541,21 @@ module T::Private::Methods
541
541
  end
542
542
  @old_hooks = nil
543
543
  else
544
- old_included = T::Private::ClassUtils.replace_method(Module, :included, true) do |arg|
544
+ # Grab the original methods before replacing them, so that each block
545
+ # closure can reference a variable that is already assigned.
546
+ # (Do this directly, to avoid pinning errors)
547
+ old_included = Module.instance_method(:included)
548
+ T::Private::ClassUtils.replace_method(old_included, Module, :included) do |arg|
545
549
  old_included.bind_call(self, arg)
546
550
  ::T::Private::Methods._hook_impl(arg, false, self)
547
551
  end
548
- old_extended = T::Private::ClassUtils.replace_method(Module, :extended, true) do |arg|
552
+ old_extended = Module.instance_method(:extended)
553
+ T::Private::ClassUtils.replace_method(old_extended, Module, :extended) do |arg|
549
554
  old_extended.bind_call(self, arg)
550
555
  ::T::Private::Methods._hook_impl(arg, true, self)
551
556
  end
552
- old_inherited = T::Private::ClassUtils.replace_method(Class, :inherited, true) do |arg|
557
+ old_inherited = Class.instance_method(:inherited)
558
+ T::Private::ClassUtils.replace_method(old_inherited, Class, :inherited) do |arg|
553
559
  old_inherited.bind_call(self, arg)
554
560
  ::T::Private::Methods._hook_impl(arg, false, self)
555
561
  end
@@ -602,22 +608,6 @@ module T::Private::Methods
602
608
  end
603
609
  mod.extend(SingletonMethodHooks)
604
610
  end
605
-
606
- # `name` must be an instance method (for class methods, pass in mod.singleton_class)
607
- def self.visibility_method_name(mod, name)
608
- if mod.public_method_defined?(name)
609
- :public
610
- elsif mod.protected_method_defined?(name)
611
- :protected
612
- elsif mod.private_method_defined?(name)
613
- :private
614
- else
615
- # Raises a NameError formatted like the Ruby VM would (the exact text formatting
616
- # of these errors changed across Ruby VM versions, in ways that would sometimes
617
- # cause tests to fail if they were dependent on hard coding errors).
618
- mod.method(name)
619
- end
620
- end
621
611
  end
622
612
 
623
613
  # This has to be here, and can't be nested inside `T::Private::Methods`,
@@ -16,7 +16,7 @@ module T::Private::Methods::CallValidation
16
16
  # @param method_sig [T::Private::Methods::Signature]
17
17
  # @return [UnboundMethod] the new wrapper method (or the original one if we didn't wrap it)
18
18
  def self.wrap_method_if_needed(mod, method_sig, original_method)
19
- original_visibility = T::Private::Methods.visibility_method_name(mod, method_sig.method_name)
19
+ original_visibility = T::Private::ClassUtils.visibility_method_name(mod, method_sig.method_name)
20
20
  if method_sig.mode == T::Private::Methods::Modes.abstract
21
21
  create_abstract_wrapper(mod, method_sig, original_method, original_visibility)
22
22
  # Do nothing in this case; this method was not wrapped in _on_method_added.
@@ -304,7 +304,7 @@ module T::Private::Methods::SignatureValidation
304
304
  end
305
305
 
306
306
  private_class_method def self.method_visibility(method)
307
- T::Private::Methods.visibility_method_name(method.owner, method.name)
307
+ T::Private::ClassUtils.visibility_method_name(method.owner, method.name)
308
308
  end
309
309
 
310
310
  # Higher = more restrictive.
data/lib/types/struct.rb CHANGED
@@ -10,7 +10,8 @@ end
10
10
  class T::Struct < T::InexactStruct
11
11
  def self.inherited(subclass)
12
12
  super(subclass)
13
- T::Private::ClassUtils.replace_method(subclass.singleton_class, :inherited, true) do |s|
13
+ original_method = subclass.singleton_class.instance_method(:inherited)
14
+ T::Private::ClassUtils.replace_method(original_method, subclass.singleton_class, :inherited) do |s|
14
15
  super(s)
15
16
  raise "#{self.name} is a subclass of T::Struct and cannot be subclassed"
16
17
  end
@@ -23,7 +24,8 @@ class T::ImmutableStruct < T::InexactStruct
23
24
  def self.inherited(subclass)
24
25
  super(subclass)
25
26
 
26
- T::Private::ClassUtils.replace_method(subclass.singleton_class, :inherited, true) do |s|
27
+ original_method = subclass.singleton_class.instance_method(:inherited)
28
+ T::Private::ClassUtils.replace_method(original_method, subclass.singleton_class, :inherited) do |s|
27
29
  super(s)
28
30
  raise "#{self.name} is a subclass of T::ImmutableStruct and cannot be subclassed"
29
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorbet-runtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.13188
4
+ version: 0.6.13189
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stripe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-05-05 00:00:00.000000000 Z
11
+ date: 2026-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: benchmark