Skip to content

Commit

Permalink
Fix support for dynamic keys. (#8273)
Browse files Browse the repository at this point in the history
* Skip RBS test.
  • Loading branch information
ioquatix committed Aug 24, 2023
1 parent bd22bb2 commit d4c720a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 3 additions & 5 deletions cont.c
Expand Up @@ -2118,7 +2118,7 @@ rb_fiber_storage_get(VALUE self)
static int
fiber_storage_validate_each(VALUE key, VALUE value, VALUE _argument)
{
rb_check_id(&key);
Check_Type(key, T_SYMBOL);

return ST_CONTINUE;
}
Expand Down Expand Up @@ -2190,8 +2190,7 @@ rb_fiber_storage_set(VALUE self, VALUE value)
static VALUE
rb_fiber_storage_aref(VALUE class, VALUE key)
{
ID id = rb_check_id(&key);
if (!id) return Qnil;
Check_Type(key, T_SYMBOL);

VALUE storage = fiber_storage_get(fiber_current(), FALSE);
if (storage == Qnil) return Qnil;
Expand All @@ -2212,8 +2211,7 @@ rb_fiber_storage_aref(VALUE class, VALUE key)
static VALUE
rb_fiber_storage_aset(VALUE class, VALUE key, VALUE value)
{
ID id = rb_check_id(&key);
if (!id) return Qnil;
Check_Type(key, T_SYMBOL);

VALUE storage = fiber_storage_get(fiber_current(), value != Qnil);
if (storage == Qnil) return Qnil;
Expand Down
14 changes: 14 additions & 0 deletions spec/ruby/core/fiber/storage_spec.rb
Expand Up @@ -74,6 +74,20 @@
Fiber.new { Fiber[:life] }.resume.should be_nil
end
end

ruby_version_is "3.2.3" do
it "can use dynamically defined keys" do
key = :"#{self.class.name}#.#{self.object_id}"
Fiber.new { Fiber[key] = 42; Fiber[key] }.resume.should == 42
end

it "can't use invalid keys" do
invalid_keys = [Object.new, "Foo", 12]
invalid_keys.each do |key|
-> { Fiber[key] }.should raise_error(TypeError)
end
end
end
end

describe "Fiber.[]=" do
Expand Down
2 changes: 2 additions & 0 deletions tool/rbs_skip_tests
Expand Up @@ -25,6 +25,8 @@ test_subtract(RBS::CliTest) running tests without Bundler
test_subtract_several_subtrahends(RBS::CliTest) running tests without Bundler
test_subtract_write(RBS::CliTest) running tests without Bundler

test_aref(FiberSingletonTest) the method should not accept String keys

NetSingletonTest depending on external resources
NetInstanceTest depending on external resources
TestHTTPRequest depending on external resources
Expand Down

0 comments on commit d4c720a

Please sign in to comment.