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

Add specs for for loop with attribute target #1166

Merged
merged 3 commits into from
Jun 25, 2024
Merged
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
70 changes: 70 additions & 0 deletions language/for_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,76 @@ class OFor
$var = old_global_var
end

it "allows an attribute as an iterator name" do
class OFor
attr_accessor :target
end

ofor = OFor.new
m = [1,2,3]
n = 0
for ofor.target in m
n += 1
end
ofor.target.should == 3
n.should == 3
end

# Segfault in MRI 3.3 and lower: https://bugs.ruby-lang.org/issues/20468
ruby_version_is "3.4" do
Copy link
Member

@andrykonchin andrykonchin Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add a link to the issue. I suppose the fix will be backported soon so the version guard will be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the link, but the bug report doesn't mention anything about backports, so I'm not sure if we should be expecting it. But the link doesn't hurt.

I added specs for the syntax for a[0]/a[:a] in x as well, according to Prism these are different parse nodes than the regular for foo.bar in x

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use ruby_bug for this, and it should be backported (segfault fixes should basically always be backported)

it "allows an attribute with safe navigation as an iterator name" do
class OFor
attr_accessor :target
end

ofor = OFor.new
m = [1,2,3]
n = 0
eval <<~RUBY
for ofor&.target in m
n += 1
end
RUBY
ofor.target.should == 3
n.should == 3
end

it "allows an attribute with safe navigation on a nil base as an iterator name" do
ofor = nil
m = [1,2,3]
n = 0
eval <<~RUBY
for ofor&.target in m
n += 1
end
RUBY
ofor.should be_nil
n.should == 3
end
end

it "allows an array index writer as an iterator name" do
arr = [:a, :b, :c]
m = [1,2,3]
n = 0
for arr[1] in m
n += 1
end
arr.should == [:a, 3, :c]
n.should == 3
end

it "allows a hash index writer as an iterator name" do
hash = { a: 10, b: 20, c: 30 }
m = [1,2,3]
n = 0
for hash[:b] in m
n += 1
end
hash.should == { a: 10, b: 3, c: 30 }
n.should == 3
end

# 1.9 behaviour verified by nobu in
# http://redmine.ruby-lang.org/issues/show/2053
it "yields only as many values as there are arguments" do
Expand Down