Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ractor] shareable_constant_value: literal makes constants shareable between ractors #892

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions language/fixtures/shareable_constant_value_magic_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# shareable_constant_value: literal

A = "rac"
H = "tor"

Ractor.new do
print A
print H
end.take
42 changes: 42 additions & 0 deletions language/ractor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative "../spec_helper"

describe "magic comments in ractors" do
ruby_version_is "3.0" do
it "makes constants shareable between ractors" do
out = ruby_exe(fixture(__FILE__, "shareable_constant_value_magic_comment.rb"), options: "-W0")
out.should == "ractor"
end

it "makes constants shareable between ractors" do
a, b, c = Class.new.class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
# shareable_constant_value: experimental_everything
A = [[1]]
# shareable_constant_value: none
B = [[2]]
# shareable_constant_value: literal
C = [["shareable", "constant#{self.class.name}"]]
[A, B, C]
RUBY
Ractor.shareable?(a).should == true
Ractor.shareable?(b).should == false
Ractor.shareable?(c).should == true
end

context "when a shared constant is not a literal" do
it "raises an error when is computed" do
-> { Class.new.class_eval(<<~RUBY, __FILE__, __LINE__ + 1) }.should raise_error(Ractor::IsolationError, "cannot assign unshareable object to C")
# shareable_constant_value: literal
C = ["Not " + "shareable"]
RUBY
end

it "raises an error when referencing a non-frozen variable" do
-> { Class.new.class_eval(<<~RUBY, __FILE__, __LINE__ + 1) }.should raise_error(Ractor::IsolationError, "cannot assign unshareable object to C")
# shareable_constant_value: literal
value = "value"
C = value
RUBY
end
end
end
end